diff --git a/include/cef.h b/include/cef.h index d4e748ee0..f23607f58 100644 --- a/include/cef.h +++ b/include/cef.h @@ -220,7 +220,16 @@ public: virtual void LoadStream(CefRefPtr stream, const std::wstring& url) =0; - // Register a new handler tied to the specified JS object |name|. Returns + // Execute a string of JavaScript code in the specified target frame. The + // |script_url| parameter is the URL where the script in question can be + // found, if any. The renderer may request this URL to show the developer the + // source of the error. The |start_line| parameter is the base line number + // to use for error reporting. + virtual void ExecuteJavaScript(const std::wstring& js_code, + const std::wstring& script_url, + int start_line, TargetFrame targetFrame) =0; + +// Register a new handler tied to the specified JS object |name|. Returns // true if the handler is registered successfully. // A JS handler will be accessible to JavaScript as window.. virtual bool AddJSHandler(const std::wstring& classname, diff --git a/libcef/browser_impl.cc b/libcef/browser_impl.cc index 07829dc2c..3189f4076 100644 --- a/libcef/browser_impl.cc +++ b/libcef/browser_impl.cc @@ -154,6 +154,16 @@ void CefBrowserImpl::LoadStream(CefRefPtr stream, &CefBrowserImpl::UIT_LoadHTMLForStreamRef, stream.get(), url)); } +void CefBrowserImpl::ExecuteJavaScript(const std::wstring& js_code, + const std::wstring& script_url, + int start_line, + TargetFrame targetFrame) +{ + PostTask(FROM_HERE, NewRunnableMethod(this, + &CefBrowserImpl::UIT_ExecuteJavaScript, js_code, script_url, start_line, + targetFrame)); +} + bool CefBrowserImpl::AddJSHandler(const std::wstring& classname, CefRefPtr handler) { @@ -310,6 +320,22 @@ void CefBrowserImpl::UIT_LoadURLForRequestRef(CefRequest* request) request->Release(); } +void CefBrowserImpl::UIT_ExecuteJavaScript(const std::wstring& js_code, + const std::wstring& script_url, + int start_line, + TargetFrame targetFrame) +{ + REQUIRE_UIT(); + + WebFrame* frame; + if(targetFrame == TF_FOCUSED) + frame = UIT_GetWebView()->GetFocusedFrame(); + else + frame = UIT_GetWebView()->GetMainFrame(); + + frame->ExecuteJavaScript(WideToUTF8(js_code), GURL(script_url), start_line); +} + void CefBrowserImpl::UIT_GoBackOrForward(int offset) { REQUIRE_UIT(); diff --git a/libcef/browser_impl.h b/libcef/browser_impl.h index dc9342da9..381acd1dd 100644 --- a/libcef/browser_impl.h +++ b/libcef/browser_impl.h @@ -58,6 +58,9 @@ public: const std::wstring& url); virtual void LoadStream(CefRefPtr stream, const std::wstring& url); + virtual void ExecuteJavaScript(const std::wstring& js_code, + const std::wstring& script_url, + int start_line, TargetFrame targetFrame); virtual bool AddJSHandler(const std::wstring& classname, CefRefPtr handler); virtual bool HasJSHandler(const std::wstring& classname); @@ -137,13 +140,16 @@ public: void UIT_LoadURLForRequest(const std::wstring& url, const std::wstring& frame_name, const std::wstring& method, - net::UploadData *upload_data, - const WebRequest::HeaderMap& headers); + net::UploadData *upload_data, + const WebRequest::HeaderMap& headers); void UIT_LoadURLForRequestRef(CefRequest* request); void UIT_LoadHTML(const std::wstring& html, const std::wstring& url); void UIT_LoadHTMLForStreamRef(CefStreamReader* stream, const std::wstring& url); + void UIT_ExecuteJavaScript(const std::wstring& js_code, + const std::wstring& script_url, + int start_line, TargetFrame targetFrame); void UIT_GoBackOrForward(int offset); void UIT_Reload(); bool UIT_Navigate(const BrowserNavigationEntry& entry, bool reload); diff --git a/tests/cefclient/cefclient.cpp b/tests/cefclient/cefclient.cpp index 5bf39b843..06b9a4137 100644 --- a/tests/cefclient/cefclient.cpp +++ b/tests/cefclient/cefclient.cpp @@ -788,7 +788,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) if(browser.get()) browser->StopLoad(); return 0; - case ID_TESTS_JAVASCRIPT: // Test our javascript handler + case ID_TESTS_JAVASCRIPT_HANDLER: // Test our javascript handler if(browser.get()) { std::wstring html = @@ -801,6 +801,13 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) browser->LoadString(html, L"about:blank"); } return 0; + case ID_TESTS_JAVASCRIPT_EXECUTE: // Test execution of javascript + if(browser.get()) + { + browser->ExecuteJavaScript(L"alert('JavaScript execute works!');", + L"about:blank", 0, CefBrowser::TF_MAIN); + } + return 0; case ID_TESTS_PLUGIN: // Test our custom plugin if(browser.get()) { @@ -811,6 +818,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) L""; browser->LoadString(html, L"about:blank"); } + return 0; } } break; diff --git a/tests/cefclient/cefclient.rc b/tests/cefclient/cefclient.rc index ed65a677d..fb83795dc 100644 --- a/tests/cefclient/cefclient.rc +++ b/tests/cefclient/cefclient.rc @@ -57,7 +57,8 @@ BEGIN END POPUP "Tests" BEGIN - MENUITEM "JavaScript", ID_TESTS_JAVASCRIPT + MENUITEM "JavaScript Handler", ID_TESTS_JAVASCRIPT_HANDLER + MENUITEM "JavaScript Execute", ID_TESTS_JAVASCRIPT_EXECUTE MENUITEM "Plugin", ID_TESTS_PLUGIN END END diff --git a/tests/cefclient/resource.h b/tests/cefclient/resource.h index 922cb099d..cc7fd3dba 100644 --- a/tests/cefclient/resource.h +++ b/tests/cefclient/resource.h @@ -21,8 +21,9 @@ #define IDC_NAV_FORWARD 201 #define IDC_NAV_RELOAD 202 #define IDC_NAV_STOP 203 -#define ID_TESTS_JAVASCRIPT 32771 -#define ID_TESTS_PLUGIN 32772 +#define ID_TESTS_JAVASCRIPT_HANDLER 32771 +#define ID_TESTS_JAVASCRIPT_EXECUTE 32772 +#define ID_TESTS_PLUGIN 32773 #define IDC_STATIC -1 #define IDS_LOGO 1000