From ce9acac0ae5c7344c7d6f10cab8d058a091eec7d Mon Sep 17 00:00:00 2001 From: gorhill Date: Wed, 17 Dec 2014 08:02:37 -0500 Subject: [PATCH] this fixes #418; also remove last chrome API reference from portable code --- platform/chromium/vapi-background.js | 53 +++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/platform/chromium/vapi-background.js b/platform/chromium/vapi-background.js index 74c656b..208d3eb 100644 --- a/platform/chromium/vapi-background.js +++ b/platform/chromium/vapi-background.js @@ -19,7 +19,7 @@ Home: https://github.com/gorhill/uBlock */ -/* global self */ +/* global self, µBlock */ // For background page @@ -394,6 +394,57 @@ vAPI.lastError = function() { /******************************************************************************/ +// This is called only once, when everything has been loaded in memory after +// the extension was launched. It can be used to inject content scripts +// in already opened web pages, to remove whatever nuisance could make it to +// the web pages before uBlock was ready. + +vAPI.onLoadAllCompleted = function() { + // http://code.google.com/p/chromium/issues/detail?id=410868#c11 + // Need to be sure to access `vAPI.lastError()` to prevent + // spurious warnings in the console. + var scriptDone = function() { + vAPI.lastError(); + }; + var scriptEnd = function(tabId) { + if ( vAPI.lastError() ) { + return; + } + vAPI.tabs.injectScript(tabId, { + file: 'js/contentscript-end.js', + allFrames: true, + runAt: 'document_idle' + }, scriptDone); + }; + var scriptStart = function(tabId) { + vAPI.tabs.injectScript(tabId, { + file: 'js/vapi-client.js', + allFrames: true, + runAt: 'document_start' + }, function(){ }); + vAPI.tabs.injectScript(tabId, { + file: 'js/contentscript-start.js', + allFrames: true, + runAt: 'document_idle' + }, function(){ scriptEnd(tabId); }); + }; + var bindToTabs = function(tabs) { + var µb = µBlock; + var i = tabs.length, tab; + while ( i-- ) { + tab = tabs[i]; + µb.bindTabToPageStats(tab.id, tab.url); + // https://github.com/gorhill/uBlock/issues/129 + scriptStart(tab.id); + } + }; + + chrome.tabs.query({ url: 'http://*/*' }, bindToTabs); + chrome.tabs.query({ url: 'https://*/*' }, bindToTabs); +}; + +/******************************************************************************/ + })(); /******************************************************************************/