Variable is not defined error in my Firefox extension

917 views Asked by At

I'm developing a Firefox add-on. When I run it, I open up the browser console and it says, AMO_Uedit_Beta_Firefox is not defined :browser.xul.

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://Uedit/skin/skin.css" type="text/css"?>
<!DOCTYPE Uedit SYSTEM "chrome://Uedit/locale/translations.dtd">
<overlay id="Uedit-overlay" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    <script src="Uedit.js" />
    <toolbarpalette id="BrowserToolbarPalette">
        <toolbarbutton id="Uedit" class="toolbarbutton-1" label="Edit HTML" tooltiptext="Edit HTML" oncommand="AMO_Uedit_Beta_Firefox.Uedit()" />
    </toolbarpalette>
</overlay>

The toolbar button that calls a function which is part of an object (AMO_Uedit_Beta_Firefox).

I double-checked the names and they both match. Is it because the script doesn't load properly? I'm sure it's not that variable names can't start with capital letters.

var AMO_Uedit_Beta_Firefox={ // This is for "wrapping the loose variables."

Both the file's references are the exact same. Could it be because the script didn't load at all?

<toolbarbutton id="Uedit" class="toolbarbutton-1" label="Edit HTML" tooltiptext="Edit HTML" oncommand="AMO_Uedit_Beta_Firefox.Uedit()" />

I tried changing the relative URL (<script src="Uedit.js" />) to an absolute URL (<script src="chrome://Uedit/Uedit.js" />) in the browser.xul, but now it just returns a blank error message.

enter image description here Weird blank error message.

These errors cause the rest of the add-on to not work at all, so I can't continue developing it until this is fixed. What're some possible solutions?

EDIT:

I figured out a solution. I have to put a line of JavaScript before the first statement.

var AMO_Uedit_Beta_Firefox = { // Will not work!
    ...

If I put a console.log in the front, for example.

console.log("");
var AMO_Uedit_Beta_Firefox = { // This will work!
    ...

The only question is, why does this work?

3

There are 3 answers

0
clickbait On BEST ANSWER

I figured out a solution. I have to put a line of JavaScript before the first statement.

var AMO_Uedit_Beta_Firefox = { // Will not work!
    ...

If I put a console.log in the front, for example.

console.log("foo bar");
var AMO_Uedit_Beta_Firefox = { // This will work!
    ...

I solved the problem myself, although the solution is strange.

1
erikvold On

It looks like the path to the Uedit.js file is wrong. You should use an absolute path in your xul overlay.

4
Makyen On

As erikvold has said, the link to your Uedit.js script is wrong. This is the minimum that is wrong and is the thing that is complained about in the console: browser.xul:5 is line 5 in browser.xul, which is:

<script src="Uedit.js" />

You state that you have tried:

<script src="chrome://Uedit/Uedit.js" />

That will not work. At a minimum, it should be something like:

<script src="chrome://Uedit/content/Uedit.js" type="application/x-javascript" />

Note the content/ after //Uedit/. However, that being correct assumes you have set up your chrome.manifest with an appropriate content line in addition to the other ones implied by your code (skin and locale). It also assumes that Uedit.js is in that directory. Assuming that the directory structure of your add-on is normal, the content line in your chrome.manifest file would look something like:

content        Uedit        chrome/content/

As to it actually working, there is no way for us to know if it will work as you have not included the source code that defines all of AMO_Uedit_Beta_Firefox and specifically not AMO_Uedit_Beta_Firefox.Uedit(). For instance, in addition to the above problem, there could easily be a syntax error that prevents the script from loading. Such a syntax error would cause the console to report that AMO_Uedit_Beta_Firefox was undefined when you attempt to execute AMO_Uedit_Beta_Firefox.Uedit() as the code for the toolbarbutton's oncommand.

You can easily test to see if your Uedit.js script is loading by having something print in the console when the script loads (i.e. outside the definition of AMO_Uedit_Beta_Firefox).