javascript 如何在选项卡重新加载时运行此脚本(chrome 扩展)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16949810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:34:43  来源:igfitidea点击:

How can I run this script when the tab reloads (chrome extension)?

javascriptgoogle-chrome-extensionmanifestreloadonupdate

提问by András Geiszl

So i'd like to run a script when the tab reloads in a specified URL. It almost works, but actually id doesn't :) This is my manifest file:

所以我想在指定的 URL 中重新加载选项卡时运行脚本。它几乎可以工作,但实际上 id 没有 :) 这是我的清单文件:

{
"manifest_version": 2,

"name": "Sample Extension",
"description": "Sample Chrome Extension",
"version": "1.0",

"content_scripts":
[
    {
      "matches": ["http://translate.google.hu/*"],
      "js": ["run.js"]
    }
],

"permissions":
[
    "activeTab",
    "tabs"
],

"browser_action":
{
    "default_title": "Sample",
    "default_icon": "icon.png"
}
}

and this is run.js:

这是 run.js:

chrome.tabs.onUpdated.addListener(
function ( tabId, changeInfo, tab )
{
    if ( changeInfo.status === "complete" )
    {
        chrome.tabs.executeScript( null, {file: "program.js"} );
    }
}
);

The programs.js just alerts some text (yet). When I put an alert to the first line of the run.js, it alerts, but when I put it in the if, it doesn't. I can't find the problem. Did I type something wrong?

program.js 只是提醒一些文本(还)。当我在 run.js 的第一行发出警报时,它会发出警报,但是当我将它放入 if 时,它不会。我找不到问题。我输入错了吗?

回答by BeardFist

Assuming that http://translate.google.hu/*pages are the ones you wish to inject code into on reload, you would have to go about it in a slightly different way. Currently you are alwaysinjecting code into those pages (without the permission to do so, no less) and then trying to use the chrome.tabsapi inside that content script, which you can't do. Instead, we will put the listener in a background page and inject the code only on a page refresh, like you want. First the manifest:

假设http://translate.google.hu/*页面是您希望在重新加载时向其中注入代码的页面,您将不得不以稍微不同的方式进行处理。目前,您总是将代码注入这些页面(未经许可,不少于),然后尝试chrome.tabs在该内容脚本中使用api,这是您无法做到的。相反,我们会将侦听器放在后台页面中,并仅在页面刷新时注入代码,就像您想要的那样。首先是清单

{
  "manifest_version": 2,
  "name": "Sample Extension",
  "description": "Sample Chrome Extension",
  "version": "1.0",
  "background": {
    "scripts": ["background.js"]
  },
  "permissions":[
    "http://translate.google.hu/*", "tabs"
  ]
}

background.js

背景.js

chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab){
  if (tab.url.indexOf("http://translate.google.hu/") > -1 && 
      changeInfo.url === undefined){
    chrome.tabs.executeScript(tabId, {file: "program.js"} );
  }
});

This will listen for the onUpdatedevent, checks if it is one of the url's that we want to inject into, and then it checks if the page was reloaded. That last step is accomplished by checking if changeInfo.urlexists. If it does, then that means that the url was changed and thus not a refresh. Conversely, if it doesn't exist, then the page must have only been refreshed.

这将侦听onUpdated事件,检查它是否是url我们想要注入的's之一,然后检查页面是否已重新加载。最后一步是通过检查是否changeInfo.url存在来完成的。如果是这样,则意味着 url 已更改,因此不是刷新。相反,如果它不存在,那么页面必须只是被刷新了。

回答by fregante

content_scriptsare run at every page (re)load, so it's best to just use those to detect it.

content_scripts在每个页面(重新)加载时运行,所以最好只使用它们来检测它。

This way you also don't risk running any code in the backgroundbefore your content_scriptis ready to receive any message.

这样,backgroundcontent_script准备好接收任何消息之前,您也不会冒险运行任何代码。