0

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?

Wayfarer
  • 620
  • 6
  • 16

2 Answers2

4

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.
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • What an awesome elaborate (and quick) response to a noob question. Tnx so much for taking this seriously. Dumbing this down for myself: 1) when a user removes the extension, the only thing I can do is inject a block of html into (all) tabs that are already open. I can't trigger a tab (with my site) to be opened. 2) If user closes down Chrome and starts up again will the content script still be there/will it survive? I guess not. So the "visit our site" message will only be visible during the initial browser session, correct? Next browser session will have nothing left of my extension. – Wayfarer Jun 02 '17 at 13:22
  • let me know if you don't have anything to do this weekend :) I have some work I'd be able to have commissioned. – Wayfarer Jun 02 '17 at 13:32
  • 1
    Id also add to this answer that you can set an uninstall url to open when the user uninstalls the extension. – Zig Mandel Jun 02 '17 at 13:55
  • so above 1) IS possible ? I CAN in fact open a tab with my site to be opened when user removes extension ? Apparently this is supported since v41 of Chrome – Wayfarer Jun 02 '17 at 16:52
-3

Hi You can also use this

chrome.runtime.setUninstallURL('http://myurl', function callback(id) {
        console.log("successfully uninstall");
    });
}
  • chrome.runtime.setUninstallURL runs right away and callback is not going to execute when you uninstall your extension - Please refer - https://stackoverflow.com/questions/47077685/perform-activity-before-chrome-extension-is-getting-uninstalled – Sandeep Chikhale Mar 14 '18 at 08:00