Javascript 打开和关闭chrome extension
我正在使用chrome扩展,该扩展在浏览器操作中有两个图标(开和关); 基本上,在后台执行script.js(插入文件:script.js) 使用Javascript 打开和关闭chrome extension,javascript,google-chrome,google-chrome-extension,google-chrome-devtools,Javascript,Google Chrome,Google Chrome Extension,Google Chrome Devtools,我正在使用chrome扩展,该扩展在浏览器操作中有两个图标(开和关); 基本上,在后台执行script.js(插入文件:script.js) 使用chrome.tabs.executeScript(tab.id,{file:“script.js”,function(){}); 我有问题把它关掉! 我曾尝试在background.js和script.js之间使用消息通信,但这两种方式都不起作用。如果我理解正确,您的扩展应该有两种状态,打开和关闭。单击扩展图标可将其打开/关闭 在这种情况下,您应该使
chrome.tabs.executeScript(tab.id,{file:“script.js”,function(){});
我有问题把它关掉!
我曾尝试在background.js和script.js之间使用消息通信,但这两种方式都不起作用。如果我理解正确,您的扩展应该有两种状态,打开和关闭。单击扩展图标可将其打开/关闭 在这种情况下,您应该使用存储,以便扩展知道它处于什么状态。因此,在单击事件中,请使用类似以下内容:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.storage.sync.get('state', function(data) {
if (data.state === 'on') {
chrome.storage.sync.set({state: 'off'});
//do something, removing the script or whatever
} else {
chrome.storage.sync.set({state: 'on'});
//inject your script
}
});
});
请注意,这发生在扩展/浏览器级别,并将应用于所有选项卡,因此您可能需要更复杂的内容来记录选项卡ID和状态
然后,您可以选择在执行某些操作之前始终运行内容脚本并检查打开/关闭状态,或者插入并删除脚本。但我不确定是否删除脚本。根据脚本的功能,您可能只想刷新页面(即,如果您的脚本与DOM发生冲突,并且您希望在关闭扩展时撤消该操作)。如果我理解正确,您的扩展应该有两种状态,打开和关闭。单击扩展图标可将其打开/关闭 在这种情况下,您应该使用存储,以便扩展知道它处于什么状态。因此,在单击事件中,请使用类似以下内容:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.storage.sync.get('state', function(data) {
if (data.state === 'on') {
chrome.storage.sync.set({state: 'off'});
//do something, removing the script or whatever
} else {
chrome.storage.sync.set({state: 'on'});
//inject your script
}
});
});
请注意,这发生在扩展/浏览器级别,并将应用于所有选项卡,因此您可能需要更复杂的内容来记录选项卡ID和状态
然后,您可以选择在执行某些操作之前始终运行内容脚本并检查打开/关闭状态,或者插入并删除脚本。但我不确定是否删除脚本。根据脚本的功能,您可能只想刷新页面(即,如果您的脚本与DOM发生冲突,并且您希望在关闭扩展时撤消该操作)。background.js
var enable=false;
chrome.browserAction.onClicked.addListener(function (tab) {
enable = enable ? false : true;
if(enable){
//turn on...
chrome.browserAction.setIcon({ path: 'icon.png' });
chrome.browserAction.setBadgeText({ text: 'ON' });
chrome.tabs.executeScript(null, { file: 'content.js' });
}else{
//turn off...
chrome.browserAction.setIcon({ path: 'disable.png'});
chrome.browserAction.setBadgeText({ text: '' });
}
});
background.js
var enable=false;
chrome.browserAction.onClicked.addListener(function (tab) {
enable = enable ? false : true;
if(enable){
//turn on...
chrome.browserAction.setIcon({ path: 'icon.png' });
chrome.browserAction.setBadgeText({ text: 'ON' });
chrome.tabs.executeScript(null, { file: 'content.js' });
}else{
//turn off...
chrome.browserAction.setIcon({ path: 'disable.png'});
chrome.browserAction.setBadgeText({ text: '' });
}
});
为了补充@david gilbertson所说的使某些选项卡处于活动和非活动状态的功能,我在这里创建了该功能。我还添加了一些用于删除和向阵列中添加选项卡的功能。享受吧
function addTab(array, new_tab_id)
{
array.push(new_tab_id);
//then call the set to update with modified value
chrome.storage.sync.set({
active_tabs:array
}, function() {
console.log("added tab");
});
}
function removeTab(array, rem_tab_id)
{
const index = array.indexOf(rem_tab_id);
if (index > -1) {
array.splice(index, 1);
}
//then call the set to update with modified value
chrome.storage.sync.set({
active_tabs:array
}, function() {
console.log("removed tab");
});
}
chrome.browserAction.onClicked.addListener(function (tab) {`enter code here`
chrome.storage.sync.get({active_tabs : []}, function(data) {
if (data.active_tabs.includes(request.tab_id)) {
removeTab(data.active_tabs, request.tab_id)
console.log("Turned Off ".concat(request.tab_id))
document.removeEventListener("mousemove", highlightCurrentHover, false);
} else {
addTab(data.active_tabs, request.tab_id)
console.log("Turned On ".concat(request.tab_id))
document.addEventListener('mousemove', highlightCurrentHover, false);
}
});
);
为了补充@david gilbertson所说的使某些选项卡处于活动和非活动状态的功能,我在这里创建了该功能。我还添加了一些用于删除和向阵列中添加选项卡的功能。享受吧
function addTab(array, new_tab_id)
{
array.push(new_tab_id);
//then call the set to update with modified value
chrome.storage.sync.set({
active_tabs:array
}, function() {
console.log("added tab");
});
}
function removeTab(array, rem_tab_id)
{
const index = array.indexOf(rem_tab_id);
if (index > -1) {
array.splice(index, 1);
}
//then call the set to update with modified value
chrome.storage.sync.set({
active_tabs:array
}, function() {
console.log("removed tab");
});
}
chrome.browserAction.onClicked.addListener(function (tab) {`enter code here`
chrome.storage.sync.get({active_tabs : []}, function(data) {
if (data.active_tabs.includes(request.tab_id)) {
removeTab(data.active_tabs, request.tab_id)
console.log("Turned Off ".concat(request.tab_id))
document.removeEventListener("mousemove", highlightCurrentHover, false);
} else {
addTab(data.active_tabs, request.tab_id)
console.log("Turned On ".concat(request.tab_id))
document.addEventListener('mousemove', highlightCurrentHover, false);
}
});
);
打开和关闭扩展是什么意思?是否希望该按钮完全禁用扩展?不能这样做并显示“打开”按钮--禁用扩展将禁用扩展,包括“打开”按钮。关闭时,脚本将无法与选项卡交互。您需要提供更多信息。“我遇到问题”这还不够--人们需要查看一些代码,您需要解释您尝试过的内容,否则他们帮不了您。与后台页面的通信确实有效。您为什么还需要后台?如果需要使用本地存储,请使用chrome.storage,而不需要从后台执行。@user2997816:向我们展示了您所拥有的功能ied和它是如何失败的/问题是什么。发布一些代码。打开和关闭它是什么意思?你想让按钮完全禁用扩展吗?你不能这样做并让“开”按钮出现--禁用扩展会禁用扩展,包括“开”按钮。关闭时,脚本将无法与选项卡交互。您需要提供更多信息。“我遇到问题”这还不够--人们需要查看一些代码,您需要解释您尝试过的内容,否则他们帮不了您。与后台页面的通信确实有效。您为什么还需要后台?如果需要使用本地存储,请使用chrome.storage,而不需要从后台执行。@user2997816:向我们展示了您所拥有的功能ied和它如何失败/问题是什么。发布一些代码。公平警告:使用
persistent:false
event pages时将失败空气警告:使用persistent:false
event pages时将失败