Add API to execute javascript in devtools.

This commit is contained in:
Cheng Zhao 2014-04-08 12:50:12 +08:00
parent a50d3bde5c
commit d7a54cf3a6
4 changed files with 23 additions and 4 deletions

View file

@ -516,6 +516,18 @@ void Window::GetDevTools(const v8::FunctionCallbackInfo<v8::Value>& args) {
args.GetReturnValue().Set(devtools); args.GetReturnValue().Set(devtools);
} }
// static
void Window::ExecuteJavaScriptInDevTools(
const v8::FunctionCallbackInfo<v8::Value>& args) {
UNWRAP_WINDOW_AND_CHECK;
std::string code;
if (!FromV8Arguments(args, &code))
return node::ThrowTypeError("Bad argument");
self->window_->ExecuteJavaScriptInDevTools(code);
}
// static // static
void Window::LoadURL(const v8::FunctionCallbackInfo<v8::Value>& args) { void Window::LoadURL(const v8::FunctionCallbackInfo<v8::Value>& args) {
UNWRAP_WINDOW_AND_CHECK; UNWRAP_WINDOW_AND_CHECK;
@ -700,6 +712,8 @@ void Window::Initialize(v8::Handle<v8::Object> target) {
NODE_SET_PROTOTYPE_METHOD(t, "isCrashed", IsCrashed); NODE_SET_PROTOTYPE_METHOD(t, "isCrashed", IsCrashed);
NODE_SET_PROTOTYPE_METHOD(t, "getDevTools", GetDevTools); NODE_SET_PROTOTYPE_METHOD(t, "getDevTools", GetDevTools);
NODE_SET_PROTOTYPE_METHOD(
t, "executeJavaScriptInDevTools", ExecuteJavaScriptInDevTools);
NODE_SET_PROTOTYPE_METHOD(t, "loadUrl", LoadURL); NODE_SET_PROTOTYPE_METHOD(t, "loadUrl", LoadURL);
NODE_SET_PROTOTYPE_METHOD(t, "getUrl", GetURL); NODE_SET_PROTOTYPE_METHOD(t, "getUrl", GetURL);

View file

@ -105,6 +105,8 @@ class Window : public EventEmitter,
// APIs for devtools. // APIs for devtools.
static void GetDevTools(const v8::FunctionCallbackInfo<v8::Value>& args); static void GetDevTools(const v8::FunctionCallbackInfo<v8::Value>& args);
static void ExecuteJavaScriptInDevTools(
const v8::FunctionCallbackInfo<v8::Value>& args);
// APIs for NavigationController. // APIs for NavigationController.
static void LoadURL(const v8::FunctionCallbackInfo<v8::Value>& args); static void LoadURL(const v8::FunctionCallbackInfo<v8::Value>& args);

View file

@ -225,6 +225,11 @@ void NativeWindow::InspectElement(int x, int y) {
agent->InspectElement(x, y); agent->InspectElement(x, y);
} }
void NativeWindow::ExecuteJavaScriptInDevTools(const std::string& script) {
GetDevToolsWebContents()->GetRenderViewHost()->ExecuteJavascriptInWebFrame(
string16(), base::UTF8ToUTF16(script));
}
void NativeWindow::FocusOnWebView() { void NativeWindow::FocusOnWebView() {
GetWebContents()->GetRenderViewHost()->Focus(); GetWebContents()->GetRenderViewHost()->Focus();
} }
@ -612,10 +617,7 @@ void NativeWindow::CallDevToolsFunction(const std::string& function_name,
} }
} }
} }
base::string16 javascript = ExecuteJavaScriptInDevTools(function_name + "(" + params + ");");
base::ASCIIToUTF16(function_name + "(" + params + ");");
GetDevToolsWebContents()->GetRenderViewHost()->ExecuteJavascriptInWebFrame(
string16(), javascript);
} }
void NativeWindow::OnRendererMessage(const string16& channel, void NativeWindow::OnRendererMessage(const string16& channel,

View file

@ -136,6 +136,7 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
virtual void CloseDevTools(); virtual void CloseDevTools();
virtual bool IsDevToolsOpened(); virtual bool IsDevToolsOpened();
virtual void InspectElement(int x, int y); virtual void InspectElement(int x, int y);
virtual void ExecuteJavaScriptInDevTools(const std::string& script);
virtual void FocusOnWebView(); virtual void FocusOnWebView();
virtual void BlurWebView(); virtual void BlurWebView();