简体   繁体   English

每个标签的会话 - Google Chrome 扩展

[英]Session for each tab - Google Chrome Extension

In a Google Chrome Extension, is there a way to have different "sessions" for each tab?在 Google Chrome 扩展程序中,是否可以为每个选项卡设置不同的“会话”?

For exemple, Adblock does that: it shows the number of blocked ads for the current page, and this number changes for each tab (and if you have 2 windows open, a different number for each window...)例如,Adblock 就是这样做的:它显示当前页面被阻止的广告数量,每个标签的数量都会发生变化(如果您打开了 2 个窗口,每个窗口的数量不同......)

For exemple, if I use the Chrome demo to change the icon on click :例如,如果我使用 Chrome 演示更改单击图标:

manifest.json清单文件

{
 "name": "A browser action which changes its icon when clicked",
 "description": "Change browser action color when its icon is clicked",
 "version": "1.2",
 "background": { "scripts": ["background.js"] },
 "browser_action": {
     "name": "Click to change the icon's color"
 },
 "manifest_version": 2
}

background.js背景.js

var min = 1;
var max = 5;
var current = min;

function updateIcon() {
 chrome.browserAction.setIcon({path:"icon" + current + ".png"});
 current++;

 if (current > max)
   current = min;
}

chrome.browserAction.onClicked.addListener(updateIcon);
updateIcon();

Is there a way to add some code to have a different "scope" on each tab?有没有办法添加一些代码以在每个选项卡上具有不同的“范围”? For exemple, if I click 3 times on the first tab, it would show icon3, and if I open a new tab, it would show icon1 for this tab (but still icon3 for the first tab)例如,如果我在第一个选项卡上单击 3 次,它会显示 icon3,如果我打开一个新选项卡,它会显示该选项卡的 icon1(但第一个选项卡仍然是 icon3)

Thanks谢谢

chrome.browserAction API takes tabs into account by providing, and taking, tabId as a parameter. chrome.browserAction API通过提供并采用tabId作为参数来考虑选项卡。

For example, here's the expected signature of an onClicked listener:例如,这是一个onClicked侦听器的预期签名:

The callback parameter should be a function that looks like this:回调参数应该是一个如下所示的函数:

 function( tabs.Tab tab) {...};

tabs.Tab tab tabs.Tab tab

So, in your updateIcon function, you can get the ID of the tab:因此,在您的updateIcon函数中,您可以获得选项卡的 ID:

function updateIcon(tab) {
  // Can use tab.id here
  /* ... */
  chrome.browserAction.setIcon({
    path:"icon" + current + ".png",
    tabId: tab.id // Limit change to tab
  });
}

Of course, this means you have to store your data per-tab.当然,这意味着您必须按标签存储数据。 Simplest way is to make current an Object (or Array, but it'll be sparse) with tabId s acting like keys.最简单的方法是使current对象(或数组,但它会是稀疏的)与tabId s 充当键。

var min = 1;
var max = 5;
var current = {};

function updateIcon(tab) {
  if (!current[tab.id]) { // Not yet defined
    current[tab.id] = min;
  }

  chrome.browserAction.setIcon({
    path: "icon" + current[tab.id] + ".png",
    tabId: tab.id
  });

  if (current[tab.id] === max) {
    current[tab.id] = min;
  } else {
    current[tab.id]++;
  }
}

If you're wary of the tab data growing over time, you can listen to chrome.tabs.onRemoved to clear the data out.如果您担心标签数据随着时间的推移而增长,您可以收听chrome.tabs.onRemoved来清除数据。

You may also want to pay attention to chrome.tabs.onReplaced (which replaces the content within a tab, changing the ID).您可能还需要注意chrome.tabs.onReplaced (它替换选项卡内的内容,更改 ID)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM