Making extensions/chromium/chrome.tabs.executeScriptInFrame.js adhere to the style guide

This commit is contained in:
Tim van der Meij 2014-03-09 23:29:34 +01:00
parent 8d2068dc6f
commit 241cb7999d

View File

@ -5,7 +5,7 @@
* *
* Implements the chrome.tabs.executeScriptInFrame API. * Implements the chrome.tabs.executeScriptInFrame API.
* This API is similar to the chrome.tabs.executeScript method, except * This API is similar to the chrome.tabs.executeScript method, except
* that it also recognizes the "frameId" property. * that it also recognizes the "frameId" property.
* This frameId can be obtained through the webNavigation or webRequest API. * This frameId can be obtained through the webNavigation or webRequest API.
* *
* When an error occurs, chrome.runtime.lastError is set. * When an error occurs, chrome.runtime.lastError is set.
@ -18,32 +18,36 @@
* In addition, the following field must also be set in manifest.json: * In addition, the following field must also be set in manifest.json:
* "web_accessible_resources": ["getFrameId"] * "web_accessible_resources": ["getFrameId"]
*/ */
/* globals chrome, console */
(function() { (function() {
/* jshint browser:true, maxlen:100 */ /* jshint browser:true, maxlen:100 */
/* globals chrome, console */ 'use strict';
'use strict';
chrome.tabs.executeScriptInFrame = executeScript;
// This URL is used to communicate the frameId. The resource is never visited, so it should chrome.tabs.executeScriptInFrame = executeScript;
// 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.
var MAXIMUM_RESPONSE_TIME_MS = 1000;
// Callbacks are stored here until they're invoked. // This URL is used to communicate the frameId. The resource is never
// Key = dummyUrl, value = callback function // visited, so it should be a non-existent location. Do not use *, ", '
var callbacks = {}; // 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.
var MAXIMUM_RESPONSE_TIME_MS = 1000;
chrome.webRequest.onBeforeRequest.addListener(function showFrameId(details) { // Callbacks are stored here until they're invoked.
// Key = dummyUrl, value = callback function
var callbacks = {};
chrome.webRequest.onBeforeRequest.addListener(function showFrameId(details) {
// Positive integer frameId >= 0 // 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; 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); var width = String.fromCharCode(frameId & 0xFF, (frameId >> 8) & 0xFF);
// When frameId > 0xFFFF, use the height to convey the additional information. // When frameId > 0xFFFF, use the height to convey the additional
// Again, add 1 to make sure that the height is non-zero. // information. Again, add 1 to make sure that the height is non-zero.
var height = String.fromCharCode((frameId >> 16) + 1, 0); var height = String.fromCharCode((frameId >> 16) + 1, 0);
// Convert data to base64 to avoid loss of bytes // Convert data to base64 to avoid loss of bytes
var image = 'data:image/gif;base64,' + btoa( var image = 'data:image/gif;base64,' + btoa(
@ -76,184 +80,204 @@ chrome.webRequest.onBeforeRequest.addListener(function showFrameId(details) {
// GIF trailer // GIF trailer
'\x3b' '\x3b'
); );
return {redirectUrl: image}; return {redirectUrl: image};
}, { }, {
urls: [URL_WHAT_IS_MY_FRAME_ID + '*'], urls: [URL_WHAT_IS_MY_FRAME_ID + '*'],
types: ['image'] types: ['image']
}, ['blocking']); }, ['blocking']);
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { chrome.runtime.onMessage.addListener(function(message, sender,
sendResponse) {
if (message && message.executeScriptCallback) { if (message && message.executeScriptCallback) {
var callback = callbacks[message.identifier]; var callback = callbacks[message.identifier];
if (callback) { if (callback) {
if (message.hello) { if (message.hello) {
clearTimeout(callback.timer); clearTimeout(callback.timer);
return; return;
}
delete callbacks[message.identifier];
// 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);
} }
delete callbacks[message.identifier];
// 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);
}
} }
}); });
/** /**
* Execute content script in a specific frame. * Execute content script in a specific frame.
* *
* @param tabId {integer} required * @param tabId {integer} required
* @param details.frameId {integer} required * @param details.frameId {integer} required
* @param details.code {string} Code or file is required (not both) * @param details.code {string} Code or file is required (not both)
* @param details.file {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 details.runAt {optional string} One of "document_start",
* @param callback {optional function(optional array of any result)} When an error occurs, result * "document_end", "document_idle"
* is not set. * @param callback {optional function(optional result array)} When an error
*/ * occurs, result
function executeScript(tabId, details, callback) { * is not set.
console.assert(typeof details === 'object', 'details must be an object (argument 0)'); */
function executeScript(tabId, details, callback) {
console.assert(typeof details === 'object',
'details must be an object (argument 0)');
var frameId = details.frameId; var frameId = details.frameId;
console.assert(typeof tabId === 'number', 'details.tabId must be a number'); console.assert(typeof tabId === 'number',
console.assert(typeof frameId === 'number', 'details.frameId must be a number'); 'details.tabId must be a number');
var sourceType = 'code' in details ? 'code' : 'file'; 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'); console.assert(sourceType in details, 'No source code or file specified');
var sourceValue = details[sourceType]; 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; var runAt = details.runAt;
if (!callback) callback = function() {/* no-op*/}; if (!callback) {
console.assert(typeof callback === 'function', 'callback must be a function'); callback = function() {/* no-op*/};
}
console.assert(typeof callback === 'function',
'callback must be a function');
if (frameId === 0) { 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
var injectDetails = { // in the main frame
allFrames: false, var injectDetails = {
runAt: runAt allFrames: false,
}; runAt: runAt
injectDetails[sourceType] = sourceValue; };
chrome.tabs.executeScript(tabId, injectDetails, callback); injectDetails[sourceType] = sourceValue;
return; chrome.tabs.executeScript(tabId, injectDetails, callback);
return;
} }
var identifier = Math.random().toString(36); var identifier = Math.random().toString(36);
if (sourceType === 'code') { if (sourceType === 'code') {
executeScriptInFrame(); executeScriptInFrame();
} else { // sourceType === 'file' } else { // sourceType === 'file'
(function() { (function() {
var x = new XMLHttpRequest(); var x = new XMLHttpRequest();
x.open('GET', chrome.extension.getURL(sourceValue), true); x.open('GET', chrome.extension.getURL(sourceValue), true);
x.onload = function() { x.onload = function() {
sourceValue = x.responseText; sourceValue = x.responseText;
executeScriptInFrame(); executeScriptInFrame();
}; };
x.onerror = function executeScriptResourceFetchError() { x.onerror = function executeScriptResourceFetchError() {
var message = 'Failed to load file: "' + sourceValue + '".'; var message = 'Failed to load file: "' + sourceValue + '".';
console.error('executeScript: ' + message); console.error('executeScript: ' + message);
chrome.runtime.lastError = chrome.extension.lastError = { message: message }; chrome.runtime.lastError = chrome.extension.lastError =
try { { message: message };
callback(); try {
} finally { callback();
chrome.runtime.lastError = chrome.extension.lastError = undefined; } finally {
} chrome.runtime.lastError = chrome.extension.lastError = undefined;
}; }
x.send(); };
})(); x.send();
})();
} }
function executeScriptInFrame() { function executeScriptInFrame() {
callbacks[identifier] = callback; callbacks[identifier] = callback;
chrome.tabs.executeScript(tabId, { chrome.tabs.executeScript(tabId, {
code: '(' + DETECT_FRAME + ')(' + code: '(' + DETECT_FRAME + ')(' +
'window,' + 'window,' +
JSON.stringify(identifier) + ',' + JSON.stringify(identifier) + ',' +
frameId + ',' + frameId + ',' +
JSON.stringify(sourceValue) + ')', JSON.stringify(sourceValue) + ')',
allFrames: true, allFrames: true,
runAt: 'document_start' runAt: 'document_start'
}, function(results) { }, function(results) {
if (results) { if (results) {
callback.timer = setTimeout(executeScriptTimedOut, MAXIMUM_RESPONSE_TIME_MS); callback.timer = setTimeout(executeScriptTimedOut,
} else { MAXIMUM_RESPONSE_TIME_MS);
// Failed :( } else {
delete callbacks[identifier]; // Failed :(
callback(); delete callbacks[identifier];
} 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;
console.error('executeScript: ' + message);
chrome.runtime.lastError = chrome.extension.lastError = { message: message };
try {
callback();
} finally {
chrome.runtime.lastError = chrome.extension.lastError = undefined;
} }
});
} }
}
/** function executeScriptTimedOut() {
* Code executed as a content script. var callback = callbacks[identifier];
*/ if (!callback) {
var DETECT_FRAME = '' + function checkFrame(window, identifier, frameId, code) { return;
}
delete callbacks[identifier];
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 };
try {
callback();
} finally {
chrome.runtime.lastError = chrome.extension.lastError = undefined;
}
}
}
/**
* Code executed as a content script.
*/
var DETECT_FRAME = '' + function checkFrame(window, identifier, frameId,
code) {
var i; var i;
if ('__executeScript_frameId__' in window) { if ('__executeScript_frameId__' in window) {
evalAsContentScript(); evalAsContentScript();
} else { } 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
i = window.document.createElement('img'); // in Chrome 27-29
i.onload = function() { i = window.document.createElement('img');
window.__executeScript_frameId__ = (this.naturalWidth - 1) + i.onload = function() {
(this.naturalHeight - 1 << 16); window.__executeScript_frameId__ = (this.naturalWidth - 1) +
evalAsContentScript(); (this.naturalHeight - 1 << 16);
}; evalAsContentScript();
// Trigger webRequest event to get frameId };
// (append extra characters to bust the cache) // Trigger webRequest event to get frameId
i.src = 'URL_WHAT_IS_MY_FRAME_ID?' + Math.random().toString(36).slice(-6); // (append extra characters to bust the cache)
i.src = 'URL_WHAT_IS_MY_FRAME_ID?' +
Math.random().toString(36).slice(-6);
} }
for (i = 0 ; i < window.frames.length; ++i) { for (i = 0 ; i < window.frames.length; ++i) {
try { try {
var frame = window.frames[i]; var frame = window.frames[i];
var scheme = frame.location.protocol; var scheme = frame.location.protocol;
if (scheme !== 'https:' && scheme !== 'http:' && scheme !== 'file:') { if (scheme !== 'https:' && scheme !== 'http:' && scheme !== 'file:') {
checkFrame(frame, identifier, frameId, code); checkFrame(frame, identifier, frameId, code);
}
} catch (e) {
// blocked by same origin policy, so it's not a javascript: / about:blank
// URL. chrome.tabs.executeScript will run the script for the frame.
} }
} catch (e) {
// blocked by same origin policy, so it's not a javascript:/about:blank
// 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
chrome.runtime.sendMessage({
executeScriptCallback: true,
hello: true,
identifier: identifier
});
var result = null;
try {
// jshint evil:true
result = window.eval(code);
} finally {
chrome.runtime.sendMessage({
executeScriptCallback: true,
evalResult: result,
identifier: identifier
});
}
}
}.toString().replace('URL_WHAT_IS_MY_FRAME_ID', URL_WHAT_IS_MY_FRAME_ID);
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
chrome.runtime.sendMessage({
executeScriptCallback: true,
hello: true,
identifier: identifier
});
var result = null;
try {
// jshint evil:true
result = window.eval(code);
} finally {
chrome.runtime.sendMessage({
executeScriptCallback: true,
evalResult: result,
identifier: identifier
});
}
}
}.toString().replace('URL_WHAT_IS_MY_FRAME_ID', URL_WHAT_IS_MY_FRAME_ID);
})(); })();