From f74f07e0929958cea1721dd4d13f8d5048e7671d Mon Sep 17 00:00:00 2001 From: Deathamns Date: Tue, 16 Dec 2014 22:31:03 +0100 Subject: [PATCH] Firefox: implement vAPI.tabs.injectScript --- platform/chromium/vapi-client.js | 2 +- platform/firefox/frameModule.js | 25 +++++++++++++++++++++---- platform/firefox/vapi-background.js | 28 +++++++++++++++++++++++++++- platform/firefox/vapi-client.js | 15 ++++++++++++++- platform/safari/vapi-background.js | 2 +- platform/safari/vapi-client.js | 4 ++-- tools/make-firefox.sh | 2 +- tools/make-safari.sh | 2 +- 8 files changed, 68 insertions(+), 12 deletions(-) diff --git a/platform/chromium/vapi-client.js b/platform/chromium/vapi-client.js index c35de73b0..67f6ddf49 100644 --- a/platform/chromium/vapi-client.js +++ b/platform/chromium/vapi-client.js @@ -55,7 +55,7 @@ var messagingConnector = function(response) { var channels = vAPI.messaging.channels; var channel, listener; - if ( response.broadcast === true ) { + if ( response.broadcast === true && !response.portName ) { for ( channel in channels ) { if ( channels.hasOwnProperty(channel) === false ) { continue; diff --git a/platform/firefox/frameModule.js b/platform/firefox/frameModule.js index 8eaeab52c..376c35931 100644 --- a/platform/firefox/frameModule.js +++ b/platform/firefox/frameModule.js @@ -113,7 +113,18 @@ let contentPolicy = { }; let docObserver = { - contentBaseURI: 'chrome://' + appName + '/content/js/', + contentBaseURI: 'chrome://' + appName + '/content/', + injectScript: function(script, evalCode) { + if (evalCode) { + Components.utils.evalInSandbox(script, this); + return; + } + + Services.scriptloader.loadSubScript( + docObserver.contentBaseURI + script, + this + ); + }, initContext: function(win, sandbox) { let messager = getMessager(win); @@ -125,6 +136,12 @@ let docObserver = { }); win.self = win; + + Components.utils.exportFunction( + this.injectScript, + win, + {defineAs: 'injectScript'} + ); } win.sendAsyncMessage = messager.sendAsyncMessage; @@ -158,12 +175,12 @@ let docObserver = { let lss = Services.scriptloader.loadSubScript; win = this.initContext(win, true); - lss(this.contentBaseURI + 'vapi-client.js', win); - lss(this.contentBaseURI + 'contentscript-start.js', win); + lss(this.contentBaseURI + 'js/vapi-client.js', win); + lss(this.contentBaseURI + 'js/contentscript-start.js', win); let docReady = function(e) { this.removeEventListener(e.type, docReady, true); - lss(docObserver.contentBaseURI + 'contentscript-end.js', win); + lss(docObserver.contentBaseURI + 'js/contentscript-end.js', win); }; doc.addEventListener('DOMContentLoaded', docReady, true); diff --git a/platform/firefox/vapi-background.js b/platform/firefox/vapi-background.js index 5814d5072..92ff9d571 100644 --- a/platform/firefox/vapi-background.js +++ b/platform/firefox/vapi-background.js @@ -390,6 +390,11 @@ vAPI.tabs.get = function(tabId, callback) { } } + // for internal use + if (tab && callback === undefined) { + return tab; + } + if (!tab) { callback(); return; @@ -538,7 +543,27 @@ vAPI.tabs.close = function(tabIds) { /******************************************************************************/ vAPI.tabs.injectScript = function(tabId, details, callback) { + var tab = vAPI.tabs.get(tabId); + if (!tab) { + return; + } + + tab.linkedBrowser.messageManager.sendAsyncMessage( + vAPI.app.cleanName + ':broadcast', + JSON.stringify({ + broadcast: true, + portName: 'vAPI', + msg: { + cmd: 'injectScript', + details: details + } + }) + ); + + if (typeof callback === 'function') { + setTimeout(callback, 13); + } }; /******************************************************************************/ @@ -566,7 +591,8 @@ vAPI.setIcon = function(tabId, img, badge) { var button = curWin.document.getElementById(vAPI.toolbarButton.widgetId); var icon = vAPI.tabIcons[tabId]; button.setAttribute('badge', icon && icon.badge || ''); - button.style.listStyleImage = 'url(' + vAPI.getURL(icon && icon.img || 'img/browsericons/icon16-off.svg') + ')'; + icon = vAPI.getURL(icon && icon.img || 'img/browsericons/icon16-off.svg'); + button.style.listStyleImage = 'url(' + icon + ')'; }; /******************************************************************************/ diff --git a/platform/firefox/vapi-client.js b/platform/firefox/vapi-client.js index 0ecb65529..79f6ef922 100644 --- a/platform/firefox/vapi-client.js +++ b/platform/firefox/vapi-client.js @@ -44,7 +44,7 @@ var messagingConnector = function(response) { var channels = vAPI.messaging.channels; var channel, listener; - if ( response.broadcast === true ) { + if ( response.broadcast === true && !response.portName ) { for ( channel in channels ) { if ( channels.hasOwnProperty(channel) === false ) { continue; @@ -92,6 +92,19 @@ vAPI.messaging = { messagingConnector(JSON.parse(msg)); }; addMessageListener(this.connectorId, this.connector); + + this.channels['vAPI'] = {}; + this.channels['vAPI'].listener = function(msg) { + if (msg.cmd === 'injectScript') { + var details = msg.details; + + if (!details.allFrames && window !== window.top) { + return; + } + + self.injectScript(details.file || details.code, !details.file); + } + }; }, close: function() { diff --git a/platform/safari/vapi-background.js b/platform/safari/vapi-background.js index 8c4e61f62..362ab1a00 100644 --- a/platform/safari/vapi-background.js +++ b/platform/safari/vapi-background.js @@ -352,7 +352,7 @@ vAPI.tabs.injectScript = function(tabId, details, callback) { tab.page.dispatchMessage('broadcast', { portName: 'vAPI', msg: { - cmd: 'runScript', + cmd: 'injectScript', details: details } }); diff --git a/platform/safari/vapi-client.js b/platform/safari/vapi-client.js index e7c1cd76e..523b4dca1 100644 --- a/platform/safari/vapi-client.js +++ b/platform/safari/vapi-client.js @@ -42,7 +42,7 @@ var messagingConnector = function(response) { var channels = vAPI.messaging.channels; var channel, listener; - if ( response.broadcast === true ) { + if ( response.broadcast === true && !response.portName ) { for ( channel in channels ) { if ( channels.hasOwnProperty(channel) === false ) { continue; @@ -101,7 +101,7 @@ vAPI.messaging = { this.channels['vAPI'] = { listener: function(msg) { - if (msg.cmd === 'runScript' && msg.details.code) { + if (msg.cmd === 'injectScript' && msg.details.code) { Function(msg.details.code).call(self); } } diff --git a/tools/make-firefox.sh b/tools/make-firefox.sh index 35a4a9934..a959d5235 100644 --- a/tools/make-firefox.sh +++ b/tools/make-firefox.sh @@ -23,7 +23,7 @@ cp platform/firefox/frameModule.js $DES/ cp platform/firefox/chrome.manifest $DES/ cp platform/firefox/install.rdf $DES/ -echo "*** uBlock_xpi: Generating locales" +echo "*** uBlock_xpi: Generating meta..." python tools/make-firefox-meta.py $DES/ echo "*** uBlock_xpi: Package done." diff --git a/tools/make-safari.sh b/tools/make-safari.sh index bb8d5cd2b..db719bc00 100755 --- a/tools/make-safari.sh +++ b/tools/make-safari.sh @@ -19,7 +19,7 @@ cp platform/safari/*.js $DES/js/ cp platform/safari/Info.plist $DES/ cp platform/safari/Settings.plist $DES/ -echo "*** uBlock_xpi: Generating locales" +echo "*** uBlock_xpi: Generating meta..." python tools/make-safari-meta.py $DES/ echo "*** uBlock.safariextension: Package done."