From reading examples, this seems like it should be quite easy. Here's my code:
rhkTest = {
onPageLoad: function(event) {
var doc = event.originalTarget;
var wnd = doc.defaultView;
var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
.getService(Components.interfaces.mozIJSSubScriptLoader);
loader.loadSubScript("chrome://rhkoshi-extension/content/testfoo.js", wnd);
alert('typeof(wnd) = ' + typeof(wnd));
alert('typeof(wnd.window.rhk_test1) = ' + typeof(wnd.window.rhk_test1));
alert('typeof(wnd.window.rhk_test2) = ' + typeof(wnd.window.rhk_test2));
alert('typeof(wnd.window.rhk_test3) = ' + typeof(wnd.window.rhk_test3));
alert('typeof(wnd.rhk_test1) = ' + typeof(wnd.rhk_test1));
alert('typeof(wnd.rhk_test2) = ' + typeof(wnd.rhk_test2));
alert('typeof(wnd.rhk_test3) = ' + typeof(wnd.rhk_test3));
alert('typeof(rhk_test1) = ' + typeof(rhk_test1));
alert('typeof(rhk_test2) = ' + typeof(rhk_test2));
alert('typeof(rhk_test3) = ' + typeof(rhk_test3));
alert('typeof(this.rhk_test1) = ' + typeof(this.rhk_test1));
alert('typeof(this.rhk_test2) = ' + typeof(this.rhk_test2));
alert('typeof(this.rhk_test3) = ' + typeof(this.rhk_test3));
alert('typeof(window.rhk_test1) = ' + typeof(window.rhk_test1));
alert('typeof(window.rhk_test2) = ' + typeof(window.rhk_test2));
alert('typeof(window.rhk_test3) = ' + typeof(window.rhk_test3));
},
onLoad: function(event) {
var appcontent = document.getElementById("appcontent");
if (appcontent) {
appcontent.addEventListener("DOMContentLoaded", rhkTest.onPageLoad, true);
}
},
};
window.addEventListener("load", function(e) { rhkTest.onLoad(e); }, false);
where testfoo.js contains:
window.rhk_test1 = 'testval1';
rhk_test2 = 'testval2';
var rhk_test3 = 'testval3';
window.alert('Running testfoo.js');
I get the alert "Running testfoo.js", so the file is found and executed. I also get an alert noting that wnd is an "object" (as expected -- it's initialized elsewhere). However, all the other alerts show "undefined" for the various typeof() calls. Naturally, I don't expect all of these to have values, but I was hoping that at least one of them might show something.
What happened to my values? Aren't they supposed to be in properties of wnd?
I'm running Firefox 19.0 on Windows 7 (if that matters).