我正在寻找一个氧化物等效于这个QtWebKit函数:
WebView.experimental.evaluateJavaScript()或WebKitGtk
WebView.execute_script()我很难找到关于氧化物的文件。这篇文章有很好的信息,但并不是我想要做的:http://daker.me/2014/05/how-to-use-oxide-in-your-ubuntu-qml-application.html
发布于 2014-07-11 18:37:01
没有内置的等效项,但是可以通过在用户脚本中设置消息处理程序,在HTML文档中处理的DOM中触发事件来再现此行为。对于这些步骤中的每一个,都会传递要执行的代码。举一个简单的例子:
import QtQuick 2.0
import Ubuntu.Components 0.1
import com.canonical.Oxide 1.0
Rectangle {
width: units.gu(50)
height: units.gu(75)
// Both the UserScript and the call to sendMessage need to share the same
// context, which should be in the form of a URL. It doesn't seem to matter
// what it is, though.
property string usContext: "messaging://"
WebView {
id: webview
anchors {
top: parent.top
left: parent.left
right: parent.right
bottom: button.top
}
context: webcontext
url: Qt.resolvedUrl("oxide-test.html")
function executeJavascript(code) {
var req = rootFrame.sendMessage(usContext, "EXECUTE", {code: code});
}
}
WebContext {
id: webcontext
userScripts: [
UserScript {
context: usContext
url: Qt.resolvedUrl("oxide-user.js")
}
]
}
Button {
id: button
anchors {
bottom: parent.bottom
left: parent.left
right: parent.right
}
text: "Press Me"
onClicked: webview.executeJavascript("exampleFunc('Hello');")
}
}oxide.addMessageHandler("EXECUTE", function (msg) {
var event = new CustomEvent("ExecuteJavascript", {detail: msg.args.code});
document.dispatchEvent(event);
});<html>
<head>
<script>
document.addEventListener("ExecuteJavascript", function (event) { eval(event.detail); });
function exampleFunc(message) {
document.body.innerHTML += "<p>" + message + "</p>";
}
</script>
</head>
<body>
</body>
</html>(请注意,如果您所要做的只是操作DOM,那么您可以在用户脚本中这样做。)
这段代码不允许您获得执行的结果。您可能可以传递一个回调,但我怀疑这实际上是行不通的。相反,您需要设置一个并行消息传递链,以便将结果返回到QML上下文。
https://askubuntu.com/questions/492501
复制相似问题