我正在開發一個應用程序Xamarin.UWP
,它試圖將Javascript注入到本地html文件(uri:ms-appdata:/// local/index。 HTML)像這樣:是否有腳本通知不適用於UWP ms-appdata的解決方法
async void OnWebViewNavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
if (args.IsSuccess)
{
// Inject JS script
if (Control != null && Element != null)
{
foreach (var f in Element.RegisteredFunctions.Where(ff => !ff.IsInjected))
{
await Control.InvokeScriptAsync("eval", new[] { string.Format(JavaScriptFunctionTemplate, f.Name) });
f.Injected();
}
}
}
}
然後,當Javascript方法被調用,這將調用OnWebViewScriptNotify
方法,這樣我可以proccess在我的應用程序的請求。
麻煩的是一些kind of security reasons這並不工作:
這是我們做了,我們已經有反饋,所以我們 重新評估它的政策決定。如果您將 NavigateToStreamUri與解析器對象一起使用,則相同的限制不適用。在內部, ms-appdata:///會發生什麼情況。
然後我試了一下在這種情況下,這是使用一個解析器這裏提到建議:https://stackoverflow.com/a/18979635/2987066
但是這會對性能產生巨大的影響,因爲它是所有的文件不斷地轉換成流加載,以及某些頁面加載不正確。
我又看了看使用AddWebAllowedObject
方法,像這樣:
private void Control_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
{
if (Control != null && Element != null)
{
foreach (var f in Element.RegisteredFunctions)
{
var communicator = new HtmlCommunicator(f);
Control.AddWebAllowedObject("HtmlCommunicator", communicator);
}
}
}
其中HtmlCommunicator
是:
[AllowForWeb]
public sealed class HtmlCommunicator
{
public JSFunctionInjection Function { get; set; }
public HtmlCommunicator(JSFunctionInjection function)
{
Function = function;
}
public void Fred()
{
var d = 2;
//Do something with Function
}
}
,並在我的HTML它就像這樣:
try { window.HtmlCommunicator.Fred(); } catch (err) { }
但這也不起作用。
那麼有沒有辦法解決這個荒謬的限制?
從[http://blog.damiendelaire.com/2016/06/communicating-back-and-forth-with.html](http://blog.damiendelaire.com/2016/06/communicating-back- and-forth-with.html)我讀到'因爲一些奇怪的原因,你需要添加在新的WRC庫中創建這個標記,並將這個類標記爲[AllowForWeb]'我將調查更多 – user1
我在上面的鏈接中注意到[此鏈接](https://www.suchan.cz/2016/01/hacking-uwp-webview-part-2-bypassing-window-external-notify-whitelist/)他們都使用'ms-app-web',所以我'我不確定最後一種方法是否適用於'ms-app-data'。將該課程放在另一個WRC庫中不起作用 – user1