From a8cb8397349cf8505681185d6c972b79dad6af04 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 16 Jun 2014 09:10:41 +0800 Subject: [PATCH] webContents.executeJavaScript should run code after page is loaded. Fixes atom/atom#1805. --- atom/browser/api/atom_api_web_contents.cc | 2 +- atom/browser/api/lib/web-contents.coffee | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 9877dc8b3eb2..84e695ffddec 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -186,7 +186,7 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder( .SetMethod("getRoutingId", &WebContents::GetRoutingID) .SetMethod("getProcessId", &WebContents::GetProcessID) .SetMethod("isCrashed", &WebContents::IsCrashed) - .SetMethod("executeJavaScript", &WebContents::ExecuteJavaScript) + .SetMethod("_executeJavaScript", &WebContents::ExecuteJavaScript) .SetMethod("_send", &WebContents::SendIPCMessage); } diff --git a/atom/browser/api/lib/web-contents.coffee b/atom/browser/api/lib/web-contents.coffee index 38fef143dfa1..92e41cdbde1e 100644 --- a/atom/browser/api/lib/web-contents.coffee +++ b/atom/browser/api/lib/web-contents.coffee @@ -11,6 +11,16 @@ module.exports.wrap = (webContents) -> webContents.send = (args...) -> @_send 'ATOM_INTERNAL_MESSAGE', [args...] + # Make sure webContents.executeJavaScript would run the code only when the + # web contents has been loaded. + webContents.loaded = false + webContents.once 'did-finish-load', -> @loaded = true + webContents.executeJavaScript = (code) -> + if @loaded + @_executeJavaScript code + else + webContents.once 'did-finish-load', @_executeJavaScript.bind(this, code) + # The processId and routingId and identify a webContents. webContents.getId = -> "#{@getProcessId()}-#{@getRoutingId()}" webContents.equal = (other) -> @getId() is other.getId()