如何从HTML页面访问Gnome应用程序JavaScript函数?

How can i access to a Gnome application JavaScript function from a HTML page?

本文关键字:应用程序 Gnome JavaScript 函数 访问 HTML      更新时间:2023-09-26

我学习了如何将HTML页面嵌入到gnome JavaScript应用程序中,并在Gtk+框架中显示它。

// Create a webview to show the web app
this._webView = new Webkit.WebView ();
// Put the web app into the webview
this._webView.load_uri (GLib.filename_to_uri (GLib.get_current_dir() +
    "/hellognome.html", null));
// Put the webview into the window
this._window.add (this._webView);
// Show the window and all child widgets
this._window.show_all();

现在我要访问Gtk+ API(如访问文件系统)或JavaScript函数在我的gnome JavaScript应用程序从这个HTML (hellognome.html)文件使用JavaScript?

像这样:

<html>
    <head>
        <title>Hello, GNOME!</title>
        <!-- Use JavaScript to show a greeting when someone clicks the button -->
        <script type="application/javascript">
        function greeting () {
            document.getElementById ("greeting").innerHTML = ("O hai!");
            //// access to a Gtk+ API (like accessing to file system ) 
                 or a JavaScript function in my gnome JavaScript application.
        }
        </script>
    </head>
    <body>
        <br /> <br />
        <button type="button" onclick="greeting()">Hello, GNOME!</button>
        <!-- Empty H1 element gets filled in when the button is clicked -->
        <h1 id="greeting"></h1>
    </body>
</html>

你不能在HTML中的JS中使用GTK api。您必须在两者之间建立某种通信方法。例如,连接window-object-cleared信号,在窗口对象上设置一些方法。