如何在JavaScript中注册window.external.notify事件处理程序

4

我正在尝试在HTML/JavaScript应用程序中使用Azure访问控制服务。下面的代码示例将在选择身份提供者进行身份验证后显示令牌:

<!DOCTYPE html>
<html>
<head>
</head>

<body>
<script type="text/javascript">
    function handleData (data){
    
        document.getElementById('tokenDiv').innerHTML = "Token = " + data;
    }
    function chooseIdentityProvider ( url ){
    
        document.getElementById('loginFrame').src = url;            
    }
    // HOW TO register notify event handler?    
    window.external.notify = handleData;        
</script>
<select name="AvailableProviders" onchange="if (this.selectedIndex) chooseIdentityProvider (this.value)">
    <option value="-1">Select Identity Provider</option>
    <option value="https://login.live.com/login.srf?wa=wsignin1.0&wtrealm=https%3a%2f%2faccesscontrol.windows.net%2f&wreply=https%3a%2f%2fnamespace.accesscontrol.windows.net%3a443%2fv2%2fwsfederation&wp=MBI_FED_SSL&wctx=pr%3djavascriptnotify%26rm%3duri%253awk%253atest">Windows Live ID</option>  
    <option value="https://www.google.com/accounts/o8/ud?openid.ns=http%3a%2f%2fspecs.openid.net%2fauth%2f2.0&openid.mode=checkid_setup&openid.claimed_id=http%3a%2f%2fspecs.openid.net%2fauth%2f2.0%2fidentifier_select&openid.identity=http%3a%2f%2fspecs.openid.net%2fauth%2f2.0%2fidentifier_select&openid.realm=https%3a%2f%2fnamespace.accesscontrol.windows.net%3a443%2fv2%2fopenid&openid.return_to=https%3a%2f%2fnamespace.accesscontrol.windows.net%3a443%2fv2%2fopenid%3fcontext%3dpr%253djavascriptnotify%2526rm%253duri%25253awk%25253atest%26provider%3dGoogle&openid.ns.ax=http%3a%2f%2fopenid.net%2fsrv%2fax%2f1.0&openid.ax.mode=fetch_request&openid.ax.required=email%2cfullname%2cfirstname%2clastname&openid.ax.type.email=http%3a%2f%2faxschema.org%2fcontact%2femail&openid.ax.type.fullname=http%3a%2f%2faxschema.org%2fnamePerson&openid.ax.type.firstname=http%3a%2f%2faxschema.org%2fnamePerson%2ffirst&openid.ax.type.lastname=http%3a%2f%2faxschema.org%2fnamePerson%2flast">Google</option>
    <option value="https://open.login.yahooapis.com/openid/op/auth?openid.ns=http%3a%2f%2fspecs.openid.net%2fauth%2f2.0&openid.mode=checkid_setup&openid.claimed_id=http%3a%2f%2fspecs.openid.net%2fauth%2f2.0%2fidentifier_select&openid.identity=http%3a%2f%2fspecs.openid.net%2fauth%2f2.0%2fidentifier_select&openid.realm=https%3a%2f%2fnamespace.accesscontrol.windows.net%3a443%2fv2%2fopenid&openid.return_to=https%3a%2f%2fnamespace.accesscontrol.windows.net%3a443%2fv2%2fopenid%3fcontext%3dpr%253djavascriptnotify%2526rm%253duri%25253awk%25253atest%26provider%3dYahoo!&openid.ns.ax=http%3a%2f%2fopenid.net%2fsrv%2fax%2f1.0&openid.ax.mode=fetch_request&openid.ax.required=email%2cfullname%2cfirstname%2clastname&openid.ax.type.email=http%3a%2f%2faxschema.org%2fcontact%2femail&openid.ax.type.fullname=http%3a%2f%2faxschema.org%2fnamePerson&openid.ax.type.firstname=http%3a%2f%2faxschema.org%2fnamePerson%2ffirst&openid.ax.type.lastname=http%3a%2f%2faxschema.org%2fnamePerson%2flast">Yahoo!</option>"
</select> 
<br />
<iframe id="loginFrame" height="200" width="60%" src="" />
<br />
<div id="tokenDiv"></div>
</body>
</html>
以下是需要翻译的内容:

iFrame的内容如下:

<script type="text/javascript">
try{
    window.external.notify(...token...);
}
catch(err){
    alert("Error ACS50021: windows.external.Notify is not registered.");
}
</script>

我遇到了一个错误:

"错误 ACS50021:未注册 windows.external.Notify。"

我该如何在 JavaScript 中注册 notify 事件处理程序?


这是在浏览器中还是在“浏览器控件”中? - Diodeus - James MacFarlane
这是一个浏览器,可以在手机设备上通过PhoneGap包装的浏览器控件中使用。 - Glaxalg
2
我认为浏览器不支持这个。我认为只有.NET中的浏览器控件支持它。 - Diodeus - James MacFarlane
2个回答

2
你是否已经启用了允许接收消息的URL,类似于这样:
            // code needed -> allowed parameter to check all URL
        var unloadFunc = "(function(){ function navigating(){ window.external.notify('%%' + location.href);} window.onbeforeunload=navigating;return location.href;})();";
        var host = wv.InvokeScript("eval", new string[] { unloadFunc });
        wv.AllowedScriptNotifyUris = new[] { new Uri(host) };

wv 是 XAML 中的 webview 元素。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接