I'm developing a Firefox extension using AngularJS and I'm trying to use some of the AngularJS services inside my main.js file. I use require()
to include the relevant modules and angular.bootstrap()
to initialize the modules.
However, I always get the following error:
Reference Error: window is not defined
Is there anything else I can do about it?
AngularJS isn't really meant for this kind of environment - it is an MVC framework and in your case V (the view) is missing. You don't tell what you want to use AngularJS for but you might want to consider using it in your content scripts rather than main.js
.
That said, AngularJS is often using window
simply as a means to access the global object. This is easy enough to fix, the next-to-last line of AngularJS source code says:
})(window, document);
This should be changed into:
})(this, document);
This will at least allow the functions to proceed that don't really require a view. Of course you would still need to remove the last line (the one attempting to insert a stylesheet into the document) and export the angular
variable instead:
exports.angular = angular;
There isn't much detail so I am unsure if this suggestion fits with what you want to do. If you want to use some JS libraries that require a DOM and don't want to show a document to the user, consider using the page-worker module:
https://addons.mozilla.org/en-US/developers/docs/sdk/latest/packages/addon-kit/page-worker.html
You will need to set up event-driven communications between the worker and main.js, as with any other content document used with SDK code:
https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/guides/events.html
May be you have to get window object via
var window = require("window-utils").activeBrowserWindow;
?
What is the view that you are trying to update? You should be able to provide angular.bootstrap
with some non-root element to start compiling from. http://docs.angularjs.org/api/angular.bootstrap
If you're using some service that relies on window, you also might need to supply your own tweaked $window
service. http://docs.angularjs.org/api/ng.$window