Chrome扩展程序开发:对扩展程序禁用操作

时间:2017-06-02 12:33:02

标签: google-chrome google-chrome-extension

我想知道当用户禁用/删除扩展时是否可以有动作触发器。

可能的行动: 显示HTML页面类型“我们讨厌看到你去。请查看我们的网站”

这样的事情会用于此目的吗?

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

“myScript.js”持有要在点击时执行的逻辑。

取自this post

编辑:还发现“如果禁用或卸载扩展程序,则不会触发任何事件”

onUninstalled事件(来自API文档)可能无法帮助我吗?

2 个答案:

答案 0 :(得分:2)

正如您在documentation中看到的那样,browserAction.onClicked不能用于此任务。

由于在禁用/删除扩展程序后仍然存在的扩展程序的唯一部分是内容脚本,因此您可以声明定期尝试访问扩展程序的内容脚本,以便失败表明扩展程序已禁用。

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

的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>');
}

注意:

  • 这仅适用于允许内容脚本注入的页面。换句话说,除了基于铬的浏览器中的chrome://设置之类的浏览器内置页面或基于Firefox的插件中的插件,扩展/插件webstore站点,其他扩展&#39;页。
  • 在您的网站上打开新标签会出现问题,因为内容脚本无法访问chrome.tabs API,默认弹出窗口阻止策略会阻止所有其他标准JavaScript方法打开新标签页但是,可以由用户手动更改。
  • 在内容脚本中可用的几个chrome API中,只有chrome.runtime能够在禁用扩展程序时幸存下来。
  • 当您重新启用或重新加载扩展程序时,其内容脚本不会自动重新注入打开的标签页!您需要使用chrome.tabs.query和chrome.tabs.executeScript在后台/事件页面脚本中手动执行此操作。在StackOverflow上搜索示例。

答案 1 :(得分:-1)

您好您也可以使用此

chrome.runtime.setUninstallURL('http://myurl', function callback(id) {
        console.log("successfully uninstall");
    });
}
相关问题