如何在 Xamarin.Forms 的 webview 中设置用户代理
我的程序中使用的页面之一需要 UA 包含字符串“weishao”,我尝试使用下面的 JavaScript 来更改 UA 但它不起作用。
var customUserAgent = 'Mozilla/5.0 (Linux; Android 10; EBG-AN00 Build/HUAWEIEBG-AN00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/83.0.4103.106 Mobile Safari/537.36 weishao(3.2.2.74616)';
Object.defineProperty(navigator, 'userAgent', {
value: customUserAgent,
writable: false
});
页面的检测器是:
function is_weixin() {
var href = window.location.href;
if (href.indexOf("errorPage") != -1){
return true;
}
var ua = navigator.userAgent.toLowerCase();
var isWeixin = ua.indexOf('micromessenger') != -1;
var weishao = ua.indexOf('weishao') != -1;
if (isWeixin){
return true;
}else if( weishao) {
return true;
}else if(dd.env.platform != "notInDingTalk") {
return true;
}else{
alert("Not allowed!");
var openid = $("#openid").val();
if (dkywcommon.isEmpty(openid)) {
openid = "";
}
window.location.href = "/errorPage?openid="+openid;
return true;
}
}
One of the pages used in my Program needs the UA contains the string "weishao", I have tried to use javascript below to change the UA but it is not working.
var customUserAgent = 'Mozilla/5.0 (Linux; Android 10; EBG-AN00 Build/HUAWEIEBG-AN00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/83.0.4103.106 Mobile Safari/537.36 weishao(3.2.2.74616)';
Object.defineProperty(navigator, 'userAgent', {
value: customUserAgent,
writable: false
});
The detector of the page is:
function is_weixin() {
var href = window.location.href;
if (href.indexOf("errorPage") != -1){
return true;
}
var ua = navigator.userAgent.toLowerCase();
var isWeixin = ua.indexOf('micromessenger') != -1;
var weishao = ua.indexOf('weishao') != -1;
if (isWeixin){
return true;
}else if( weishao) {
return true;
}else if(dd.env.platform != "notInDingTalk") {
return true;
}else{
alert("Not allowed!");
var openid = $("#openid").val();
if (dkywcommon.isEmpty(openid)) {
openid = "";
}
window.location.href = "/errorPage?openid="+openid;
return true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
xamarin 目前不支持在 webview 上自定义 user-agent,但如果您只是在寻找 xamarin.android 的解决方法,您可以参考下面的代码。
请参阅
https://github.com/xamarin/Xamarin.Forms/issues/8432< /a> .
Customizing user-agent on webview is not supported currently in xamarin, but if you're just looking for the workaround for xamarin.android , you can refer to the code below .
Refer to
https://github.com/xamarin/Xamarin.Forms/issues/8432 .