简体   繁体   English

当标签重载(Chrome扩展名)时如何运行此脚本?

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

So i'd like to run a script when the tab reloads in a specified URL.所以我想在指定的 URL 中重新加载选项卡时运行脚本。 It almost works, but actually id doesn't :) This is my manifest file:它几乎可以工作,但实际上 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). program.js 只是提醒一些文本(还)。 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.当我在 run.js 的第一行放置警报时,它会发出警报,但是当我将它放入 if 时,它不会。 I can't find the problem.我找不到问题。 Did I type something wrong?我输入错了吗?

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.假设http://translate.google.hu/*页面是您希望在重新加载时注入代码的页面,您将不得不以稍微不同的方式进行处理。 Currently you are always injecting code into those pages (without the permission to do so, no less) and then trying to use the chrome.tabs api inside that content script, which you can't do.目前,您总是将代码注入这些页面(未经许可,不少于),然后尝试在该内容脚本中使用chrome.tabs api,这是您无法做到的。 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 :首先是清单

{
  "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 onUpdated event, checks if it is one of the url 's that we want to inject into, and then it checks if the page was reloaded.这将侦听onUpdated事件,检查它是否是我们要注入的url之一,然后检查页面是否已重新加载。 That last step is accomplished by checking if changeInfo.url exists.最后一步是通过检查changeInfo.url存在来完成的。 If it does, then that means that the url was changed and thus not a refresh.如果是这样,则意味着 url 已更改,因此不是刷新。 Conversely, if it doesn't exist, then the page must have only been refreshed.相反,如果它不存在,那么页面必须只是被刷新了。

content_scripts are 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 background before your content_script is ready to receive any message.这样,在content_script准备好接收任何消息之前,您也不会冒险在background运行任何代码。

2021 2021年

If you want to detect reload from background.js in manifest 3 (maybe also 2), chrome.tabs.onUpdated approach didn't work for me :/ It was invoked too many times.如果您想检测清单 3(也可能是 2)中 background.js 的重新加载,chrome.tabs.onUpdated 方法对我不起作用:/它被调用了太多次。

That what worked for me in the end!这最终对我有用!

// --- On Reloading or Entering example.com --- 
chrome.webNavigation.onCommitted.addListener((details) => {
    if (["reload", "link", "typed", "generated"].includes(details.transitionType) &&
        details.url === "http://example.com/") {

        codeAfterReload();

        // If you want to run only when the reload finished (at least the DOM was loaded)
        chrome.webNavigation.onCompleted.addListener(function onComplete() {

            codeAfterReloadAndFinishSomeLoading();

            chrome.webNavigation.onCompleted.removeListener(onComplete);
        });
    }
});

For more transition types: https://developer.chrome.com/docs/extensions/reference/history/#transition_types更多过渡类型: https : //developer.chrome.com/docs/extensions/reference/history/#transition_types

good luck :)祝你好运 :)

暂无
暂无

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

相关问题 如何在 chrome 扩展中动态运行后台脚本? - How can I run background script dynamically in chrome extension? 在开发chrome扩展时,如何打开空白页面来运行脚本? - When developing a chrome extension how can I open a blank page to run a script on? 如何为每个标签页单独运行Chrome扩展程序功能? - How can Chrome extension functionality be run independently for each tab? Chrome 扩展程序:从 Background.js 注入到选项卡的 Javascript 即使重新加载选项卡也会继续运行。 如何使注入的 javascript 只运行一次? - Chrome Extension: Javascript injected from Background.js to tab keeps running even if tab reloads. How to make injected javascript run only once? 移至其他标签页后,如何停止我的Chrome扩展程序? - How can I get my Chrome extension to stop when i move to another tab? 如何在Chrome扩展程序中获取标签通知,以便每次新通知到达时,扩展程序都可以显示桌面提醒 - How can I get tab notifications in chrome extension, so that the extension can show desktop alert each time when new notification arrives 页面动态加载Chrome扩展程序内容时如何运行脚本 - How to run script when page loads content dynamically for Chrome Extension 如何验证Chrome扩展程序中的标签页ID? - How can I validate a tab id in my Chrome Extension? 我怎样才能打开带有 chrome 扩展的新标签页? - How can i open the new tab with chrome extension? 使用Chrome扩展程序加载页面时如何运行脚本 - How to run a script when a page loads using Chrome extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM