NW.js - webview - Angular没有定义错误
问题描述:
我想控制外部网站使用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只是例子。
答
从nw.js
docs:
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。