Chrome自定义插件实现新建标签页在当前tab右侧打开,关闭tab切换到左侧tab。

Chrome关闭标签页的行为是自动跳转到左侧的标签页。新建标签页总是在最右侧打开标签页。而我总是习惯于新建tab在当前tab右侧,关闭tab切换到左侧tab。

开发了这个插件还原了我的习惯。主要实现:
1. Ctrl+T新建标签页,在当前标签页右侧打开。
2. 点击标签栏里的“+”新建标签页,在当前标签页右侧打开。
3. 关闭标签页,切换到左侧的标签页。

实现代码如下:
mervyn.extension.js

// 记录当前活跃标签的索引和 ID
let lastActiveTabId = null;
let lastActiveIndex = 0;
// 实时追踪活跃标签,这是关闭后跳转左侧的基础
chrome.tabs.onActivated.addListener(activeInfo => {
    chrome.tabs.get(activeInfo.tabId, (tab) => {
        if (tab && tab.windowId !== chrome.windows.WINDOW_ID_NONE) {
            lastActiveTabId = tab.id;
            lastActiveIndex = tab.index;
        }
    });
});
//通过ctrl+T新标签页在当前标签页右侧打开
chrome.commands.onCommand.addListener((command) => {
  if (command === "newTabToTheRight") {
    chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
      const currentTab = tabs[0];
      chrome.tabs.create({
        index: currentTab.index + 1,
      })
    });
  }
});
//通过点击+号新建标签页在当前标签页右侧打开
chrome.tabs.onCreated.addListener((newTab) => {
    // 逻辑:如果是点击加号打开的,newTab 默认会在最右边(index 最大)
    // 我们需要把它移动到刚才活跃标签的右侧
    chrome.tabs.query({ currentWindow: true }, (tabs) => {
        // 找到刚才那个活跃标签的最新状态
        const opener = tabs.find(t => t.id === lastActiveTabId);
        
        if (opener && newTab.id !== opener.id) {
            // 执行移动:移动到原活跃标签 index + 1
            chrome.tabs.move(newTab.id, { index: opener.index + 1 });
        }
    });
});
// 关闭标签页切换到左侧的tab
chrome.tabs.onRemoved.addListener((tabId, removeInfo) => {
  chrome.tabs.query({ currentWindow: true }, function (tabs) {//查询当前窗口的所有tab
    if (tabs.length > 1) {
      // 当前tab的index
      chrome.tabs.query({ active: true, currentWindow: true }, function (currentTab) {
        var currentIndex = currentTab[0].index; //如果从一个tab页面点击打开新tab,currentIndex还是等于父tab的值,关闭新tab会调转到父tab的左侧tab。
        // 左边tab的index
        var leftIndex = currentIndex - 1;
        var rightIndex = currentIndex;//关闭了tab 右边tab的index就是当前的tab的index(chrome默认关闭tab是切换到右侧tab的)
        if (leftIndex < 0) {//左边没有tab了
          // leftIndex = tabs.length - 1; // Wrap around to the last tab  
          leftIndex = rightIndex; // 切换到右边tab  右边没有tab了应该会自动关闭窗口可以不处理
        }
        // tabs[leftIndex].index !== removeInfo.tabIndex  //可用于对比关闭的标签页index是否为左侧的标签页  但是测试无效
        // 切换到左边tab(没有时切换到右边tab)
        chrome.tabs.update(tabs[leftIndex].id, { active: true });
      });
    }
  });
});

manifest.json

{
  "manifest_version": 3,
  "name": "Mervyn's Tab",
  "version": "1.0",
  "description": "新建tab在当前tab右侧;关闭tab切换到左侧的tab;",
  "permissions": [
    "activeTab",
    "tabs"
  ],
  "background": {
    "service_worker": "mervyn.extension.js"
  },
  "commands": {
    "newTabToTheRight": {
      "suggested_key": {
        "default": "Ctrl+T" //这里设置快捷键无效(或许需要更新到最新的chrome),可以在chrome浏览器chrome://extensions/扩展管理里键盘快捷键里设置
      },
      "description": "Opens a new tab to the right of the current tab" // Valid description
    }
  }
}

另外manifest中ctrl+T快捷键新建标签页的配置可能会无效,需要在安装插件后,手动访问 chrome://extensions/shortcuts。如果发现快捷键一栏为空,需手动录入一次以确保劫持成功。

Leave a Comment