Inject chrome.* to content scripts
This commit is contained in:
parent
d8db695712
commit
f5b430d9e1
4 changed files with 56 additions and 33 deletions
|
@ -1,4 +1,4 @@
|
|||
const {webFrame} = require('electron')
|
||||
const {runInThisContext} = require('vm')
|
||||
|
||||
// Check whether pattern matches.
|
||||
// https://developer.chrome.com/extensions/match_patterns
|
||||
|
@ -9,6 +9,19 @@ const matchesPattern = function (pattern) {
|
|||
return location.href.match(regexp)
|
||||
}
|
||||
|
||||
// Run the code with chrome API integrated.
|
||||
const runContentScript = function (extensionId, url, code) {
|
||||
const chrome = {}
|
||||
require('./chrome-api').injectTo(extensionId, chrome)
|
||||
const wrapper = `(function (chrome) {\n ${code}\n })`
|
||||
const compiledWrapper = runInThisContext(wrapper, {
|
||||
filename: url,
|
||||
lineOffset: 1,
|
||||
displayErrors: true
|
||||
})
|
||||
compiledWrapper.call(this, chrome)
|
||||
}
|
||||
|
||||
// Run injected scripts.
|
||||
// https://developer.chrome.com/extensions/content_scripts
|
||||
const injectContentScript = function (script) {
|
||||
|
@ -16,8 +29,8 @@ const injectContentScript = function (script) {
|
|||
if (!matchesPattern(match)) return
|
||||
}
|
||||
|
||||
for (const js of script.js) {
|
||||
const fire = () => webFrame.executeJavaScript(js)
|
||||
for (const {url, code} of script.js) {
|
||||
const fire = runContentScript.bind(window, script.extensionId, url, code)
|
||||
if (script.runAt === 'document_start') {
|
||||
process.once('document-start', fire)
|
||||
} else if (script.runAt === 'document_end') {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue