javascript - How do I reload a Greasemonkey script when AJAX changes the URL without reloading the page? -
i have application trying create greasemonkey script for. utilizes lot of jquery , ajax load content dynamically.
i've noticed url does change each time load new item, though page doesn't refresh.
is there listener can place on page relaunch script each time url changes?
how depends on site/application , on trying do. here options, easiest , robust first:
don't try catch url changes. use calls
waitforkeyelements()
act on parts of various pages wanted manipulate in first place. neatly handles whole slew of timing issues inherent other approaches.
see also: "choosing , activating right controls on ajax-driven site".just poll url changes. it's simple , works in practice, fewer timing issues technique #1.
if site uses ajax changes fragment (aka "hash"), fire on
hashchange
event. alas, fewer , fewer ajax sites this.use mutation observers monitor changes to
<title>
tag. most ajax pages nice enough change title too. may fire before target content loaded, though.hack
history.pushstate
function. gives faster notice of page changes, 95% of time, fires before target elements have loaded. still need timer. plus, brings in cross-scope problems, in userscript environment.
for reference, here polling example. it's still best, bare-bones, cross-browser, catch-all method:
/*--- note, gmmain () fire under these conditions: 1) page loads or html reload (f5, etc.). 2) scheme, host, or port change. these cause browser load fresh page. 3) ajax changes url (even if not trigger 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.'); // whatever want here. }
Comments
Post a Comment