Browse Source

render: executejavascript with option to simulate usergesture

Robo 9 years ago
parent
commit
239d535cac

+ 3 - 2
atom/browser/api/atom_api_web_contents.cc

@@ -572,8 +572,9 @@ void WebContents::InsertCSS(const std::string& css) {
   web_contents()->InsertCSS(css);
 }
 
-void WebContents::ExecuteJavaScript(const base::string16& code) {
-  web_contents()->GetMainFrame()->ExecuteJavaScript(code);
+void WebContents::ExecuteJavaScript(const base::string16& code,
+                                    bool has_user_gesture) {
+  Send(new AtomViewMsg_ExecuteJavaScript(routing_id(), code, has_user_gesture));
 }
 
 void WebContents::OpenDevTools(mate::Arguments* args) {

+ 2 - 1
atom/browser/api/atom_api_web_contents.h

@@ -65,7 +65,8 @@ class WebContents : public mate::TrackableObject<WebContents>,
   void SetUserAgent(const std::string& user_agent);
   std::string GetUserAgent();
   void InsertCSS(const std::string& css);
-  void ExecuteJavaScript(const base::string16& code);
+  void ExecuteJavaScript(const base::string16& code,
+                         bool has_user_gesture);
   void OpenDevTools(mate::Arguments* args);
   void CloseDevTools();
   bool IsDevToolsOpened();

+ 3 - 3
atom/browser/api/lib/web-contents.coffee

@@ -46,11 +46,11 @@ wrapWebContents = (webContents) ->
   # web contents has been loaded.
   webContents.loaded = false
   webContents.once 'did-finish-load', -> @loaded = true
-  webContents.executeJavaScript = (code) ->
+  webContents.executeJavaScript = (code, hasUserGesture=false) ->
     if @loaded
-      @_executeJavaScript code
+      @_executeJavaScript code, hasUserGesture
     else
-      webContents.once 'did-finish-load', @_executeJavaScript.bind(this, code)
+      webContents.once 'did-finish-load', @_executeJavaScript.bind(this, code, hasUserGesture)
 
   # The navigation controller.
   controller = new NavigationController(webContents)

+ 4 - 0
atom/common/api/api_messages.h

@@ -34,6 +34,10 @@ IPC_MESSAGE_ROUTED2(AtomViewMsg_Message,
                     base::string16 /* channel */,
                     base::ListValue /* arguments */)
 
+IPC_MESSAGE_ROUTED2(AtomViewMsg_ExecuteJavaScript,
+                    base::string16 /* code */,
+                    bool /* has user gesture */)
+
 // Sent by the renderer when the draggable regions are updated.
 IPC_MESSAGE_ROUTED1(AtomViewHostMsg_UpdateDraggableRegions,
                     std::vector<atom::DraggableRegion> /* regions */)

+ 23 - 0
atom/renderer/atom_render_view_observer.cc

@@ -26,6 +26,8 @@
 #include "third_party/WebKit/public/web/WebFrame.h"
 #include "third_party/WebKit/public/web/WebLocalFrame.h"
 #include "third_party/WebKit/public/web/WebKit.h"
+#include "third_party/WebKit/public/web/WebScopedUserGesture.h"
+#include "third_party/WebKit/public/web/WebScriptSource.h"
 #include "third_party/WebKit/public/web/WebView.h"
 #include "ui/base/resource/resource_bundle.h"
 
@@ -113,6 +115,8 @@ bool AtomRenderViewObserver::OnMessageReceived(const IPC::Message& message) {
   bool handled = true;
   IPC_BEGIN_MESSAGE_MAP(AtomRenderViewObserver, message)
     IPC_MESSAGE_HANDLER(AtomViewMsg_Message, OnBrowserMessage)
+    IPC_MESSAGE_HANDLER(AtomViewMsg_ExecuteJavaScript,
+                        OnJavaScriptExecuteRequest)
     IPC_MESSAGE_UNHANDLED(handled = false)
   IPC_END_MESSAGE_MAP()
 
@@ -143,4 +147,23 @@ void AtomRenderViewObserver::OnBrowserMessage(const base::string16& channel,
   }
 }
 
+void AtomRenderViewObserver::OnJavaScriptExecuteRequest(
+    const base::string16& code, bool has_user_gesture) {
+  if (!document_created_)
+    return;
+
+  if (!render_view()->GetWebView())
+    return;
+
+  scoped_ptr<blink::WebScopedUserGesture> gesture(
+      has_user_gesture ? new blink::WebScopedUserGesture : nullptr);
+
+  v8::Isolate* isolate = blink::mainThreadIsolate();
+  v8::HandleScope handle_scope(isolate);
+
+  blink::WebFrame* frame = render_view()->GetWebView()->mainFrame();
+  frame->executeScriptAndReturnValue(
+      blink::WebScriptSource(code));
+}
+
 }  // namespace atom

+ 2 - 0
atom/renderer/atom_render_view_observer.h

@@ -32,6 +32,8 @@ class AtomRenderViewObserver : public content::RenderViewObserver {
 
   void OnBrowserMessage(const base::string16& channel,
                         const base::ListValue& args);
+  void OnJavaScriptExecuteRequest(const base::string16& code,
+                                  bool has_user_gesture);
 
   // Weak reference to renderer client.
   AtomRendererClient* renderer_client_;

+ 3 - 2
docs/api/browser-window.md

@@ -905,11 +905,12 @@ Returns a `String` represents the user agent for this page.
 
 Injects CSS into this page.
 
-### WebContents.executeJavaScript(code)
+### WebContents.executeJavaScript(code, userGesture)
 
 * `code` String
+* `userGesture` Boolean - Default false
 
-Evaluates `code` in page.
+Evaluates `code` in page. If `userGesture` is set will simulate user gesture.
 
 ### WebContents.setAudioMuted(muted)