使用 android ActionView Intent 打开时,Javascript 重定向在 Edge 浏览器中不起作用,但在手动重新加载后工作

标签 javascript android android-intent microsoft-edge

情况
在我们的 Android 应用程序 (Xamarin) 中,我们使用 ActionView Intent 打开一个网页。代码如下所示:

Intent intent = new Intent((string)Intent.ActionView, Android.Net.Uri.Parse(args.url));           
           
intent.AddFlags(ActivityFlags.NewTask);
打开的页面在某些时候会进行 JS 重定向,如下所示:
window.location = '...';
我们尝试了该行的许多不同变体,包括 window.location.href = '...' , window.location.assign('...');还有更多。都表现出相同的行为。
问题
多年来,这在所有浏览器中都运行良好 - 但现在我们遇到了一个问题,当 android 设备上的浏览器是 Edge 浏览器时:
当 Intent 最初打开浏览器选项卡时,window.location = '...' Javascript 中的 line 只是被浏览器忽略。没有错误消息 - 只是被忽略了。
但是,如果手动打开具有完全相同 URL 的相同浏览器选项卡(通过重新加载或通过复制和粘贴 URL),则 JS 重定向执行得很好。
问题
我们如何解决这个问题,我们如何使 JS 重定向可靠地工作?
我的猜测是我们遇到了一个安全功能,它可以防止 JS 在用户从未交互过的浏览器选项卡中重定向。
有什么东西(也许是一个 Intent 标志?)来规避这个?我们已经尝试过标志 GrantWriteUriPermission ,但没有帮助。
可能的重复
Android Browser Facebook Redirect Does Not Always Trigger Intent for URL :
建议在链接上设置 URL 并伪造点击的情况不起作用。

最佳答案

微软边缘安全
Microsoft Edge 最近修复了有关 XSS Targeting Non-Script Elements 的问题(2021 年 6 月 24 日)。
两名研究人员通过 Microsoft Edge 浏览器访问另一种语言的网站并尝试翻译该页面时发现了该漏洞。 Microsoft 最近修复的目标是避免有关从第三方应用程序动态访问内容的漏洞,特别是在浏览器重定向的情况下。他们需要迅速采取行动,因为漏洞非常大。

In order to mitigate a large class of potential cross-site scripting issues, the Microsoft Edge Extension system has incorporated the general concept of Content Security Policy (CSP)


好的,但是……有什么解决办法吗?
也许您可以找到解决您的问题的解决方案here ,特别是关于 <button onclick="..."> 的部分.
内联代码在 CSP 的概念中被认为是有害的,微软推荐了一些好的做法:

1 - The clickHandler definition must be moved into an external JavaScript

2 - The inline event handler definitions must be rewritten in terms of addEventListener and extracted into your external js file. If you are currently starting your program using code like <body onload="main();">, consider replacing it by hooking into the DOMContentLoaded event of the document, or the load event of the window, depending on your requirements. Use the former, since it generally triggers more quickly.

3 - Function inside onclick call must be rewritten to avoid converting the string of function into JavaScript for running.


文档中引用的外部 .js 文件的代码示例如下所示:
function awesome() {
// Do something awesome!
}

function totallyAwesome() {
// do something TOTALLY awesome!
}

function awesomeTask() {
    awesome();
    totallyAwesome();
}

function clickHandler(e) {
    setTimeout(awesomeTask, 1000);
}

function main() {
    // Initialization work goes here.
}

// Add event listeners once the DOM has fully loaded by listening for the
// `DOMContentLoaded` event on the document, and adding your listeners to
// specific elements when it triggers.
document.addEventListener('DOMContentLoaded', function () {
    document.querySelector('button').addEventListener('click', 
clickHandler);
    main();
});
希望它有帮助

关于使用 android ActionView Intent 打开时,Javascript 重定向在 Edge 浏览器中不起作用,但在手动重新加载后工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69176725/

相关文章:

javascript - PreventDefault() 不起作用

android - 如何更改 Android Studio/Gradle 中库的资源覆盖顺序?

android eclipse无法执行dex java堆空间错误

java - Zxing: Activity 获取结果

javascript - 如何在 Next.js 中为 material-ui 正确配置 babel?

javascript - 在 ASP.NET 项目中调试 javascript

javascript - 带有框架的 jQuery getScript 函数

android - Titanium.Platform.id 包含适用于 Android 和 iOS 的 UUID 或 UDID?

Android:如何同时清除所有应用程序的缓存

android-intent - Android Intents - SetDataAndType 与 setData 和 setType 之间的区别?