From 4ad98583579c8b187e7008e70d1ac30fd20404c8 Mon Sep 17 00:00:00 2001 From: Deathamns Date: Thu, 12 Mar 2015 18:20:48 +0100 Subject: [PATCH] Firefox: load content-scripts on extension start --- platform/firefox/frameModule.js | 6 +++++- platform/firefox/frameScript.js | 32 ++++++++++++++++++++++++++++- platform/firefox/vapi-background.js | 17 ++++++++++++++- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/platform/firefox/frameModule.js b/platform/firefox/frameModule.js index 9e1c44416..f33154cf2 100644 --- a/platform/firefox/frameModule.js +++ b/platform/firefox/frameModule.js @@ -299,7 +299,11 @@ const contentObserver = { } }; - doc.addEventListener('DOMContentLoaded', docReady, true); + if ( doc.readyState === 'loading') { + doc.addEventListener('DOMContentLoaded', docReady, true); + } else { + docReady({ target: doc, type: 'DOMContentLoaded' }); + } } }; diff --git a/platform/firefox/frameScript.js b/platform/firefox/frameScript.js index 560eca251..bb5f898cd 100644 --- a/platform/firefox/frameScript.js +++ b/platform/firefox/frameScript.js @@ -19,13 +19,43 @@ Home: https://github.com/gorhill/uBlock */ +/******************************************************************************/ + +(function() { + 'use strict'; /******************************************************************************/ -Components.utils.import( +let {contentObserver} = Components.utils.import( Components.stack.filename.replace('Script', 'Module'), null ); +let injectContentScripts = function(win) { + if ( !win || !win.document ) { + return; + } + + contentObserver.observe(win.document); + + if ( win.frames && win.frames.length ) { + let i = win.frames.length; + while ( i-- ) { + injectContentScripts(win.frames[i]); + } + } +}; + +let onLoadCompleted = function() { + removeMessageListener('ublock-load-completed', onLoadCompleted); + injectContentScripts(content); +}; + +addMessageListener('ublock-load-completed', onLoadCompleted); + +/******************************************************************************/ + +})(); + /******************************************************************************/ diff --git a/platform/firefox/vapi-background.js b/platform/firefox/vapi-background.js index 49c8860ce..b27ced4a9 100644 --- a/platform/firefox/vapi-background.js +++ b/platform/firefox/vapi-background.js @@ -1849,7 +1849,22 @@ vAPI.lastError = function() { // in already opened web pages, to remove whatever nuisance could make it to // the web pages before uBlock was ready. -vAPI.onLoadAllCompleted = function() {}; +vAPI.onLoadAllCompleted = function() { + var µb = µBlock; + for ( var tab of this.tabs.getAll() ) { + // We're insterested in only the tabs that were already loaded + if ( tab.hasAttribute('pending') ) { + continue; + } + + var tabId = this.tabs.getTabId(tab); + var browser = getBrowserForTab(tab); + µb.bindTabToPageStats(tabId, browser.currentURI.spec); + browser.messageManager.sendAsyncMessage( + location.host + '-load-completed' + ); + } +}; /******************************************************************************/