tabToWindow icon indicating copy to clipboard operation
tabToWindow copied to clipboard

Feature req: Reatach tab to original window

Open kgabor545 opened this issue 3 years ago • 1 comments

Thanks for this amazing extension, I use it with mouse gestures and it makes life much easier. I often send a tab to the second display, than bring it back to its original place. If I could bring it back with keycombination (therefore mouse gestures for me) that would be awesome. Please consider implementing it. Thank you

kgabor545 avatar Feb 12 '23 22:02 kgabor545

Nevermind. it took me 2hours, but chatGPT made it to me :o What do you think abut this? manifest.json

{
  "manifest_version": 3,
  "name": "Detach Tab",
  "version": "1.0",
  "permissions": ["activeTab", "windows", "system.display"],
  "commands": {
    "detach_tab": {
      "suggested_key": {
        "default": "Ctrl+Shift+E",
        "mac": "MacCtrl+Shift+E"
      },
      "description": "Detach the current tab to a new window"
    },
    "reattach_tab": {
      "suggested_key": {
        "default": "Ctrl+Shift+F",
        "mac": "MacCtrl+Shift+F"
      },
      "description": "Reattach Tab"
    }
  },
  "background": {
    "service_worker": "background.js"
  }
}

background.js

var originalTabInfo = {}

chrome.commands.onCommand.addListener(function (command) {
  if (command === 'detach_tab') {
    chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
      var activeTab = tabs[0]
      var currentWindow = activeTab.windowId
      originalTabInfo[activeTab.id] = {
        windowId: currentWindow,
        index: activeTab.index,
      }
      chrome.windows.get(currentWindow, function (currentWindowInfo) {
        var displays = chrome.system.display.getInfo(function (displays) {
          var currentDisplay = displays.find(function (display) {
            return (
              display.bounds.left <= currentWindowInfo.left &&
              display.bounds.left + display.bounds.width >
                currentWindowInfo.left
            )
          })
          var otherDisplay = displays.find(function (display) {
            return display.id !== currentDisplay.id
          })

          var createData = {
            tabId: activeTab.id,
            left: otherDisplay.bounds.left,
            top: otherDisplay.bounds.top,
            width: currentWindowInfo.width,
            height: currentWindowInfo.height,
            focused: true,
          }
          chrome.windows.create(createData, function (newWindow) {
            if (currentWindowInfo.state === 'maximized') {
              chrome.windows.update(newWindow.id, { state: 'maximized' })
            }
          })
        })
      })
    })
  } else if (command === 'reattach_tab') {
    chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
      var activeTab = tabs[0]
      var originalTab = originalTabInfo[activeTab.id]
      if (originalTab) {
        chrome.tabs.move(
          activeTab.id,
          {
            windowId: originalTab.windowId,
            index: originalTab.index,
          },
          function () {
            chrome.tabs.update(activeTab.id, {
              active: true,
            })
          }
        )
        delete originalTabInfo[activeTab.id]
      }
    })
  }
})

kgabor545 avatar Feb 13 '23 00:02 kgabor545