|
|
|
@ -18,15 +18,17 @@
@@ -18,15 +18,17 @@
|
|
|
|
|
* In addition, the following field must also be set in manifest.json: |
|
|
|
|
* "web_accessible_resources": ["getFrameId"] |
|
|
|
|
*/ |
|
|
|
|
/* globals chrome, console */ |
|
|
|
|
|
|
|
|
|
(function() { |
|
|
|
|
/* jshint browser:true, maxlen:100 */ |
|
|
|
|
/* globals chrome, console */ |
|
|
|
|
'use strict'; |
|
|
|
|
|
|
|
|
|
chrome.tabs.executeScriptInFrame = executeScript; |
|
|
|
|
|
|
|
|
|
// This URL is used to communicate the frameId. The resource is never visited, so it should
|
|
|
|
|
// be a non-existent location. Do not use *, ", ' or line breaks in the file name.
|
|
|
|
|
// This URL is used to communicate the frameId. The resource is never
|
|
|
|
|
// visited, so it should be a non-existent location. Do not use *, ", '
|
|
|
|
|
// or line breaks in the file name.
|
|
|
|
|
var URL_WHAT_IS_MY_FRAME_ID = chrome.extension.getURL('getFrameId'); |
|
|
|
|
// The callback will be called within ... ms:
|
|
|
|
|
// Don't set a too low value.
|
|
|
|
@ -38,12 +40,14 @@ var callbacks = {};
@@ -38,12 +40,14 @@ var callbacks = {};
|
|
|
|
|
|
|
|
|
|
chrome.webRequest.onBeforeRequest.addListener(function showFrameId(details) { |
|
|
|
|
// Positive integer frameId >= 0
|
|
|
|
|
// Since an image is used as a data transport, we add 1 to get a non-zero width.
|
|
|
|
|
// Since an image is used as a data transport, we add 1 to get a
|
|
|
|
|
// non-zero width.
|
|
|
|
|
var frameId = details.frameId + 1; |
|
|
|
|
// Assume that the frameId fits in three bytes - which is a very reasonable assumption.
|
|
|
|
|
// Assume that the frameId fits in three bytes - which is a very
|
|
|
|
|
// reasonable assumption.
|
|
|
|
|
var width = String.fromCharCode(frameId & 0xFF, (frameId >> 8) & 0xFF); |
|
|
|
|
// When frameId > 0xFFFF, use the height to convey the additional information.
|
|
|
|
|
// Again, add 1 to make sure that the height is non-zero.
|
|
|
|
|
// When frameId > 0xFFFF, use the height to convey the additional
|
|
|
|
|
// information. Again, add 1 to make sure that the height is non-zero.
|
|
|
|
|
var height = String.fromCharCode((frameId >> 16) + 1, 0); |
|
|
|
|
// Convert data to base64 to avoid loss of bytes
|
|
|
|
|
var image = 'data:image/gif;base64,' + btoa( |
|
|
|
@ -82,7 +86,8 @@ chrome.webRequest.onBeforeRequest.addListener(function showFrameId(details) {
@@ -82,7 +86,8 @@ chrome.webRequest.onBeforeRequest.addListener(function showFrameId(details) {
|
|
|
|
|
types: ['image'] |
|
|
|
|
}, ['blocking']); |
|
|
|
|
|
|
|
|
|
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { |
|
|
|
|
chrome.runtime.onMessage.addListener(function(message, sender, |
|
|
|
|
sendResponse) { |
|
|
|
|
if (message && message.executeScriptCallback) { |
|
|
|
|
var callback = callbacks[message.identifier]; |
|
|
|
|
if (callback) { |
|
|
|
@ -91,10 +96,12 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
@@ -91,10 +96,12 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
delete callbacks[message.identifier]; |
|
|
|
|
// Result within an array to be consistent with the chrome.tabs.executeScript API.
|
|
|
|
|
// Result within an array to be consistent with the
|
|
|
|
|
// chrome.tabs.executeScript API.
|
|
|
|
|
callback([message.evalResult]); |
|
|
|
|
} else { |
|
|
|
|
console.warn('Callback not found for response in tab ' + sender.tab.id); |
|
|
|
|
console.warn('Callback not found for response in tab ' + |
|
|
|
|
sender.tab.id); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
@ -106,25 +113,35 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
@@ -106,25 +113,35 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
|
|
|
|
|
* @param details.frameId {integer} required |
|
|
|
|
* @param details.code {string} Code or file is required (not both) |
|
|
|
|
* @param details.file {string} Code or file is required (not both) |
|
|
|
|
* @param details.runAt {optional string} One of "document_start", "document_end", "document_idle" |
|
|
|
|
* @param callback {optional function(optional array of any result)} When an error occurs, result |
|
|
|
|
* @param details.runAt {optional string} One of "document_start", |
|
|
|
|
* "document_end", "document_idle" |
|
|
|
|
* @param callback {optional function(optional result array)} When an error |
|
|
|
|
* occurs, result |
|
|
|
|
* is not set. |
|
|
|
|
*/ |
|
|
|
|
function executeScript(tabId, details, callback) { |
|
|
|
|
console.assert(typeof details === 'object', 'details must be an object (argument 0)'); |
|
|
|
|
console.assert(typeof details === 'object', |
|
|
|
|
'details must be an object (argument 0)'); |
|
|
|
|
var frameId = details.frameId; |
|
|
|
|
console.assert(typeof tabId === 'number', 'details.tabId must be a number'); |
|
|
|
|
console.assert(typeof frameId === 'number', 'details.frameId must be a number'); |
|
|
|
|
var sourceType = 'code' in details ? 'code' : 'file'; |
|
|
|
|
console.assert(typeof tabId === 'number', |
|
|
|
|
'details.tabId must be a number'); |
|
|
|
|
console.assert(typeof frameId === 'number', |
|
|
|
|
'details.frameId must be a number'); |
|
|
|
|
var sourceType = ('code' in details ? 'code' : 'file'); |
|
|
|
|
console.assert(sourceType in details, 'No source code or file specified'); |
|
|
|
|
var sourceValue = details[sourceType]; |
|
|
|
|
console.assert(typeof sourceValue === 'string', 'details.' + sourceType + ' must be a string'); |
|
|
|
|
console.assert(typeof sourceValue === 'string', |
|
|
|
|
'details.' + sourceType + ' must be a string'); |
|
|
|
|
var runAt = details.runAt; |
|
|
|
|
if (!callback) callback = function() {/* no-op*/}; |
|
|
|
|
console.assert(typeof callback === 'function', 'callback must be a function'); |
|
|
|
|
if (!callback) { |
|
|
|
|
callback = function() {/* no-op*/}; |
|
|
|
|
} |
|
|
|
|
console.assert(typeof callback === 'function', |
|
|
|
|
'callback must be a function'); |
|
|
|
|
|
|
|
|
|
if (frameId === 0) { |
|
|
|
|
// No need for heavy lifting if we want to inject the script in the main frame
|
|
|
|
|
// No need for heavy lifting if we want to inject the script
|
|
|
|
|
// in the main frame
|
|
|
|
|
var injectDetails = { |
|
|
|
|
allFrames: false, |
|
|
|
|
runAt: runAt |
|
|
|
@ -149,7 +166,8 @@ function executeScript(tabId, details, callback) {
@@ -149,7 +166,8 @@ function executeScript(tabId, details, callback) {
|
|
|
|
|
x.onerror = function executeScriptResourceFetchError() { |
|
|
|
|
var message = 'Failed to load file: "' + sourceValue + '".'; |
|
|
|
|
console.error('executeScript: ' + message); |
|
|
|
|
chrome.runtime.lastError = chrome.extension.lastError = { message: message }; |
|
|
|
|
chrome.runtime.lastError = chrome.extension.lastError = |
|
|
|
|
{ message: message }; |
|
|
|
|
try { |
|
|
|
|
callback(); |
|
|
|
|
} finally { |
|
|
|
@ -160,7 +178,6 @@ function executeScript(tabId, details, callback) {
@@ -160,7 +178,6 @@ function executeScript(tabId, details, callback) {
|
|
|
|
|
})(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function executeScriptInFrame() { |
|
|
|
|
callbacks[identifier] = callback; |
|
|
|
|
chrome.tabs.executeScript(tabId, { |
|
|
|
@ -173,7 +190,8 @@ function executeScript(tabId, details, callback) {
@@ -173,7 +190,8 @@ function executeScript(tabId, details, callback) {
|
|
|
|
|
runAt: 'document_start' |
|
|
|
|
}, function(results) { |
|
|
|
|
if (results) { |
|
|
|
|
callback.timer = setTimeout(executeScriptTimedOut, MAXIMUM_RESPONSE_TIME_MS); |
|
|
|
|
callback.timer = setTimeout(executeScriptTimedOut, |
|
|
|
|
MAXIMUM_RESPONSE_TIME_MS); |
|
|
|
|
} else { |
|
|
|
|
// Failed :(
|
|
|
|
|
delete callbacks[identifier]; |
|
|
|
@ -181,15 +199,18 @@ function executeScript(tabId, details, callback) {
@@ -181,15 +199,18 @@ function executeScript(tabId, details, callback) {
|
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function executeScriptTimedOut() { |
|
|
|
|
var callback = callbacks[identifier]; |
|
|
|
|
if (!callback) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
delete callbacks[identifier]; |
|
|
|
|
var message = 'Failed to execute script: Frame ' + frameId + ' not found in tab ' + tabId; |
|
|
|
|
var message = 'Failed to execute script: Frame ' + frameId + |
|
|
|
|
' not found in tab ' + tabId; |
|
|
|
|
console.error('executeScript: ' + message); |
|
|
|
|
chrome.runtime.lastError = chrome.extension.lastError = { message: message }; |
|
|
|
|
chrome.runtime.lastError = chrome.extension.lastError = |
|
|
|
|
{ message: message }; |
|
|
|
|
try { |
|
|
|
|
callback(); |
|
|
|
|
} finally { |
|
|
|
@ -201,12 +222,14 @@ function executeScript(tabId, details, callback) {
@@ -201,12 +222,14 @@ function executeScript(tabId, details, callback) {
|
|
|
|
|
/** |
|
|
|
|
* Code executed as a content script. |
|
|
|
|
*/ |
|
|
|
|
var DETECT_FRAME = '' + function checkFrame(window, identifier, frameId, code) { |
|
|
|
|
var DETECT_FRAME = '' + function checkFrame(window, identifier, frameId, |
|
|
|
|
code) { |
|
|
|
|
var i; |
|
|
|
|
if ('__executeScript_frameId__' in window) { |
|
|
|
|
evalAsContentScript(); |
|
|
|
|
} else { |
|
|
|
|
// Do NOT use new Image(), because of http://crbug.com/245296 in Chrome 27-29
|
|
|
|
|
// Do NOT use new Image() because of http://crbug.com/245296
|
|
|
|
|
// in Chrome 27-29
|
|
|
|
|
i = window.document.createElement('img'); |
|
|
|
|
i.onload = function() { |
|
|
|
|
window.__executeScript_frameId__ = (this.naturalWidth - 1) + |
|
|
|
@ -215,7 +238,8 @@ var DETECT_FRAME = '' + function checkFrame(window, identifier, frameId, code) {
@@ -215,7 +238,8 @@ var DETECT_FRAME = '' + function checkFrame(window, identifier, frameId, code) {
|
|
|
|
|
}; |
|
|
|
|
// Trigger webRequest event to get frameId
|
|
|
|
|
// (append extra characters to bust the cache)
|
|
|
|
|
i.src = 'URL_WHAT_IS_MY_FRAME_ID?' + Math.random().toString(36).slice(-6); |
|
|
|
|
i.src = 'URL_WHAT_IS_MY_FRAME_ID?' + |
|
|
|
|
Math.random().toString(36).slice(-6); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (i = 0 ; i < window.frames.length; ++i) { |
|
|
|
@ -230,13 +254,14 @@ var DETECT_FRAME = '' + function checkFrame(window, identifier, frameId, code) {
@@ -230,13 +254,14 @@ var DETECT_FRAME = '' + function checkFrame(window, identifier, frameId, code) {
|
|
|
|
|
// URL. chrome.tabs.executeScript will run the script for the frame.
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function evalAsContentScript() { |
|
|
|
|
if (window.__executeScript_frameId__ !== frameId) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
// Send an early message to make sure that any blocking code
|
|
|
|
|
// in the evaluated code does not cause the time-out in the background page
|
|
|
|
|
// to be triggered
|
|
|
|
|
// in the evaluated code does not cause the time-out in the background
|
|
|
|
|
// page to be triggered
|
|
|
|
|
chrome.runtime.sendMessage({ |
|
|
|
|
executeScriptCallback: true, |
|
|
|
|
hello: true, |
|
|
|
@ -255,5 +280,4 @@ var DETECT_FRAME = '' + function checkFrame(window, identifier, frameId, code) {
@@ -255,5 +280,4 @@ var DETECT_FRAME = '' + function checkFrame(window, identifier, frameId, code) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}.toString().replace('URL_WHAT_IS_MY_FRAME_ID', URL_WHAT_IS_MY_FRAME_ID); |
|
|
|
|
|
|
|
|
|
})(); |
|
|
|
|