I'm writing an extension for enterprise environments that needs to detect if a user ever disables it. I know that you can detect the disabling of other extensions with chrome.management.onDisabled
, but how does an extension detect when itself is disabled?
I was able to find an answer which uses chrome.runtime
from a content script, but that's impossible if the user does not have any http/https websites open.
This is my current solution, where I use chrome.history
to check if the extension was ever disabled:
chrome.history.onVisited.addListener((site) => {
localStorage.lastActive = site.lastVisitTime;
});
if (!localStorage.lastActive) return;
chrome.history.search({text: "", maxResults: 1}, (data) => {
let wasDisabled = data[0].lastVisitTime > localStorage.lastActive;
console.log(wasDisabled);
// if true, there was a history visit when the extension was turned off
localStorage.lastActive = data[0].lastVisitTime;
})
Basically, the extension constantly checks if it was active the last time the user was browsing. Once it is turned on again, it will take action and realize whether it had been inactive.
However, I feel like this is just too hacky and inconsistent. If a user can clear their browser history, they can fool the extension.
I know that the Chrome API is vast and sometimes weird, so if anybody has knowledge of how you would detect this in a more consistent way, that would be highly appreciated. If having two separate extensions is truly the cleanest and most reliable answer, then I will do that.