重定向第三方脚本的全局拦截器
异地第三方小部件的脚本之一在执行过程中中断,并将我主页上的所有流量重定向到他们的主页。
因为我无法杀死它们,所以我要么必须杀死脚本,要么找出一种方法来杀死第三方 JavaScript 可能进行的重定向浏览器的任何尝试。我发现的最有希望的事件是 .unload()
和 window.onbeforeunload
,我的想法是希望检查 GET 请求,然后验证 URL 是否不匹配我页面上的任何有效链接,这都没有问题,只是我不知道如何获取事件的 GET 值,因为它不绑定到页面上的任何点击或操作,而是第三方中的虚假内容或其他内容.js?
有人有幸找到类似问题的解决方案吗?
One of the scripts to an offsite third party widget broke during execution and redirected all traffic on my homepage to theirs.
Since I can't kill them, I'm either going to have to kill the script, or figure out a way to kill any attempt a third party JavaScript might make to redirect the browser. The most promising events I've found are .unload()
and window.onbeforeunload
, and my idea is to hopefully inspect the GET request and then validate the URL as not matching any of the valid links on my page, that's no problem except that I don't know how to get the event's GET value considering it's not bound to any clicks or action on the page, but a bogus something or other in a third party's .js?
Anybody have luck working out a solution to something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,没有办法拦截第 3 方 javascript 可能设置 window.location 导致重定向和预检的所有可能位置。它不是一个方法,它只是一个设置为没有方法可以替换/覆盖的属性。
页面卸载事件只是让您有机会询问用户是否真的想要离开此页面,但它无法访问新页面目标是什么。
我唯一能想到的就是保留一个包含默认为 false 的布尔值的全局变量。在您自己的代码中可能更改页面的所有可能位置,您都将该布尔值设置为 true。在 onbeforeunload 处理程序中,您检查该全局变量以查看它是否是您的代码所做的事情。如果这不是您的代码所做的,您会从 onbeforeunload 返回一条消息,以便提示用户是否确实要离开。但是,您无法阻止它(这是浏览器故意设计的)。如果他们对提示说“是”,他们会将页面移至新目的地。
如果您或其他人实际上可以找出第三者故意导致重定向的原因,那么也许您可以集中精力确保这种情况不会再次发生,但您没有向我们提供任何相关信息,因此我们无法确实有帮助。
There is no way that I know of to intercept all possible places that 3rd party javascript might set window.location causing a redirection and preflight it. It is not a method, it's just a property set to there's no method to replace/override.
The page unload event just gives you a chance to ask the user if they really want to leave this page, but it has no access to what the new page destination is.
The only thing I can think of is to keep a global variable that contains a boolean that defaults to false. In all possible places that you might change pages in your own code, you set that boolean to true. In the onbeforeunload handler, you check that global variable to see whether it's something your code did or not. If it is not something your code did, you return a message from onbeforeunload so the user will be prompted for whether they really want to leave or not. But, you can't prevent it (this is a browser design on purpose). If they say yes to the prompt, they will leave the page to the new destination.
If you or someone else could actually figure out why the 3rd party was inintentionally causing the redirection, then perhaps you could concentrate on making sure that condition doesn't happen again, but you've given us no info on that so we can't really help with that.