Porting extension from Chrome into FF
Followed this tutorial (which works fine in Chrome): http://www.codingscripts.com/check-whether-user-has-a-chrome-extension-installed/
Sending message from webpage to extension: In (web)pagescript.js this has:
function IsExist(extensionId,callback){
chrome.runtime.sendMessage(extensionId, { message: "installed" },
function (reply) {
if (reply) {
callback(true);
}else{
callback(false);
}
});
}
IsExist("Your extension id",function(installed){
if(!installed){
alert("Please install extension ");
}
});
Receiving message from webpage in extension:
chrome.runtime.onMessageExternal.addListener(
function(req, sender, callback) {
if (req) {
if (req.message) {
if (req.message == "installed") {
callback(true);
}
}
}
return true;
});
What I'm trying to achieve
A couple of html pages on my website need to redirect back to the homepage when the extension is NOT installed. So those pages need to be able to figure out (on their own) if the extension is installed or not.
Error I'm getting when I open webpage
ReferenceError : chrome is not defined. (I also tried with browser.runtime.onMessageExternal but then it throws "browser" is not defined). Is there no way to do this similar to what can be done in Chrome ?
if(typeof myExtensionExists !== 'undefined') {}
. This simplifies your logic, removes the asynchronous call and only requires you to add a very simple content script that runs atdocument_start
on the pages in your website. – Discounterdocument_start
, then your extension code is guaranteed to execute prior to your webpage's scripts. At that point the<body>
and<head>
don't exist (arenull
). When interacting with the DOM at that time you often will usedocument.documentElement
. My description implied a content script:document.documentElement.appendChild(document.createElement('script')).textContent='var myExtensionExists = true;';
You can wait for thebody
to exist, if you want (e.g. using aMutationObserver
; I've done this in a user script to add a<style>
after the<body>
). – Discounterdocument_idle
ordocument_end
makes it indeterminate (in the general case) as to your webpage scripts or your extension executing first, as thoserun_at
declarations don't have guaranteed times (order wrt. page scripts) that they execute (what happens depends on the content of your webpage). In this case, or at least as general advice, my preference would bedocument_start
, as that guarantees the extension's flag will exist when you check for it in your webpage scritps. Obviously, there are other ways to guarantee this, but that just seams easiest to me. – Discounter