Chrome扩展:在发布时停止加载页面 [英] chrome extension : Stop loading the page on launch
问题描述
当我点击扩展图标时,我想停止页面加载。我有一个背景页面,当我单击扩展名Icon时,我需要一个类似于window.stop()的选项。如果主页面仍处于加载状态,请停止加载并加载扩展JavaScript的内容。
这需要主机权限或 activeTab
) :
chrome.browserAction.onClicked.addListener(function(tab){
chrome.tabs.executeScript(tab。 id,{
code:window.stop();,
runAt:document_start
});
});
我不确定清单定义的内容脚本会发生什么情况。用 run_at
而不是 document_start
定义的脚本可能不会被注入;在这种情况下,您可以使用 executeScript
的回调函数在任何情况下注入它们,并在内容脚本中放入一些守护程序代码以防止其执行两次:
// content.js
if(contentInjected)return;
var contentInjected = true;
/ *其余代码* /
I would like to stop the page loading when I click the extension icon. I have a background page , I need an option similar to window.stop() when I click the extension Icon. If the main page is still in loading condition stop loading and load the content JavaScript of extension should work.
You can always do this (requires host permissions or activeTab
):
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(tab.id, {
code: "window.stop();",
runAt: "document_start"
});
});
I am not sure what happens with manifest-defined content scripts. It's possible that scripts defined with run_at
other than document_start
will not be injected; in this case you can use executeScript
's callback to inject them in any case, and put in some guard code in the content script to prevent it from executing twice:
// content.js
if(contentInjected) return;
var contentInjected = true;
/* rest of the code */
这篇关于Chrome扩展:在发布时停止加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!