Browse Source

Script injection, element picker, messaging

- Add script injection to vAPI, plus a raw implementation for Safari
  (element-picker.js requires it)
- Tweak element picker to work with Safari
- Revert a change from previous commit: element-picker.js' background
  message handler (since actually it can have its own messaging channel)
- Don't send "undefined" reponses from background to content
pull/2/head
Deathamns 10 years ago
committed by gorhill
parent
commit
816dca2f33
  1. 41
      src/js/vapi-background.js

41
src/js/vapi-background.js

@ -136,7 +136,19 @@ if (window.chrome) {
wrapper(); wrapper();
} }
}, },
close: chrome.tabs.remove.bind(chrome.tabs)
close: chrome.tabs.remove.bind(chrome.tabs),
injectScript: function(tabId, details, callback) {
if (!callback) {
callback = function(){};
}
if (tabId) {
chrome.tabs.executeScript(tabId, details, callback);
}
else {
chrome.tabs.executeScript(details, callback);
}
}
}; };
// Must read: https://code.google.com/p/chromium/issues/detail?id=410868#c8 // Must read: https://code.google.com/p/chromium/issues/detail?id=410868#c8
@ -176,7 +188,7 @@ if (window.chrome) {
var onMessage = function(request) { var onMessage = function(request) {
var callback = function(response) { var callback = function(response) {
// stfu // stfu
if (chrome.runtime.lastError) {
if (chrome.runtime.lastError || response === undefined) {
return; return;
} }
@ -492,6 +504,29 @@ if (window.chrome) {
if (tab) { if (tab) {
tab.close(); tab.close();
} }
},
injectScript: function(tabId, details, callback) {
var tab = tabId ? this.stack[tabId] : safari.application.activeBrowserWindow.activeTab;
if (details.file) {
var xhr = new XMLHttpRequest;
xhr.overrideMimeType('application/x-javascript;charset=utf-8');
xhr.open('GET', details.file, false);
xhr.send();
details.code = xhr.responseText;
}
tab.page.dispatchMessage('message', {
portName: 'vAPI',
msg: {
cmd: 'runScript',
details: details
}
});
if (typeof callback === 'function') {
setTimeout(callback, 13);
}
} }
}; };
@ -624,7 +659,7 @@ if (window.chrome) {
} }
var callback = function(response) { var callback = function(response) {
if (request.message.requestId) {
if (request.message.requestId && response !== undefined) {
request.target.page.dispatchMessage( request.target.page.dispatchMessage(
'message', 'message',
{ {

Loading…
Cancel
Save