通过浏览器操作/图标禁用/启用Chrome扩展程序 [英] Disable / Enable Chrome Extension Via Browser Action / Icon
问题描述
我正在开发的chrome扩展将内容脚本和css插入到网站的每个页面上。但是,用户可能有一个或多个页面,他或她不希望扩展程序运行,所以如果我可以将浏览器操作设置为开/关切换,这将非常棒。
我想要做的是这样的:
chrome.browserAction.onClicked .addListener(function(tab){
//如果ENABLED THEN DISABLE
//如果DISABLED THEN ENABLE
}
任何帮助都将不胜感激!
后台页面:
function disableExtension(disabled)
$ b chrome.windows.getAll({populate:true},function(window_list)
{
for(var i = 0; i< window_list.length; ++ i)
{
var window = window_list [i];
for(var j = 0; j< window.tabs.length; ++ j)
{
var tab = window.tabs [j];
if(checkContentScriptExists(tab))
{
chrome.tabs.executeScript(tab.id,{code:disabled =+ disabled +;},allTabs:true)
}
}
}
//找不到匹配的网址。在新标签中打开它
chrome.tabs.create({url:url,selected:true});
});
}
内容脚本应在运行前检查条件
if(!disabled)doSomething();
二。有争议的方法来保存背景年龄内容中的禁用变量
背景页:
function disableExtension(disabled)
{
global.disabled = disabled;
chrome.runtime.onMessage.addListener(function(request,sender,sendResponse){
if(request.msg ==getDisabled){
sendResponse({disabled :global.disabled});
return true;
}
});
和内容脚本应在执行前查询当前禁用状态
chrome.runtime.sendMessage({msg:getDiasbled},function(response){
if(!response.disabled)doSomething();
});
The chrome extension I am developing inserts content scripts and css onto every page of a website. However, the user may have a certain page or pages he or she does not want the extension to run on, so it would be great if I could set up the browser action as basically a toggle on / off.
What I'd like to do is something like this:
chrome.browserAction.onClicked.addListener(function(tab) {
//IF ENABLED THEN DISABLE
//IF DISABLED THEN ENABLE
}
Any help would be greatly appreciated!
Such API is not provided. But two possible workarounds exists:
I. You can use "disabled" flag viable and update it from your background page.
Background page:
function disableExtension(disabled)
{
chrome.windows.getAll({populate : true}, function (window_list)
{
for (var i = 0; i < window_list.length; ++i)
{
var window = window_list[i];
for (var j = 0; j < window.tabs.length; ++j)
{
var tab = window.tabs[j];
if (checkContentScriptExists(tab))
{
chrome.tabs.executeScript(tab.id, {code : "disabled = " + disabled + ";"}, allTabs: true)
}
}
}
// No matching url found. Open it in the new tab
chrome.tabs.create({ url : url, selected: true });
});
}
And content script should check the condition before run
if (!disabled) doSomething();
II. Controversial approach to save disable variable within background age content
Background page:
function disableExtension(disabled)
{
global.disabled = disabled;
}
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.msg == "getDisabled") {
sendResponse({disabled: global.disabled});
return true;
}
});
and content script should query current disabled status before execution
chrome.runtime.sendMessage({msg: "getDiasbled"}, function(response) {
if (!response.disabled) doSomething();
});
这篇关于通过浏览器操作/图标禁用/启用Chrome扩展程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!