2016-05-28 07:41:12 +00:00
|
|
|
const {ipcRenderer} = require('electron')
|
2016-05-28 06:37:44 +00:00
|
|
|
const {runInThisContext} = require('vm')
|
2016-05-27 01:29:57 +00:00
|
|
|
|
|
|
|
// Check whether pattern matches.
|
|
|
|
// https://developer.chrome.com/extensions/match_patterns
|
|
|
|
const matchesPattern = function (pattern) {
|
2016-05-27 02:07:06 +00:00
|
|
|
if (pattern === '<all_urls>') return true
|
2016-05-27 01:29:57 +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.
|
|
|
|
const runContentScript = function (extensionId, url, code) {
|
2016-05-28 08:51:49 +00:00
|
|
|
const context = {}
|
2016-05-29 01:46:48 +00:00
|
|
|
require('./chrome-api').injectTo(extensionId, false, context)
|
2016-05-28 06:37:44 +00:00
|
|
|
const wrapper = `(function (chrome) {\n ${code}\n })`
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-05-27 01:29:57 +00:00
|
|
|
// Run injected scripts.
|
|
|
|
// https://developer.chrome.com/extensions/content_scripts
|
2016-05-28 08:51:49 +00:00
|
|
|
const injectContentScript = function (extensionId, script) {
|
2017-07-20 18:01:49 +00:00
|
|
|
if (!script.matches.some(matchesPattern)) return
|
2016-05-27 01:29:57 +00:00
|
|
|
|
2017-07-20 18:24:39 +00:00
|
|
|
if (script.js) {
|
|
|
|
for (const {url, code} of script.js) {
|
|
|
|
const fire = runContentScript.bind(window, extensionId, url, code)
|
|
|
|
if (script.runAt === 'document_start') {
|
|
|
|
process.once('document-start', fire)
|
|
|
|
} else if (script.runAt === 'document_end') {
|
|
|
|
process.once('document-end', fire)
|
|
|
|
} else if (script.runAt === 'document_idle') {
|
|
|
|
document.addEventListener('DOMContentLoaded', fire)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (script.css) {
|
|
|
|
for (const {code} of script.css) {
|
|
|
|
process.once('document-end', () => {
|
|
|
|
var node = document.createElement('style')
|
|
|
|
node.innerHTML = code
|
|
|
|
window.document.body.appendChild(node)
|
|
|
|
})
|
2016-05-27 01:29:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-28 07:41:12 +00:00
|
|
|
// Handle the request of chrome.tabs.executeJavaScript.
|
2016-05-28 12:13:00 +00:00
|
|
|
ipcRenderer.on('CHROME_TABS_EXECUTESCRIPT', function (event, senderWebContentsId, requestId, extensionId, url, code) {
|
2016-05-28 07:41:12 +00:00
|
|
|
const result = runContentScript.call(window, extensionId, url, code)
|
2016-05-28 12:13:00 +00:00
|
|
|
ipcRenderer.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
|
|
|
}
|
|
|
|
}
|