Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my init() function not being run? (Automatically adding a toolbar button when installing a Firefox extension but only on first run)
    primarykey
    data
    text
    <p>I keep returning to this problem, and keep on failing to solve it.</p> <p>I've read tens of pages describing how to do it, and looked at answers such as <a href="https://stackoverflow.com/questions/579656/how-can-i-make-my-firefox-extension-toolbar-button-appear-automatically">How can I make my Firefox extension toolbar button appear automatically?</a> on here, but I just can't get this right.</p> <p>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 <code>init(</code>) function to run and the load listener doesn't seem to be being added either (I have added <code>alert</code>s before and after the <code>window.top.addEventListener..</code> code but the <code>alert</code>s aren't firing either.</p> <p>What I want to achieve is the recommended process of adding the toolbar button on install / update but only on first run/install.</p> <p>My extension consists of:</p> <ul> <li><code>chrome.manifest</code> and <code>install.rdf</code> in the extension root directory.</li> <li>a <code>chrome</code> folder which contains the other elements (<code>extensionName.js</code>, <code>extensionName.png</code>, <code>extensionName.xul</code>, <code>extensionName.css</code> and <code>icon.png</code>).</li> <li><code>prefs.js</code> in a <code>defaults/preferences</code> directory. This contains the <code>extensions.MyExtensionNameButton.firstRunDone</code> pref set to <code>false</code>.</li> </ul> <p>The extension itself works exactly as required (it includes a toolbar button and right-click context menu items).</p> <p>My <code>extensionName.js</code> file contains the following (with the actual extension functions trimmed out as they are working and would make the question too long):</p> <pre><code>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); </code></pre> <p>The relevant part of the xul that includes the .js file is shown below:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;?xml-stylesheet type="text/css" href="chrome://MyExtensionNameButton/content/button.css"?&gt; &lt;!DOCTYPE overlay &gt; &lt;overlay id="MyExtensionNameButton-overlay" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"&gt; &lt;script type="application/javascript" src="chrome://MyExtensionNameButton/content/MyExtensionNameButton.js"/&gt; &lt;!-- Firefox --&gt; &lt;toolbarpalette id="BrowserToolbarPalette"&gt; &lt;toolbarbutton id="MyExtensionNameButton"/&gt; &lt;/toolbarpalette&gt; &lt;!-- button details --&gt; &lt;toolbarbutton id="MyExtensionNameButton" ...etc etc etc... </code></pre> <p>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.</p> <p>I've tried moving the <code>init()</code> function both inside and outside of that object and, I believe, calling it correctly.</p> <p>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 <code>default/preferences/prefs.js</code> file to the <code>.xpi</code> either, so something seems amiss there as well).</p> <p>Thanks,</p> <p>FM</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload