|
@@ -1,5 +1,24 @@
|
|
|
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal'
|
|
|
-import { runInThisContext } from 'vm'
|
|
|
+import { webFrame } from 'electron'
|
|
|
+
|
|
|
+const v8Util = process.atomBinding('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: {[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++
|
|
|
+}
|
|
|
|
|
|
// Check whether pattern matches.
|
|
|
// https://developer.chrome.com/extensions/match_patterns
|
|
@@ -12,21 +31,21 @@ const matchesPattern = function (pattern: string) {
|
|
|
|
|
|
// Run the code with chrome API integrated.
|
|
|
const runContentScript = function (this: any, extensionId: string, url: string, code: string) {
|
|
|
- const context: { chrome?: any } = {}
|
|
|
- require('@electron/internal/renderer/chrome-api').injectTo(extensionId, false, context)
|
|
|
- const wrapper = `((chrome) => {\n ${code}\n })`
|
|
|
- try {
|
|
|
- const compiledWrapper = runInThisContext(wrapper, {
|
|
|
- filename: url,
|
|
|
- lineOffset: 1,
|
|
|
- displayErrors: true
|
|
|
- })
|
|
|
- return compiledWrapper.call(this, context.chrome)
|
|
|
- } catch (error) {
|
|
|
- // TODO(samuelmaddock): Run scripts in isolated world, see chromium script_injection.cc
|
|
|
- console.error(`Error running content script JavaScript for '${extensionId}'`)
|
|
|
- console.error(error)
|
|
|
- }
|
|
|
+ // 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 }]
|
|
|
+ webFrame.executeJavaScriptInIsolatedWorld(worldId, sources)
|
|
|
}
|
|
|
|
|
|
const runAllContentScript = function (scripts: Array<Electron.InjectionBase>, extensionId: string) {
|
|
@@ -36,28 +55,7 @@ const runAllContentScript = function (scripts: Array<Electron.InjectionBase>, ex
|
|
|
}
|
|
|
|
|
|
const runStylesheet = function (this: any, url: string, code: string) {
|
|
|
- const wrapper = `((code) => {
|
|
|
- function init() {
|
|
|
- const styleElement = document.createElement('style');
|
|
|
- styleElement.textContent = code;
|
|
|
- document.head.append(styleElement);
|
|
|
- }
|
|
|
- document.addEventListener('DOMContentLoaded', init);
|
|
|
- })`
|
|
|
-
|
|
|
- try {
|
|
|
- const compiledWrapper = runInThisContext(wrapper, {
|
|
|
- filename: url,
|
|
|
- lineOffset: 1,
|
|
|
- displayErrors: true
|
|
|
- })
|
|
|
-
|
|
|
- return compiledWrapper.call(this, code)
|
|
|
- } catch (error) {
|
|
|
- // TODO(samuelmaddock): Insert stylesheet directly into document, see chromium script_injection.cc
|
|
|
- console.error(`Error inserting content script stylesheet ${url}`)
|
|
|
- console.error(error)
|
|
|
- }
|
|
|
+ webFrame.insertCSS(code)
|
|
|
}
|
|
|
|
|
|
const runAllStylesheet = function (css: Array<Electron.InjectionBase>) {
|