2019-03-11 23:27:57 +00:00
|
|
|
import { webFrame } from 'electron'
|
|
|
|
|
2019-03-26 02:38:35 +00:00
|
|
|
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils'
|
|
|
|
|
2019-03-18 19:37:06 +00:00
|
|
|
const v8Util = process.electronBinding('v8_util')
|
2019-03-11 23:27:57 +00:00
|
|
|
|
|
|
|
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: {[key: string]: number | undefined} = {}
|
|
|
|
|
|
|
|
// 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++
|
|
|
|
}
|
2016-05-27 01:29:57 +00:00
|
|
|
|
2019-03-28 21:03:37 +00:00
|
|
|
const escapePattern = function (pattern: string) {
|
|
|
|
return pattern.replace(/[\\^$+?.()|[\]{}]/g, '\\$&')
|
|
|
|
}
|
|
|
|
|
2016-05-27 01:29:57 +00:00
|
|
|
// Check whether pattern matches.
|
|
|
|
// https://developer.chrome.com/extensions/match_patterns
|
2019-02-18 12:47:07 +00:00
|
|
|
const matchesPattern = function (pattern: string) {
|
2016-05-27 02:07:06 +00:00
|
|
|
if (pattern === '<all_urls>') return true
|
2019-03-28 21:03:37 +00:00
|
|
|
const regexp = new RegExp(`^${pattern.split('*').map(escapePattern).join('.*')}$`)
|
2017-07-10 21:50:59 +00:00
|
|
|
const url = `${location.protocol}//${location.host}${location.pathname}`
|
2017-07-07 02:14:36 +00:00
|
|
|
return url.match(regexp)
|
2016-05-27 01:29:57 +00:00
|
|
|
}
|
|
|
|
|
2016-05-28 06:37:44 +00:00
|
|
|
// Run the code with chrome API integrated.
|
2019-02-18 12:47:07 +00:00
|
|
|
const runContentScript = function (this: any, extensionId: string, url: string, code: string) {
|
2019-03-11 23:27:57 +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)
|
|
|
|
|
|
|
|
webFrame.setIsolatedWorldInfo(worldId, {
|
|
|
|
name: `${extensionId} [${worldId}]`
|
|
|
|
// TODO(samuelmaddock): read `content_security_policy` from extension manifest
|
|
|
|
// csp: manifest.content_security_policy,
|
|
|
|
})
|
|
|
|
|
|
|
|
const sources = [{ code, url }]
|
2019-03-19 13:45:48 +00:00
|
|
|
return webFrame.executeJavaScriptInIsolatedWorld(worldId, sources)
|
2016-05-28 06:37:44 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 12:47:07 +00:00
|
|
|
const runAllContentScript = function (scripts: Array<Electron.InjectionBase>, extensionId: string) {
|
2018-09-13 16:10:51 +00:00
|
|
|
for (const { url, code } of scripts) {
|
2018-05-08 05:10:11 +00:00
|
|
|
runContentScript.call(window, extensionId, url, code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 12:47:07 +00:00
|
|
|
const runStylesheet = function (this: any, url: string, code: string) {
|
2019-03-11 23:27:57 +00:00
|
|
|
webFrame.insertCSS(code)
|
2017-09-17 03:27:03 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 12:47:07 +00:00
|
|
|
const runAllStylesheet = function (css: Array<Electron.InjectionBase>) {
|
2018-09-13 16:10:51 +00:00
|
|
|
for (const { url, code } of css) {
|
2018-05-08 05:10:11 +00:00
|
|
|
runStylesheet.call(window, url, code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-27 01:29:57 +00:00
|
|
|
// Run injected scripts.
|
|
|
|
// https://developer.chrome.com/extensions/content_scripts
|
2019-02-18 12:47:07 +00:00
|
|
|
const injectContentScript = function (extensionId: string, script: Electron.ContentScript) {
|
2019-03-08 23:53:25 +00:00
|
|
|
if (!process.isMainFrame && !script.allFrames) return
|
2017-07-20 18:01:49 +00:00
|
|
|
if (!script.matches.some(matchesPattern)) return
|
2016-05-27 01:29:57 +00:00
|
|
|
|
2017-09-17 05:56:22 +00:00
|
|
|
if (script.js) {
|
2018-05-08 05:10:11 +00:00
|
|
|
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)
|
2017-07-20 18:24:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 05:56:22 +00:00
|
|
|
if (script.css) {
|
2018-05-08 05:10:11 +00:00
|
|
|
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)
|
2016-05-27 01:29:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-28 07:41:12 +00:00
|
|
|
// Handle the request of chrome.tabs.executeJavaScript.
|
2019-03-26 02:38:35 +00:00
|
|
|
ipcRendererUtils.handle('CHROME_TABS_EXECUTE_SCRIPT', function (
|
2019-03-08 00:00:28 +00:00
|
|
|
event: Electron.Event,
|
2019-02-18 12:47:07 +00:00
|
|
|
extensionId: string,
|
|
|
|
url: string,
|
|
|
|
code: string
|
|
|
|
) {
|
2019-03-26 02:38:35 +00:00
|
|
|
return runContentScript.call(window, extensionId, url, code)
|
2016-05-28 07:41:12 +00:00
|
|
|
})
|
|
|
|
|
2019-06-19 15:23:44 +00:00
|
|
|
module.exports = (entries: Electron.ContentScriptEntry[]) => {
|
2019-06-04 23:07:34 +00:00
|
|
|
for (const entry of entries) {
|
|
|
|
if (entry.contentScripts) {
|
|
|
|
for (const script of entry.contentScripts) {
|
|
|
|
injectContentScript(entry.extensionId, script)
|
2016-05-28 08:51:49 +00:00
|
|
|
}
|
2016-05-27 02:07:06 +00:00
|
|
|
}
|
2016-05-27 01:29:57 +00:00
|
|
|
}
|
|
|
|
}
|