Last active
June 8, 2020 14:51
-
-
Save josefandersson/92d66db600802d9ce53f29ec0041a33f to your computer and use it in GitHub Desktop.
A chromium extension for moving tabs with key commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
chrome.commands.onCommand.addListener(cmd => { | |
chrome.tabs.query({ currentWindow:true, active:true }, tabs => { | |
const tab = tabs[0]; | |
if (!tab) return; | |
switch (cmd) { | |
case 'move-left': chrome.tabs.move(tab.id, { index:Math.max(0, tab.index-1) }); break; | |
case 'move-right': chrome.tabs.move(tab.id, { index:tab.index+1 }); break; | |
case 'move-to-first': chrome.tabs.move(tab.id, { index:0 }); break; | |
case 'move-to-last': chrome.tabs.move(tab.id, { index:-1 }); break; | |
case 'move-next-window': tabMoveWindow(tab, 1); break; | |
case 'move-prev-window': tabMoveWindow(tab, -1); break; | |
} | |
}); | |
}); | |
function tabMoveWindow(tab, dir) { | |
chrome.windows.getAll(wins => { | |
if (wins.length <= 1) | |
chrome.windows.create({ tabId:tab.id }); | |
else | |
for (let i = 0; i < wins.length; i++) | |
if (tab.windowId === wins[i].id) | |
return chrome.tabs.move(tab.id, { windowId: wins[(i + dir + wins.length) % wins.length].id, index:-1 }, newTab => { | |
chrome.tabs.highlight({ windowId:newTab.windowId, tabs:[newTab.index] }, () => { | |
chrome.windows.update(newTab.windowId, { focused:true }); | |
}); | |
}); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "Move tabs", | |
"version": "1.0", | |
"description": "Use commands to move tabs", | |
"permissions": ["tabs"], | |
"background": { | |
"scripts": ["background.js"], | |
"persistent": false | |
}, | |
"commands": { | |
"move-to-first": { | |
"suggested_key": { | |
"default": "Alt+K" | |
}, | |
"description": "Move current tab leftmost" | |
}, | |
"move-to-last": { | |
"suggested_key": { | |
"default": "Alt+J" | |
}, | |
"description": "Move current tab rightmost" | |
}, | |
"move-left": { | |
"suggested_key": { | |
"default": "Alt+H" | |
}, | |
"description": "Move current tab left" | |
}, | |
"move-right": { | |
"suggested_key": { | |
"default": "Alt+L" | |
}, | |
"description": "Move current tab right" | |
}, | |
"move-next-window": { | |
"description": "Move current tab to the next window" | |
}, | |
"move-prev-window": { | |
"description": "Move current tab to the previous window" | |
} | |
}, | |
"manifest_version": 2 | |
} |
Updated:
- If only one window exists and move-next/prev-window is pressed, a new window will be created and the tab moved there.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use: