我该如何重新加载的Greasemonkey脚本时AJAX改变无需重新加载页面的网址是什么? [英] How do I reload a Greasemonkey script when AJAX changes the URL without reloading the page?
问题描述
我有我试图创建一个的Greasemonkey脚本的应用程序。它采用了很多jQuery和AJAX的动态加载内容。
I have an application that I am trying to create a Greasemonkey script for. It utilizes a lot of jQuery and AJAX to load content dynamically.
我已经注意到,该URL的确实的改变,每次我打开一个新的项目,即使该网页不刷新。
I've noticed that the URL does change each time I load a new item, even though the page doesn't refresh.
有一个听众,我可以在页面上放置每次重新启动脚本中的URL发生变化?
Is there a listener I can place on the page to relaunch the script each time the URL changes?
推荐答案
您如何做到这一点取决于网站/应用程序,你正在努力做的事情。 下面是你的选择,最简单,最稳健的第一:
How you do this depends on the site/application and on what you are trying to do. Here are your options, easiest and most robust first:
-
不要试图赶上URL发生变化。 使用调用
waitForKeyElements()
以作用于部分,你想摆在首位操纵各种网页。这整齐的处理时间内在问题与所有其他方法的整体转换。
另请参阅:
Don't try to catch URL changes. Use calls to
waitForKeyElements()
to act on the parts of the various pages that you wanted to manipulate in the first place. This neatly handles a whole slew of timing issues inherent with all the other approaches.
See also: "Choosing and activating the right controls on an AJAX-driven site".
刚轮询URL的改变。 很简单,在实践中行之有效,用更少的时间问题,不是所有,但技术#1。
Just poll for URL changes. It's simple and works well in practice, with fewer timing issues than all but technique #1.
如果该网站使用AJAX的改变的片段的(又名哈希), < STRONG>火的 hashchange
事件 。唉,越来越少的AJAX网站做到这一点。
If the site uses AJAX that changes the fragment (AKA "hash"), fire on the hashchange
event. Alas, fewer and fewer AJAX sites do this.
使用突变观察员以监测变化的的&LT;冠军&GT;
代码。 最的AJAX页面都不够好,更改标题了。之前,你的目标内容被加载,尽管这可能会闪光。
Use Mutation Observers to monitor changes to the <title>
tag. Most AJAX pages are nice enough to change the title too. This may fire before your target content is loaded, though.
攻入 history.pushState
功能。 这给了更快的通知页面的变化,但95%的时候,它激发你的目标元素都加载之前。您通常还需要一个计时器。此外,它带来的交叉范围内的问题,在userscript环境。
Hack into the history.pushState
function. This gives faster notice of page changes, BUT 95% of the time, it fires before your target elements have loaded. You will usually still need a timer. Plus, it brings in cross-scope problems, in a userscript environment.
有关参考,在这里是一个查询的例子。它仍然是最好的,裸露的骨头,跨浏览器的,包罗万象的方法:
For reference, here is a polling example. It's still the best, bare-bones, cross-browser, catch-all method:
/*--- Note, gmMain () will fire under all these conditions:
1) The page initially loads or does an HTML reload (F5, etc.).
2) The scheme, host, or port change. These all cause the browser to
load a fresh page.
3) AJAX changes the URL (even if it does not trigger a new HTML load).
*/
var fireOnHashChangesToo = true;
var pageURLCheckTimer = setInterval (
function () {
if ( this.lastPathStr !== location.pathname
|| this.lastQueryStr !== location.search
|| (fireOnHashChangesToo && this.lastHashStr !== location.hash)
) {
this.lastPathStr = location.pathname;
this.lastQueryStr = location.search;
this.lastHashStr = location.hash;
gmMain ();
}
}
, 111
);
function gmMain () {
console.log ('A "New" page has loaded.');
// DO WHATEVER YOU WANT HERE.
}
这篇关于我该如何重新加载的Greasemonkey脚本时AJAX改变无需重新加载页面的网址是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!