Skip to content

Instantly share code, notes, and snippets.

@josefandersson
Last active June 8, 2020 14:51
Show Gist options
  • Save josefandersson/92d66db600802d9ce53f29ec0041a33f to your computer and use it in GitHub Desktop.
Save josefandersson/92d66db600802d9ce53f29ec0041a33f to your computer and use it in GitHub Desktop.

Revisions

  1. josefandersson revised this gist Jun 8, 2020. 1 changed file with 9 additions and 7 deletions.
    16 changes: 9 additions & 7 deletions background.js
    Original file line number Diff line number Diff line change
    @@ -15,13 +15,15 @@ chrome.commands.onCommand.addListener(cmd => {

    function tabMoveWindow(tab, dir) {
    chrome.windows.getAll(wins => {
    if (wins.length <= 1) return;
    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 });
    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 });
    });
    });
    });
    });
    }
  2. josefandersson revised this gist Jun 7, 2020. No changes.
  3. josefandersson created this gist Jun 7, 2020.
    27 changes: 27 additions & 0 deletions background.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    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) return;
    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 });
    });
    });
    });
    }
    43 changes: 43 additions & 0 deletions manifest.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    {
    "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
    }