2019-02-18 12:47:07 +00:00
|
|
|
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal'
|
|
|
|
import { runInThisContext } from 'vm'
|
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
|
2017-09-17 03:27:03 +00:00
|
|
|
const regexp = new RegExp(`^${pattern.replace(/\*/g, '.*')}$`)
|
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) {
|
|
|
|
const context: { chrome?: any } = {}
|
2018-09-20 03:43:26 +00:00
|
|
|
require('@electron/internal/renderer/chrome-api').injectTo(extensionId, false, context)
|
2017-09-17 05:56:22 +00:00
|
|
|
const wrapper = `((chrome) => {\n ${code}\n })`
|
2016-05-28 06:37:44 +00:00
|
|
|
const compiledWrapper = runInThisContext(wrapper, {
|
|
|
|
filename: url,
|
|
|
|
lineOffset: 1,
|
|
|
|
displayErrors: true
|
|
|
|
})
|
2016-05-28 08:51:49 +00:00
|
|
|
return compiledWrapper.call(this, context.chrome)
|
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) {
|
2017-09-17 05:56:22 +00:00
|
|
|
const wrapper = `((code) => {
|
2017-09-17 03:27:03 +00:00
|
|
|
function init() {
|
2017-09-17 05:56:22 +00:00
|
|
|
const styleElement = document.createElement('style');
|
2017-09-17 03:27:03 +00:00
|
|
|
styleElement.textContent = code;
|
|
|
|
document.head.append(styleElement);
|
|
|
|
}
|
|
|
|
document.addEventListener('DOMContentLoaded', init);
|
2017-09-17 06:09:12 +00:00
|
|
|
})`
|
2019-02-18 12:47:07 +00:00
|
|
|
|
2017-09-17 03:27:03 +00:00
|
|
|
const compiledWrapper = runInThisContext(wrapper, {
|
|
|
|
filename: url,
|
|
|
|
lineOffset: 1,
|
2017-09-17 06:09:12 +00:00
|
|
|
displayErrors: true
|
|
|
|
})
|
2019-02-18 12:47:07 +00:00
|
|
|
|
2017-09-17 06:09:12 +00:00
|
|
|
return compiledWrapper.call(this, 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) {
|
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-02-18 12:47:07 +00:00
|
|
|
ipcRendererInternal.on('CHROME_TABS_EXECUTESCRIPT', function (
|
|
|
|
event: Electron.Event,
|
|
|
|
senderWebContentsId: number,
|
|
|
|
requestId: number,
|
|
|
|
extensionId: string,
|
|
|
|
url: string,
|
|
|
|
code: string
|
|
|
|
) {
|
2016-05-28 07:41:12 +00:00
|
|
|
const result = runContentScript.call(window, extensionId, url, code)
|
2019-02-15 01:24:25 +00:00
|
|
|
ipcRendererInternal.sendToAll(senderWebContentsId, `CHROME_TABS_EXECUTESCRIPT_RESULT_${requestId}`, result)
|
2016-05-28 07:41:12 +00:00
|
|
|
})
|
|
|
|
|
2016-05-27 01:29:57 +00:00
|
|
|
// Read the renderer process preferences.
|
2016-05-27 02:07:06 +00:00
|
|
|
const preferences = process.getRenderProcessPreferences()
|
|
|
|
if (preferences) {
|
|
|
|
for (const pref of preferences) {
|
|
|
|
if (pref.contentScripts) {
|
2016-05-28 08:51:49 +00:00
|
|
|
for (const script of pref.contentScripts) {
|
|
|
|
injectContentScript(pref.extensionId, script)
|
|
|
|
}
|
2016-05-27 02:07:06 +00:00
|
|
|
}
|
2016-05-27 01:29:57 +00:00
|
|
|
}
|
|
|
|
}
|