[英]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
运行任何代码。
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.