Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature req: Reatach tab to original window #64

Open
kgabor545 opened this issue Feb 12, 2023 · 1 comment
Open

Feature req: Reatach tab to original window #64

kgabor545 opened this issue Feb 12, 2023 · 1 comment

Comments

@kgabor545
Copy link

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
Copy link
Author

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]
      }
    })
  }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant