Firefox: implement vAPI.tabs.injectScript

This commit is contained in:
Deathamns 2014-12-16 22:31:03 +01:00
parent 0e7122edf4
commit f74f07e092
8 changed files with 68 additions and 12 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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 + ')';
};
/******************************************************************************/

View File

@ -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() {

View File

@ -352,7 +352,7 @@ vAPI.tabs.injectScript = function(tabId, details, callback) {
tab.page.dispatchMessage('broadcast', {
portName: 'vAPI',
msg: {
cmd: 'runScript',
cmd: 'injectScript',
details: details
}
});

View File

@ -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);
}
}

View File

@ -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."

View File

@ -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."