JavaScript check if browser extension is installed for Chrome, Firefox and Opera

て烟熏妆下的殇ゞ 提交于 2019-12-04 11:57:50

问题



I want to show custom bar ( notification like: Install our plugin. ) on our site if extension is not installed for Chrome, Firefox and Opera. None will be displayed if extension is installed.

The idea is:
1. User visits our site and a bar with message appears - "Hey, install our extensions".
2. User clicks on install and extension is installed. No need for the user to open it.
3. User visit our site again and the bar does not appear again as the extension is installed.
We talk only for installation check, not cookies.

So far I was able to find solutions only for Chrome: http://blog.kotowicz.net/2012/02/intro-to-chrome-addons-hacking.html
and Firefox: http://webdevwonders.com/detecting-firefox-add-ons/
Opera is never mentioned.

Is there a way to do it for Opera, based on the idea of checking for a local resource which is part of the extension:
chrome-extension://--Extension ID--/blank.gif
Firefox chrome://firebug/content/blank.gif

Or else: Is there a simple way to check if extension is installed for Chrome, Firefox and Opera?


回答1:


If the extension is willing to cooperate, it could advertise its presence to the document easily. For example:


The extension could do

window.$$myExt = ...

Then you can detect the extension by

if(typeOf $$myExt !== 'undefined'){...

(or any variation thereof)

Obtaining the page window is somewhat tricky at least


The extension could do

document.body.classList.add("myExt-visited")

Then you could detect the extension by

if(document.body.classList.contains("myExt-visited")){...

The extension could do

document.body.innerHTML += "<div id='myExt-toolbar'>..."
// or $('body').append("<div id='myExt-toolbar'>...");

then you could detect the extension by

if(document.getElementByID("myExt-toolbar")){...
// or if($("#myExt-toolbar").length){...

alternatively, you could do

<div id="myExt-replacement">
   ...

and the extension would do

var replacement = document.getElementByID("myExt-replacement");
replacement && replacement.remove();

or you could do

function onMyExtExists(){
  ...
}

and the extension would do

onMyExtExists && onMyExtExists();


来源:https://stackoverflow.com/questions/14398548/javascript-check-if-browser-extension-is-installed-for-chrome-firefox-and-opera

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!