Run content scripts at correct phase
This commit is contained in:
parent
49d9446cce
commit
7e1f159185
4 changed files with 32 additions and 10 deletions
|
@ -1,13 +1,9 @@
|
|||
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
|
||||
if (pattern === '<all_urls>') return true
|
||||
|
||||
const regexp = new RegExp('^' + pattern.replace(/\*/g, '.*') + '$')
|
||||
return location.href.match(regexp)
|
||||
|
@ -21,15 +17,23 @@ const injectContentScript = function (script) {
|
|||
}
|
||||
|
||||
for (const js of script.js) {
|
||||
const fire = () => webFrame.executeJavaScript(js)
|
||||
if (script.runAt === 'document_start') {
|
||||
webFrame.executeJavaScript(String(js))
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Read the renderer process preferences.
|
||||
for (const pref of preferences) {
|
||||
if (pref.contentScripts) {
|
||||
pref.contentScripts.forEach(injectContentScript)
|
||||
const preferences = process.getRenderProcessPreferences()
|
||||
if (preferences) {
|
||||
for (const pref of preferences) {
|
||||
if (pref.contentScripts) {
|
||||
pref.contentScripts.forEach(injectContentScript)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue