2019-07-01 23:00:17 +00:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
const electron_1 = require("electron");
|
|
|
|
const ipcRendererUtils = require("@electron/internal/renderer/ipc-renderer-internal-utils");
|
|
|
|
const v8Util = process.electronBinding('v8_util');
|
|
|
|
const IsolatedWorldIDs = {
|
|
|
|
/**
|
|
|
|
* Start of extension isolated world IDs, as defined in
|
|
|
|
* atom_render_frame_observer.h
|
|
|
|
*/
|
|
|
|
ISOLATED_WORLD_EXTENSIONS: 1 << 20
|
|
|
|
};
|
|
|
|
let isolatedWorldIds = IsolatedWorldIDs.ISOLATED_WORLD_EXTENSIONS;
|
|
|
|
const extensionWorldId = {};
|
|
|
|
// https://cs.chromium.org/chromium/src/extensions/renderer/script_injection.cc?type=cs&sq=package:chromium&g=0&l=52
|
|
|
|
const getIsolatedWorldIdForInstance = () => {
|
|
|
|
// TODO(samuelmaddock): allocate and cleanup IDs
|
|
|
|
return isolatedWorldIds++;
|
|
|
|
};
|
|
|
|
const escapePattern = function (pattern) {
|
|
|
|
return pattern.replace(/[\\^$+?.()|[\]{}]/g, '\\$&');
|
|
|
|
};
|
2019-06-16 20:19:06 +00:00
|
|
|
// Check whether pattern matches.
|
|
|
|
// https://developer.chrome.com/extensions/match_patterns
|
|
|
|
const matchesPattern = function (pattern) {
|
2019-07-01 23:00:17 +00:00
|
|
|
if (pattern === '<all_urls>')
|
|
|
|
return true;
|
|
|
|
const regexp = new RegExp(`^${pattern.split('*').map(escapePattern).join('.*')}$`);
|
|
|
|
const url = `${location.protocol}//${location.host}${location.pathname}`;
|
|
|
|
return url.match(regexp);
|
|
|
|
};
|
2019-06-16 20:19:06 +00:00
|
|
|
// Run the code with chrome API integrated.
|
|
|
|
const runContentScript = function (extensionId, url, code) {
|
2019-07-01 23:00:17 +00:00
|
|
|
// Assign unique world ID to each extension
|
|
|
|
const worldId = extensionWorldId[extensionId] ||
|
|
|
|
(extensionWorldId[extensionId] = getIsolatedWorldIdForInstance());
|
|
|
|
// store extension ID for content script to read in isolated world
|
|
|
|
v8Util.setHiddenValue(global, `extension-${worldId}`, extensionId);
|
|
|
|
electron_1.webFrame.setIsolatedWorldInfo(worldId, {
|
|
|
|
name: `${extensionId} [${worldId}]`
|
|
|
|
// TODO(samuelmaddock): read `content_security_policy` from extension manifest
|
|
|
|
// csp: manifest.content_security_policy,
|
|
|
|
});
|
|
|
|
const sources = [{ code, url }];
|
|
|
|
return electron_1.webFrame.executeJavaScriptInIsolatedWorld(worldId, sources);
|
|
|
|
};
|
2019-06-16 20:19:06 +00:00
|
|
|
const runAllContentScript = function (scripts, extensionId) {
|
2019-07-01 23:00:17 +00:00
|
|
|
for (const { url, code } of scripts) {
|
|
|
|
runContentScript.call(window, extensionId, url, code);
|
2019-06-16 20:19:06 +00:00
|
|
|
}
|
2019-07-01 23:00:17 +00:00
|
|
|
};
|
|
|
|
const runStylesheet = function (url, code) {
|
|
|
|
electron_1.webFrame.insertCSS(code);
|
|
|
|
};
|
2019-06-20 21:19:08 +00:00
|
|
|
const runAllStylesheet = function (css) {
|
2019-07-01 23:00:17 +00:00
|
|
|
for (const { url, code } of css) {
|
|
|
|
runStylesheet.call(window, url, code);
|
|
|
|
}
|
|
|
|
};
|
2019-06-16 20:19:06 +00:00
|
|
|
// Run injected scripts.
|
|
|
|
// https://developer.chrome.com/extensions/content_scripts
|
|
|
|
const injectContentScript = function (extensionId, script) {
|
2019-07-01 23:00:17 +00:00
|
|
|
if (!process.isMainFrame && !script.allFrames)
|
|
|
|
return;
|
|
|
|
if (!script.matches.some(matchesPattern))
|
|
|
|
return;
|
|
|
|
if (script.js) {
|
|
|
|
const fire = runAllContentScript.bind(window, script.js, extensionId);
|
|
|
|
if (script.runAt === 'document_start') {
|
|
|
|
process.once('document-start', fire);
|
|
|
|
}
|
|
|
|
else if (script.runAt === 'document_end') {
|
|
|
|
process.once('document-end', fire);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
document.addEventListener('DOMContentLoaded', fire);
|
|
|
|
}
|
2019-06-16 20:19:06 +00:00
|
|
|
}
|
2019-07-01 23:00:17 +00:00
|
|
|
if (script.css) {
|
|
|
|
const fire = runAllStylesheet.bind(window, script.css);
|
|
|
|
if (script.runAt === 'document_start') {
|
|
|
|
process.once('document-start', fire);
|
|
|
|
}
|
|
|
|
else if (script.runAt === 'document_end') {
|
|
|
|
process.once('document-end', fire);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
document.addEventListener('DOMContentLoaded', fire);
|
|
|
|
}
|
2019-06-16 20:19:06 +00:00
|
|
|
}
|
2019-07-01 23:00:17 +00:00
|
|
|
};
|
2019-06-16 20:19:06 +00:00
|
|
|
// Handle the request of chrome.tabs.executeJavaScript.
|
2019-07-01 23:00:17 +00:00
|
|
|
ipcRendererUtils.handle('CHROME_TABS_EXECUTE_SCRIPT', function (event, extensionId, url, code) {
|
|
|
|
return runContentScript.call(window, extensionId, url, code);
|
|
|
|
});
|
|
|
|
module.exports = (getRenderProcessPreferences) => {
|
|
|
|
// Read the renderer process preferences.
|
|
|
|
const preferences = getRenderProcessPreferences();
|
|
|
|
if (preferences) {
|
|
|
|
for (const pref of preferences) {
|
|
|
|
if (pref.contentScripts) {
|
|
|
|
for (const script of pref.contentScripts) {
|
|
|
|
injectContentScript(pref.extensionId, script);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-16 20:19:06 +00:00
|
|
|
}
|
2019-07-01 23:00:17 +00:00
|
|
|
};
|
|
|
|
//# sourceMappingURL=content-scripts-injector.js.map
|