Chrome扩展程序开发:禁用扩展的操作 [英] Chrome Extension Development: Action on extension disable

查看:537
本文介绍了Chrome扩展程序开发:禁用扩展的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



可能的操作:
显示html页面类型我们可以在用户禁用/删除扩展名时使用动作触发器。讨厌看到你走,请检查我们的网站



会这样的工作是为了这个目的吗?

 chrome.browserAction.onClicked.addListener(function(tab){
chrome.tabs.executeScript(null,{file:myScript.js});
} );

与myScript.js保持逻辑在点击执行。



取自此帖子



编辑:也发现禁用或卸载扩展程序时不会触发任何事件

可能onUninstalled事件(来自API文档)不帮我在这里?

解决方案

请参阅文档 browserAction.onClicked不能用于此任务。



由于扩展的唯一部分是禁用/删除它的内容脚本,因此您可以声明一个内容脚本,该脚本定期尝试访问您的扩展,因此失败会指示该扩展已禁用。

这里是一个显示DOM元素的例子所有打开的标签的顶部:

manifest.json:

 content_scripts:[{
matches:[< all_urls>],
run_at:document_start,
all_frames: true,
js:[ping.js]
}]

ping.js:
$ b

  var pingTimer = setInterval(ping,1000); 

函数ping(){
var port = chrome.runtime.connect();
if(port){
port.disconnect();
return;
}
clearInterval(pingTimer);
onDisabled();
}

函数onDisabled(){
document.body.insertAdjacentHTML('beforeend',
'< div style =all:unset; position:fixed ; left:0; top:0; right:0; height:2rem;'+
'background:blue; color:white; font:1rem / 2rem sans-serif; z-index:2147483647;> '+
'我们不想看到你离开,请检查我们的网站:'+
'< a style =all:inherit; color:cyan; display:inline; position:static;' +
'href =http://example.com> http://example.com< / a>< / div>');
}

注意:


  • 这仅适用于允许内容脚本注入的页面。换句话说,除浏览器内置页面之外的所有内容,如基于铬的浏览器中的chrome://设置或基于Firefox的附加内容:扩展/ addon webstore站点和其他扩展的页面。

  • 由于内容脚本无法访问chrome.tabs API,因此打开您网站的新选项卡会出现问题,并且所有其他标准JavaScript方法打开新选项卡将被默认的弹出窗口阻止策略阻止,不过可以由用户手动修改。

  • 在内容脚本中可用的几个chrome API只有 chrome.runtime 显然是一个扩展。

  • 当您重新启用或重新加载扩展时,其内容脚本不会自动重新注入到打开的选项卡中!您需要使用chrome.tabs.query和chrome.tabs.executeScript在后台/活动页面脚本中手动执行此操作。在StackOverflow上搜索示例。


I'm wondering if it's possible to have an action trigger when user disables/removes extension.

Possible actions: Show html page genre "We hate to see you go. Please check our website"

Would something like this work for this purpose ?

 chrome.browserAction.onClicked.addListener(function(tab) {   
     chrome.tabs.executeScript(null, {file: "myScript.js"}); 
});

with "myScript.js" holding the logic to execute on click.

taken from this post

edit : also found "No event will fire in case an extension is disabled or uninstalled"

might the onUninstalled event (from the API docs) not help me out here?

解决方案

As you can see in the documentation browserAction.onClicked cannot be used for this task.

Since the only part of an extension that survives disabling/removing it is a content script, you can declare a content script that periodically tries to access your extension so a failure would indicate the extension is disabled.

Here's an example that displays a DOM element on the top of all open tabs:

manifest.json:

"content_scripts": [{
  "matches": ["<all_urls>"],
  "run_at": "document_start",
  "all_frames": true,
  "js": ["ping.js"]
}]

ping.js:

var pingTimer = setInterval(ping, 1000);

function ping() {
  var port = chrome.runtime.connect();
  if (port) {
    port.disconnect();
    return;
  }
  clearInterval(pingTimer);
  onDisabled();
}

function onDisabled() {
  document.body.insertAdjacentHTML('beforeend',
    '<div style="all:unset; position:fixed; left:0; top:0; right:0; height:2rem;' +
    ' background:blue; color:white; font: 1rem/2rem sans-serif; z-index:2147483647;">' +
    'We hate to see you go. Please check our website: ' +
    '<a style="all:inherit; color:cyan; display:inline; position:static;"' +
    ' href="http://example.com">http://example.com</a></div>');
}

Notes:

  • This will only work on pages that allow content script injection. In other words, all except the browser built-in pages like chrome://settings in chromium-based browsers or about:addons in Firefox-based ones, the extension/addon webstore site, other extensions' pages.
  • Opening a new tab with your site will be problematic as content scripts can't access chrome.tabs API, and all other standard JavaScript methods to open a new tab will be blocked by the default popup-blocking policy, which can be manually altered by a user, though.
  • Of several chrome API available in a content script only chrome.runtime survives disabling an extension, apparently.
  • When you re-enable or reload your extension its content scripts aren't reinjected automatically into the open tabs! You'll need to do it manually in your background/event page script using chrome.tabs.query and chrome.tabs.executeScript. Search for examples on StackOverflow.

这篇关于Chrome扩展程序开发:禁用扩展的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆