Run content scripts at correct phase
This commit is contained in:
parent
49d9446cce
commit
7e1f159185
4 changed files with 32 additions and 10 deletions
|
@ -186,6 +186,23 @@ void AtomRendererClient::RunScriptsAtDocumentStart(
|
|||
// Make sure every page will get a script context created.
|
||||
render_frame->GetWebFrame()->executeScript(
|
||||
blink::WebScriptSource("void 0"));
|
||||
|
||||
// Inform the docuemnt start pharse.
|
||||
node::Environment* env = node_bindings_->uv_env();
|
||||
if (env) {
|
||||
v8::HandleScope handle_scope(env->isolate());
|
||||
mate::EmitEvent(env->isolate(), env->process_object(), "document-start");
|
||||
}
|
||||
}
|
||||
|
||||
void AtomRendererClient::RunScriptsAtDocumentEnd(
|
||||
content::RenderFrame* render_frame) {
|
||||
// Inform the docuemnt end pharse.
|
||||
node::Environment* env = node_bindings_->uv_env();
|
||||
if (env) {
|
||||
v8::HandleScope handle_scope(env->isolate());
|
||||
mate::EmitEvent(env->isolate(), env->process_object(), "document-end");
|
||||
}
|
||||
}
|
||||
|
||||
blink::WebSpeechSynthesizer* AtomRendererClient::OverrideSpeechSynthesizer(
|
||||
|
|
|
@ -37,6 +37,7 @@ class AtomRendererClient : public content::ContentRendererClient {
|
|||
void RenderFrameCreated(content::RenderFrame*) override;
|
||||
void RenderViewCreated(content::RenderView*) override;
|
||||
void RunScriptsAtDocumentStart(content::RenderFrame* render_frame) override;
|
||||
void RunScriptsAtDocumentEnd(content::RenderFrame* render_frame) override;
|
||||
blink::WebSpeechSynthesizer* OverrideSpeechSynthesizer(
|
||||
blink::WebSpeechSynthesizerClient* client) override;
|
||||
bool OverrideCreatePlugin(content::RenderFrame* render_frame,
|
||||
|
|
|
@ -83,7 +83,7 @@ const injectContentScripts = function (manifest) {
|
|||
if (contentScripts[manifest.name] || !manifest.content_scripts) return
|
||||
|
||||
const readArrayOfFiles = function (relativePath) {
|
||||
return fs.readFileSync(path.join(manifest.srcDirectory, relativePath))
|
||||
return String(fs.readFileSync(path.join(manifest.srcDirectory, relativePath)))
|
||||
}
|
||||
|
||||
const contentScriptToEntry = function (script) {
|
||||
|
|
|
@ -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…
Reference in a new issue