Last active
August 6, 2018 14:37
-
-
Save kentbrew/15cd10df35f482c599efe3219d4dcd40 to your computer and use it in GitHub Desktop.
How to tell if visitors to your site are running a WebExtensions-compatible extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If we know the extension's ID and a path to one of its web-accessible resources, we can quietly check for presence. | |
(function (w, a) { | |
var checkExt = function (callback) { | |
var hazExt = false; | |
var img = new Image(); | |
img.onload = function () { | |
hazExt = true; | |
}; | |
// need to know the right protocol for Firefox and Edge extensions | |
img.src = 'chrome-extension://' + a.id + '/' + a.path; | |
w.setTimeout(function () { | |
callback(hazExt); | |
}, 100); | |
}; | |
var browser = w.chrome; | |
if (typeof browser === 'object') { | |
checkExt(function (r) { | |
// careful, now: for Chrome this will report true whether or not the extension is disabled | |
console.log('Using extension ' + a.id + '? ' + r); | |
}); | |
} else { | |
console.log('Not using a compatible browser.'); | |
} | |
}(window, { | |
'id': 'your_chrome_extension_id_goes_here', | |
'path': 'img/icon_48.png' | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment