2017-10-13 56 views
0

我想控制外部網站使用NW.js和webview,但任何時候我嘗試這個我有錯誤 - Angular沒有定義。NW.js - webview - Angular沒有定義錯誤

我來源:

的index.html

html...head...body... 
<webview id="webview" name="webview" src="https://google.com/" allownw></webview> 
<script src="../js/script.js"></script> 
...body...html 

的script.js

(function() { 
var gui = require('nw.gui'); 
var win = gui.Window.get(); 
var webview = document.getElementById("webview"); 

var tray = new gui.Tray({ 
    icon : 'assets/icon.png' 
}); 

var menu = new gui.Menu(); 

menu.append(new gui.MenuItem({ 
    type: 'normal', 
    label: '▶️ Play', 
    click: function() { 
     webview.executeScript({code:"var player=angular.element(document.body).injector().get('player'); player.play();"}); 
    } 
})); 
tray.menu = menu; 

}()); 

此代碼產生的錯誤我:的ReferenceError:角沒有定義

注意:網站google.com只是例子。

回答

0

nw.jsdocs

loading local files in webview

Add the following permission to the manifest:

"webview": { 
    "partitions": [ 
     { 
      "name": "trusted", 
      "accessible_resources": [ "<all_urls>" ] 
     } 
    ] 
    } 

and add 'partition="trusted"' attribute to the webview tag.

這是一個原因爲什麼<script標籤無法訪問您的文件../js/script.js。我還建議使用非本地文件的相對路徑示例:chrome-extension://yourdomain/js/script.js(並且它相對於根目錄不是「上」目錄)。

從谷歌的docs,您可以將accessible_resources設置爲特定的模式或文件。

insertCss,,permissionrequest事件,尤其是executeScript可能會引起您的興趣。

注意:您可能不希望給你的nwjs上下文(allownw)說的WebView訪問,因爲它可以訪問任何你的程序訪問。外部URL和http尤其適用,其中MITM篡改或第三方更改可能會導致某人獲得對您的應用程序運行的PC的訪問權限。

對於你的例子,我會保持script.js以外的web視圖,並通過腳本注入儀器webview。