Implement a simple content script injector
This commit is contained in:
parent
97c04735a2
commit
49d9446cce
4 changed files with 40 additions and 1 deletions
|
@ -49,6 +49,7 @@
|
||||||
'lib/common/init.js',
|
'lib/common/init.js',
|
||||||
'lib/common/reset-search-paths.js',
|
'lib/common/reset-search-paths.js',
|
||||||
'lib/renderer/chrome-api.js',
|
'lib/renderer/chrome-api.js',
|
||||||
|
'lib/renderer/content-scripts-injector.js',
|
||||||
'lib/renderer/init.js',
|
'lib/renderer/init.js',
|
||||||
'lib/renderer/inspector.js',
|
'lib/renderer/inspector.js',
|
||||||
'lib/renderer/override.js',
|
'lib/renderer/override.js',
|
||||||
|
|
|
@ -90,7 +90,7 @@ const injectContentScripts = function (manifest) {
|
||||||
return {
|
return {
|
||||||
matches: script.matches,
|
matches: script.matches,
|
||||||
js: script.js.map(readArrayOfFiles),
|
js: script.js.map(readArrayOfFiles),
|
||||||
run_at: script.run_at || 'document_idle'
|
runAt: script.run_at || 'document_idle'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
35
lib/renderer/content-scripts-injector.js
Normal file
35
lib/renderer/content-scripts-injector.js
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
const preferences = process.getRenderProcessPreferences()
|
||||||
|
if (!preferences || preferences.length == 0) return
|
||||||
|
|
||||||
|
const {webFrame} = require('electron')
|
||||||
|
|
||||||
|
// Check whether pattern matches.
|
||||||
|
// https://developer.chrome.com/extensions/match_patterns
|
||||||
|
const matchesPattern = function (pattern) {
|
||||||
|
if (pattern === '<all_urls>')
|
||||||
|
return true
|
||||||
|
|
||||||
|
const regexp = new RegExp('^' + pattern.replace(/\*/g, '.*') + '$')
|
||||||
|
return location.href.match(regexp)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run injected scripts.
|
||||||
|
// https://developer.chrome.com/extensions/content_scripts
|
||||||
|
const injectContentScript = function (script) {
|
||||||
|
for (const match of script.matches) {
|
||||||
|
if (!matchesPattern(match)) return
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const js of script.js) {
|
||||||
|
if (script.runAt === 'document_start') {
|
||||||
|
webFrame.executeJavaScript(String(js))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the renderer process preferences.
|
||||||
|
for (const pref of preferences) {
|
||||||
|
if (pref.contentScripts) {
|
||||||
|
pref.contentScripts.forEach(injectContentScript)
|
||||||
|
}
|
||||||
|
}
|
|
@ -69,6 +69,9 @@ if (window.location.protocol === 'chrome-devtools:') {
|
||||||
// Override default web functions.
|
// Override default web functions.
|
||||||
require('./override')
|
require('./override')
|
||||||
|
|
||||||
|
// Inject content scripts.
|
||||||
|
require('./content-scripts-injector')
|
||||||
|
|
||||||
// Load webview tag implementation.
|
// Load webview tag implementation.
|
||||||
if (nodeIntegration === 'true' && process.guestInstanceId == null) {
|
if (nodeIntegration === 'true' && process.guestInstanceId == null) {
|
||||||
require('./web-view/web-view')
|
require('./web-view/web-view')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue