简体   繁体   English

扩展中每个 Chrome 选项卡的唯一 Session

[英]Unique Session Per Chrome Tab in Extension

So I'm very new to JS and this is my first Chrome extension.所以我对 JS 很陌生,这是我的第一个 Chrome 扩展。 It's pretty straight forward.这很简单。 I have a toggle in the extensions bar.我在扩展栏中有一个切换。 When clicked it replaces text in a website.单击时,它会替换网站中的文本。 When clicked again it reverts to the original text.再次单击时,它将恢复为原始文本。 As far as that goes, it works like it should.就这一点而言,它应该像它一样工作。 The issue I'm having is when I open a new tab or window, the state of the toggle isn't reset.我遇到的问题是当我打开一个新选项卡或 window 时,开关的 state 没有重置。 This leads to:这将导致:

Tab 1: toggle is off选项卡 1:切换已关闭

Tab 1: click toggle toggle is on选项卡 1:单击切换切换已打开

Tab 2: opened, web page has original text标签2:打开,web页面有原文

Tab 2: click toggle toggle is switched to off, so nothing happens选项卡 2:单击切换切换已切换为关闭,因此没有任何反应

Tab 2: click toggle toggle is switched to on, finally replaces text选项卡 2:单击切换切换切换到打开,最后替换文本

Tab 2: click toggle toggle is off, text is reverted选项卡 2:单击切换切换已关闭,文本已还原

Tab 1: click toggle toggle is on, text is already on, so nothing happens选项卡 1:单击切换切换已打开,文本已打开,因此没有任何反应

You get the picture.你得到图片。 It's the same if I open a new widow.如果我打开一个新的寡妇,也是一样的。 I need the state of the toggle preserved within one page, but reset between tabs/windows so each tab/window only takes one click to toggle to the next state.我需要将切换的 state 保留在一个页面中,但在选项卡/窗口之间重置,因此每个选项卡/窗口只需单击一下即可切换到下一个 state。 Here's what I have:这是我所拥有的:

manifest.json manifest.json

"manifest_version": 2,
    "name": "Chrome Decolonized",
    "icons": { "16": "icon16.png",
           "48": "icon48.png",
          "128": "icon128.png" },
    "description": "Replaces foreign names for tribes and locations in tribal homelands with the original names used by the indigenous people.",
    "version": "1.0",
     "background":
    {
        "scripts": ["background.js"]
    },
    "permissions": [
      "activeTab"
    ],
    "browser_action": {
          "default_title": "(De)Colonize Toggle" 
      }
}

background.js背景.js


chrome.browserAction.onClicked.addListener(function(tab) {
    if (clickit == 0){
        chrome.tabs.executeScript({file:"content.js"}); //execute for this tab
        clickit++;
        //alert("Clickit was 0 and now is " + clickit);
    }else if (clickit == 1){
        chrome.tabs.executeScript({file:"colonized.js"}); // revert for this tab
        clickit++;
        //alert("Clickit was 1 and now is " + clickit);
    }else{
        chrome.tabs.executeScript({file:"content.js"}); //execute for this tab
        clickit = 1;
        //alert("Clickit was neither and now is " + clickit);
    }

});

Any advice is appreciated.任何建议表示赞赏。 I've searched for answers, but I've not found something really close to this question.我一直在寻找答案,但我还没有找到真正接近这个问题的东西。

You will have to keep status of toggle for each opened tab.您必须为每个打开的选项卡保留切换状态。

background.js

const state = {
  loaded:   {},
  injected: {},
}

const toggleIn = ({id:tab_id}) => {
  // toggle out: it's currently loaded and injected
  if (state.loaded[tab_id] && state.injected[tab_id]) {
    chrome.tabs.executeScript(tab_id, { file: 'content.js' });
    state.injected[tab_id] = false;
  }

  // toggle in: it's loaded and needs injected
  else if (state.loaded[tab_id] && !state.injected[tab_id]) {
    chrome.tabs.executeScript(tab_id, { file: 'colonized.js' })
    state.injected[tab_id] = true;
  }

  // fresh start in tab
  else {
    chrome.tabs.executeScript(tab_id, { file: 'content.js' })
    state.loaded[tab_id]    = true
    state.injected[tab_id]  = true
  }

  chrome.tabs.onUpdated.addListener(function(tabId) {
    if (tabId === tab_id)
      state.loaded[tabId] = false
  })
}


chrome.browserAction.onClicked.addListener(function(tab) {
   toggleIn({id: tab.id});
});

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

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