3

I used Qt before Golang, I can use javascript in Qt like this:

QWebView* view = new QWebView(parent);
view->load(QUrl("http://www.example.com"));
QString cmd("example.value = \"test\""); //using javascript to fill value
view->page()->mainFrame()->evaluateJavaScript(cmd);

QString cmd2("document.forms[\"Form1\"].submit()");//using javascript to submit a Form
QVariant result = view->page()->mainFrame()->evaluateJavaScript(cmd2);
qDebug() << result.toString(); //get javascript return data

How can I use javascript in Golang to get a specific website elements or submit forms.

Green Bird
  • 179
  • 2
  • 6

2 Answers2

2

How can I use javascript in Golang

You can use gopherjs, a compiler from Go to JavaScript.

You can see an example in the tidwall/digitalrain project, where the digitalrain.go file access html elements

js.Global.Get("document").Get("head").Call("appendChild", sheet)
js.Global.Get("document").Set("title", "whoa")

(for a great effect)

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Is there any example to get elements or submit forms from a target URL – Green Bird Feb 08 '15 at 11:28
  • @GreenBird not directly from the gpoherjs project itself (https://github.com/gopherjs/gopherjs/search?q=post&type=Code&utf8=%E2%9C%93) – VonC Feb 08 '15 at 11:33
  • @GreenBird although my older answer http://stackoverflow.com/a/27034623/6309 is about POST. – VonC Feb 08 '15 at 12:37
  • I found [this](https://github.com/sourcegraph/webloop), it's not as easy as I thought since I am using windows. – Green Bird Feb 08 '15 at 13:12
1

If you like to run JS code in Go, try Otto (https://github.com/robertkrimen/otto), it's a JS VM written in Go and very powerful.

user3162144
  • 101
  • 2