gpt4 book ai didi

javascript - 为什么 greasemonkey 检测不到 facebook 中的某些页面更改?

转载 作者:数据小太阳 更新时间:2023-10-29 05:48:48 25 4
gpt4 key购买 nike

我试图在 facebook 中对/messages 页面执行 user.js,但当导航从/更改为/messages 时,greasemonkey 似乎没有注意到。它也发生在其他内部页面中。一开始我以为是AJAX导航引起的,但是URL变了(不是hash部分),所以是正常导航吧?

这是我用过的测试页:

// ==UserScript==
// @name Test
// @namespace none
// @description just an alert when page changes
// @include http*://www.facebook.com/*
// ==/UserScript==

alert(location.href);

如何正确检测页面变化?


火狐版本:6.0.2

Greasemonkey 版本:0.9.11

最佳答案

对于支持它的浏览器,包括 Firefox 4+,Facebook 利用了 HTML5 History API。此 API 允许使用 history.pushState() 方法更改位置,但实际上并未发生导航。尽管该页面可能看起来已经改变,但所发生的只是一个改变了大部分内容的幕后 ajax 调用。

如果您想捕获此更改,则必须使用您自己的函数代理 pushState() 方法:

(function (old) {
window.history.pushState = function () {
old.apply(window.history, arguments);
alert(window.location.href);
}
})(window.history.pushState);

https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history 阅读有关历史 API 的更多信息.

关于javascript - 为什么 greasemonkey 检测不到 facebook 中的某些页面更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7381367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com