2013-01-19 13 views
1

我瞭解到,how to embedded a HTML page to a gnome JavaScript應用程序,並顯示在一個Gtk +框架。如何從HTML頁面訪問Gnome應用程序的JavaScript函數?

// 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從這個HTML(hellognome.html)文件的JavaScript應用程序在我的gnome 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> 

在此先感謝。

回答

2

您不能在HTML中使用JS內部的GTK API。你必須在兩者之間建立一些溝通方法。例如,連接到window-object-cleared信號並在窗口對象上設置一些方法。

+0

你能舉一個例子嗎? –

+0

不在Javascript中,但這裏是C中的一個例子:https://github.com/ptomato/gnome-inform7/blob/master/src/panel.c在'i7_panel_init'中,在533行,我創建了一個JS類與我想從HTML內的JS代碼調用的方法一起使用。然後在846行的'window-object-cleared'處理程序中,我將該類設置爲窗口對象的屬性。這些方法的實現是在文件開始的第50行和之後的'js_'函數。 – ptomato

+0

@ptomato,你知道這樣的事情是否可以使用Python Gtk綁定?當我嘗試實現'window-object-cleared'時,它實際上會崩潰解釋器... –

相關問題