diff --git a/atom/renderer/atom_renderer_client.cc b/atom/renderer/atom_renderer_client.cc index 114fb90555b..4ddbe956b89 100644 --- a/atom/renderer/atom_renderer_client.cc +++ b/atom/renderer/atom_renderer_client.cc @@ -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( diff --git a/atom/renderer/atom_renderer_client.h b/atom/renderer/atom_renderer_client.h index 78c2e495994..1a178e01f4f 100644 --- a/atom/renderer/atom_renderer_client.h +++ b/atom/renderer/atom_renderer_client.h @@ -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, diff --git a/lib/browser/chrome-extension.js b/lib/browser/chrome-extension.js index fbf0fef5b1c..9a86e2fc34d 100644 --- a/lib/browser/chrome-extension.js +++ b/lib/browser/chrome-extension.js @@ -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) { diff --git a/lib/renderer/content-scripts-injector.js b/lib/renderer/content-scripts-injector.js index 0594ada07bf..07f330b7cc1 100644 --- a/lib/renderer/content-scripts-injector.js +++ b/lib/renderer/content-scripts-injector.js @@ -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 === '') - return true + if (pattern === '') 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) + } } }