Skip to content

Instantly share code, notes, and snippets.

@lkuper
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lkuper/c4af5eefd1841543168b to your computer and use it in GitHub Desktop.
Save lkuper/c4af5eefd1841543168b to your computer and use it in GitHub Desktop.
Components.utils.import('resource://gre/modules/Services.jsm');
function myObserver()
{
this.register();
}
myObserver.prototype = {
observe: function(subject, topic, data) {
console.log("observed: " + subject);
let o = {};
Services.scriptloader.loadSubScript("chrome://test-extension/content/test-extension.js",
o);
var window = Components.utils.waiveXrays(subject);
console.log("window: " + window);
Components.utils.exportFunction(o.foo, window);
},
register: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
observerService.addObserver(this, "content-document-global-created", false);
},
unregister: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
observerService.removeObserver(this, "content-document-global-created");
}
}
function load(win) {
console.log("load() is running...");
observer = new myObserver();
}
var listener = {
onOpenWindow: function(aWindow) {
// Wait for the window to finish loading
let win = aWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIDOMWindowInternal);
win.addEventListener("UIReady", function(aEvent) {
win.removeEventListener(aEvent.name, arguments.callee, false);
load(win);
}, false);
},
// Unused:
onCloseWindow: function(aWindow) { },
onWindowTitleChange: function(aWindow, aTitle) { }
};
function startup(data, reason) {
console.log("startup() is running...");
// Load in existing windows.
let enumerator = Services.wm.getEnumerator("navigator:browser");
while(enumerator.hasMoreElements()) {
let win = enumerator.getNext();
load(win);
}
// Load in future windows.
Services.wm.addListener(listener);
}
function shutdown(data, reason) {
}
function install(data, reason) {
}
function uninstall(data, reason) {
}
function foo() {
console.log("foo() is running!");
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Extension test page</title>
<script type="text/javascript">
var bar = foo();
</script>
</head>
<body>
<div>...</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment