I keep returning to this problem, and keep on failing to solve it.
I've read tens of pages describing how to do it, and looked at answers such as How can I make my Firefox extension toolbar button appear automatically? on here, but I just can't get this right.
I understand the concept of what I need to do, but the point I have reached is that, while the extension itself works correctly, I cannot get the init(
) function to run and the load listener doesn't seem to be being added either (I have added alert
s before and after the window.top.addEventListener..
code but the alert
s aren't firing either.
What I want to achieve is the recommended process of adding the toolbar button on install / update but only on first run/install.
My extension consists of:
chrome.manifest
andinstall.rdf
in the extension root directory.- a
chrome
folder which contains the other elements (extensionName.js
,extensionName.png
,extensionName.xul
,extensionName.css
andicon.png
). prefs.js
in adefaults/preferences
directory. This contains theextensions.MyExtensionNameButton.firstRunDone
pref set tofalse
.
The extension itself works exactly as required (it includes a toolbar button and right-click context menu items).
My extensionName.js
file contains the following (with the actual extension functions trimmed out as they are working and would make the question too long):
objMyExtensionNameButton = {
// functions that are called by the .xul are in here,
init : function() {
var firstRunPref = "extensions.MyExtensionNameButton.firstRunDone";
alert("init function run");
if (!Application.prefs.getValue(firstRunPref)) {
Application.prefs.setValue(firstRunPref, true);
// all the rest of the first run code goes here.
alert("this is the first run");
} else {
alert("this is NOT the first run");
}
}
};
window.top.addEventListener("load", function() { objMyExtensionNameButton.init(); }, false);
The relevant part of the xul that includes the .js file is shown below:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css"
href="chrome://MyExtensionNameButton/content/button.css"?>
<!DOCTYPE overlay >
<overlay id="MyExtensionNameButton-overlay"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/javascript"
src="chrome://MyExtensionNameButton/content/MyExtensionNameButton.js"/>
<!-- Firefox -->
<toolbarpalette id="BrowserToolbarPalette">
<toolbarbutton id="MyExtensionNameButton"/>
</toolbarpalette>
<!-- button details -->
<toolbarbutton id="MyExtensionNameButton"
...etc etc etc...
I cannot seem to get anything to run when the extension is installed or when Firefox starts - clicking on extension options in the toolbar button or the right click menu runs those functions without any problem - those functions are all included in an object.
I've tried moving the init()
function both inside and outside of that object and, I believe, calling it correctly.
The other odd thing is that I don't seem to see any errors from the extension in the console (I wasn't seeing any errors when I hadn't added the default/preferences/prefs.js
file to the .xpi
either, so something seems amiss there as well).
Thanks,
FM
In your default
prefs.js
, you should addSince the pref
extensions.MyExtensionNameButton.firstRunDone
does not exist, the call toApplication.prefs.getValue(firstRunPref)
should have been failing.If you open up the error console, Ctrl+Shift+j, you should see the related error.