Run content scripts at correct phase

This commit is contained in:
Cheng Zhao 2016-05-27 11:07:06 +09:00
parent 49d9446cce
commit 7e1f159185
4 changed files with 32 additions and 10 deletions

View file

@ -186,6 +186,23 @@ void AtomRendererClient::RunScriptsAtDocumentStart(
// Make sure every page will get a script context created. // Make sure every page will get a script context created.
render_frame->GetWebFrame()->executeScript( render_frame->GetWebFrame()->executeScript(
blink::WebScriptSource("void 0")); 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( blink::WebSpeechSynthesizer* AtomRendererClient::OverrideSpeechSynthesizer(

View file

@ -37,6 +37,7 @@ class AtomRendererClient : public content::ContentRendererClient {
void RenderFrameCreated(content::RenderFrame*) override; void RenderFrameCreated(content::RenderFrame*) override;
void RenderViewCreated(content::RenderView*) override; void RenderViewCreated(content::RenderView*) override;
void RunScriptsAtDocumentStart(content::RenderFrame* render_frame) override; void RunScriptsAtDocumentStart(content::RenderFrame* render_frame) override;
void RunScriptsAtDocumentEnd(content::RenderFrame* render_frame) override;
blink::WebSpeechSynthesizer* OverrideSpeechSynthesizer( blink::WebSpeechSynthesizer* OverrideSpeechSynthesizer(
blink::WebSpeechSynthesizerClient* client) override; blink::WebSpeechSynthesizerClient* client) override;
bool OverrideCreatePlugin(content::RenderFrame* render_frame, bool OverrideCreatePlugin(content::RenderFrame* render_frame,

View file

@ -83,7 +83,7 @@ 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 fs.readFileSync(path.join(manifest.srcDirectory, relativePath)) return String(fs.readFileSync(path.join(manifest.srcDirectory, relativePath)))
} }
const contentScriptToEntry = function (script) { const contentScriptToEntry = function (script) {

View file

@ -1,13 +1,9 @@
const preferences = process.getRenderProcessPreferences()
if (!preferences || preferences.length == 0) return
const {webFrame} = require('electron') const {webFrame} = require('electron')
// Check whether pattern matches. // Check whether pattern matches.
// https://developer.chrome.com/extensions/match_patterns // https://developer.chrome.com/extensions/match_patterns
const matchesPattern = function (pattern) { const matchesPattern = function (pattern) {
if (pattern === '<all_urls>') if (pattern === '<all_urls>') return true
return true
const regexp = new RegExp('^' + pattern.replace(/\*/g, '.*') + '$') const regexp = new RegExp('^' + pattern.replace(/\*/g, '.*') + '$')
return location.href.match(regexp) return location.href.match(regexp)
@ -21,15 +17,23 @@ const injectContentScript = function (script) {
} }
for (const js of script.js) { for (const js of script.js) {
const fire = () => webFrame.executeJavaScript(js)
if (script.runAt === 'document_start') { 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. // Read the renderer process preferences.
for (const pref of preferences) { const preferences = process.getRenderProcessPreferences()
if (pref.contentScripts) { if (preferences) {
pref.contentScripts.forEach(injectContentScript) for (const pref of preferences) {
if (pref.contentScripts) {
pref.contentScripts.forEach(injectContentScript)
}
} }
} }