Skip to content

Commit

Permalink
Use an observer to power document-start injection.
Browse files Browse the repository at this point in the history
Fixes #1565
Refs #1384 (GM_addStyle works at document-start now!)
Refs #1519 (Scripts at document-start run in iframes too now!)
  • Loading branch information
arantius committed Jun 22, 2012
1 parent 24f27c3 commit 8bc16d5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
15 changes: 13 additions & 2 deletions components/greasemonkey.js
Expand Up @@ -340,7 +340,7 @@ function runScriptInSandbox(code, sandbox, script) {
return true; // did not need a (function() {...})() enclosure.
}

function startup() {
function startup(aService) {
if (gStartupHasRun) return;
gStartupHasRun = true;

Expand All @@ -359,6 +359,10 @@ function startup() {
loader.loadSubScript("chrome://greasemonkey/content/third-party/mpl-utils.js");

loadGreasemonkeyVersion();

var observerService = Components.classes['@mozilla.org/observer-service;1']
.getService(Components.interfaces.nsIObserverService);
observerService.addObserver(aService, 'document-element-inserted', false);
}

/////////////////////////////////// Service ////////////////////////////////////
Expand Down Expand Up @@ -433,7 +437,14 @@ service.prototype.observe = function(aSubject, aTopic, aData) {
switch (aTopic) {
case 'app-startup':
case 'profile-after-change':
startup();
startup(this);
break;
case 'document-element-inserted':
var doc = aSubject;
if (null === doc.location) break;
if (!GM_util.isGreasemonkeyable(doc.location.href)) break;
var win = doc.defaultView;
this.runScripts('document-start', win);
break;
}
};
Expand Down
16 changes: 0 additions & 16 deletions content/browser.js
Expand Up @@ -26,20 +26,6 @@ GM_BrowserUI.init = function() {
window.addEventListener("unload", GM_BrowserUI.chromeUnload, false);
};

GM_BrowserUI.progressListener = {
onLocationChange:function(aBrowser, aProgress, aRequest, aURI) {
if (aProgress.isLoadingDocument) {
GM_BrowserUI.gmSvc.runScripts(
'document-start', aProgress.DOMWindow, window);
}
},
onStateChange:function() { },
onProgressChange:function() { },
onStatusChange:function() { },
onSecurityChange:function() { },
onLinkIconAvailable:function() { }
};

/**
* The browser XUL has loaded. Find the elements we need and set up our
* listeners and wrapper objects.
Expand Down Expand Up @@ -83,8 +69,6 @@ GM_BrowserUI.chromeLoad = function(e) {
GM_BrowserUI.gmSvc.config;

GM_BrowserUI.showToolbarButton();

gBrowser.addTabsProgressListener(GM_BrowserUI.progressListener);
};

GM_BrowserUI.contentLoad = function(event) {
Expand Down

0 comments on commit 8bc16d5

Please sign in to comment.