Inject chrome.* to content scripts
This commit is contained in:
parent
d8db695712
commit
f5b430d9e1
4 changed files with 56 additions and 33 deletions
|
@ -123,7 +123,10 @@ const injectContentScripts = function (manifest) {
|
||||||
if (contentScripts[manifest.name] || !manifest.content_scripts) return
|
if (contentScripts[manifest.name] || !manifest.content_scripts) return
|
||||||
|
|
||||||
const readArrayOfFiles = function (relativePath) {
|
const readArrayOfFiles = function (relativePath) {
|
||||||
return String(fs.readFileSync(path.join(manifest.srcDirectory, relativePath)))
|
return {
|
||||||
|
url: `chrome-extension://${manifest.hostname}/${relativePath}`,
|
||||||
|
code: String(fs.readFileSync(path.join(manifest.srcDirectory, relativePath)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const contentScriptToEntry = function (script) {
|
const contentScriptToEntry = function (script) {
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
const {ipcRenderer} = require('electron')
|
const {ipcRenderer} = require('electron')
|
||||||
const url = require('url')
|
const url = require('url')
|
||||||
|
|
||||||
// TODO(zcbenz): Set it to correct value for content scripts.
|
|
||||||
const currentExtensionId = window.location.hostname
|
|
||||||
|
|
||||||
class Event {
|
class Event {
|
||||||
constructor () {
|
constructor () {
|
||||||
this.listeners = []
|
this.listeners = []
|
||||||
|
@ -13,6 +10,13 @@ class Event {
|
||||||
this.listeners.push(callback)
|
this.listeners.push(callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
removeListener (callback) {
|
||||||
|
const index = this.listeners.indexOf(callback)
|
||||||
|
if (index !== -1) {
|
||||||
|
this.listeners.splice(index, 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
emit (...args) {
|
emit (...args) {
|
||||||
for (const listener of this.listeners) {
|
for (const listener of this.listeners) {
|
||||||
listener(...args)
|
listener(...args)
|
||||||
|
@ -67,41 +71,44 @@ class Port {
|
||||||
ipcRenderer.send('CHROME_PORT_POSTMESSAGE', this.webContentsId, this.portId, message)
|
ipcRenderer.send('CHROME_PORT_POSTMESSAGE', this.webContentsId, this.portId, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
_onDisconnect() {
|
_onDisconnect () {
|
||||||
ipcRenderer.removeAllListeners(`CHROME_PORT_ONMESSAGE_${this.portId}`)
|
ipcRenderer.removeAllListeners(`CHROME_PORT_ONMESSAGE_${this.portId}`)
|
||||||
this.onDisconnect.emit()
|
this.onDisconnect.emit()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const chrome = window.chrome = window.chrome || {}
|
// Inject chrome API to the |context|
|
||||||
|
exports.injectTo = function (extensionId, context) {
|
||||||
|
const chrome = context.chrome = context.chrome || {}
|
||||||
|
|
||||||
chrome.extension = {
|
chrome.runtime = {
|
||||||
getURL: function (path) {
|
getURL: function (path) {
|
||||||
return url.format({
|
return url.format({
|
||||||
protocol: window.location.protocol,
|
protocol: 'chrome-extension',
|
||||||
slashes: true,
|
slashes: true,
|
||||||
hostname: currentExtensionId,
|
hostname: extensionId,
|
||||||
pathname: path
|
pathname: path
|
||||||
})
|
})
|
||||||
}
|
},
|
||||||
}
|
|
||||||
|
|
||||||
chrome.runtime = {
|
onConnect: new OnConnect(),
|
||||||
getURL: chrome.extension.getURL,
|
|
||||||
|
|
||||||
onConnect: new OnConnect(),
|
connect (...args) {
|
||||||
|
// Parse the optional args.
|
||||||
|
let targetExtensionId = extensionId
|
||||||
|
let connectInfo = {name: ''}
|
||||||
|
if (args.length === 1) {
|
||||||
|
connectInfo = args[0]
|
||||||
|
} else if (args.length === 2) {
|
||||||
|
[targetExtensionId, connectInfo] = args
|
||||||
|
}
|
||||||
|
|
||||||
connect (...args) {
|
const {webContentsId, portId} = ipcRenderer.sendSync('CHROME_RUNTIME_CONNECT', targetExtensionId, connectInfo)
|
||||||
// Parse the optional args.
|
return new Port(webContentsId, portId, connectInfo.name)
|
||||||
let extensionId = currentExtensionId
|
|
||||||
let connectInfo = {name: ''}
|
|
||||||
if (args.length === 1) {
|
|
||||||
connectInfo = args[0]
|
|
||||||
} else if (args.length === 2) {
|
|
||||||
[extensionId, connectInfo] = args
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const {webContentsId, portId} = ipcRenderer.sendSync('CHROME_RUNTIME_CONNECT', extensionId, connectInfo)
|
chrome.extension = {
|
||||||
return new Port(webContentsId, portId, connectInfo.name)
|
getURL: chrome.runtime.getURL
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
const {webFrame} = require('electron')
|
const {runInThisContext} = require('vm')
|
||||||
|
|
||||||
// Check whether pattern matches.
|
// Check whether pattern matches.
|
||||||
// https://developer.chrome.com/extensions/match_patterns
|
// https://developer.chrome.com/extensions/match_patterns
|
||||||
|
@ -9,6 +9,19 @@ const matchesPattern = function (pattern) {
|
||||||
return location.href.match(regexp)
|
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.
|
// Run injected scripts.
|
||||||
// https://developer.chrome.com/extensions/content_scripts
|
// https://developer.chrome.com/extensions/content_scripts
|
||||||
const injectContentScript = function (script) {
|
const injectContentScript = function (script) {
|
||||||
|
@ -16,8 +29,8 @@ const injectContentScript = function (script) {
|
||||||
if (!matchesPattern(match)) return
|
if (!matchesPattern(match)) return
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const js of script.js) {
|
for (const {url, code} of script.js) {
|
||||||
const fire = () => webFrame.executeJavaScript(js)
|
const fire = runContentScript.bind(window, script.extensionId, url, code)
|
||||||
if (script.runAt === 'document_start') {
|
if (script.runAt === 'document_start') {
|
||||||
process.once('document-start', fire)
|
process.once('document-start', fire)
|
||||||
} else if (script.runAt === 'document_end') {
|
} else if (script.runAt === 'document_end') {
|
||||||
|
|
|
@ -63,7 +63,7 @@ if (window.location.protocol === 'chrome-devtools:') {
|
||||||
nodeIntegration = 'true'
|
nodeIntegration = 'true'
|
||||||
} else if (window.location.protocol === 'chrome-extension:') {
|
} else if (window.location.protocol === 'chrome-extension:') {
|
||||||
// Add implementations of chrome API.
|
// Add implementations of chrome API.
|
||||||
require('./chrome-api')
|
require('./chrome-api').injectTo(window.location.hostname, window)
|
||||||
nodeIntegration = 'true'
|
nodeIntegration = 'true'
|
||||||
} else {
|
} else {
|
||||||
// Override default web functions.
|
// Override default web functions.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue