未经 "tabs" 许可,将后台脚本中的 "tab" 识别为您自己的 "newtab" 页面

Identify "tab" in background script as your own "newtab" page without "tabs" permission

我构建的扩展具有后台脚本并覆盖了“newtab”页面。 我可以在没有“tabs”或“all_urls”权限的情况下,在后台脚本“chrome.tabs.onCreated.addListener”中知道我自己的newtab页面已经打开了吗?使用“tabs”权限,我只需检查 URL.

我的最终目标是从 Omnibox 中获取焦点并将其放入我自己在 newtab 页面上的输入搜索中。我现在运行良好,我想做的是删除“标签”权限,因为它告诉用户我正在阅读他们的浏览历史记录。这太可怕了。选择加入和可选权限“选项卡”是我想避免的,因为“默认”行为始终为王,并且使用率最高。

我尝试过的:

  • ManifestV2:使用chrome.extension.getViews

    background.js:

    chrome.tabs.onCreated.addListener(tab => {
      const onUpdated = (tabId, info, updatedTab) => {
        if (tabId === tab.id && info.status && chrome.extension.getViews({tabId})[0]) {
          chrome.tabs.onUpdated.removeListener(onUpdated);
          console.log('My newtab', updatedTab);
          // doSomething(tab);
        }
      };
      chrome.tabs.onUpdated.addListener(onUpdated);
    });
    
  • ManifestV3:使用fetch事件:

    background.js:

    let isOpeningMyTab;
    
    self.onfetch = e => {
      // assuming your page is chrome-extension://**id**/newtab.html
      if (e.request.url.startsWith(location.origin + '/newtab.html')) {
        isOpeningMyTab = true;
        setTimeout(() => { isOpeningMyTab = false; });
      }
    };
    
    chrome.tabs.onCreated.addListener(tab => {
      if (isOpeningMyTab) {
        console.log('My newtab', tab);
        // doSomething(tab);
      }
    });