Changing links after AJAX call

78 views
Skip to first unread message

PhillySaxon

unread,
Sep 10, 2009, 12:46:56 PM9/10/09
to greasemonkey-users
I'm having some trouble calling my script after a page makes an AJAX
call to load a new page.

Basically, I'm trying to create a script for http://4walled.org/ that
changes a portion of the links on the search page from "edit.php?
filename=" to simply "src/", so the links link straight to the image
instead of the meaningless "edit" page, with the direct link on it.

The script works just fine on the first set of images, but when you
scroll to the bottom of the page the site makes a jQuery call to load
a new page dynamically. The result, the first set of links are just
fine with "http://4walled.org/src/image.jpg", but the dynamically
loaded links are still linking to "http://4walled.org/edit.php?
filename=image.jpg". I know I need to call the script again after the
AJAX call, but I'm not sure exactly how.

Here it is so far (sorry if the code's sloppy):


// ==UserScript==
// @name 4walled Direct Links
// @namespace about:blank
// @description Changes links in the 4walled gallery to direct links.
By PhillySaxon.
// @include http://4walled.org/*
// ==/UserScript==

function convertLinks() {
var allLinks = document.getElementsByTagName('a')

for(var i=0; i < allLinks.length; i++) {
if (allLinks[i].href.match('/?edit')){
allLinks[i].href = allLinks[i].href.replace('/edit.php?filename=','/
src/');
allLinks[i].href = allLinks[i].href += '';}
}
}

convertLinks();

Erik Vold

unread,
Sep 10, 2009, 10:23:32 PM9/10/09
to greasemonkey-users
Hey,

What you want to do is add a "DOMNodeInserted" event listener to the
document, or an element lower down in the tree containing the updated
search results, which will check for any results that exist that
haven't already been updated by your script. For the last bit I
usually add a custom attribute to some tag when I'm done, so that if I
have to check again I can easily tell that my work is done.

This http://userscripts.org/scripts/review/56369 is a userscript I
wrote for Google Analytics, which adds a "DOMNodeInserted" event
listener to the document and checks if a piece of text way at the
bottom of the table (which is updated by ajax) has a link by checking
for the existence of an attribute on a particular node; it should be
similar in outline to what you want to do.

Erik

Anthony Lieuallen

unread,
Sep 10, 2009, 11:04:26 PM9/10/09
to greasemon...@googlegroups.com
On 9/10/2009 12:46 PM, PhillySaxon wrote:
> I'm having some trouble calling my script after a page makes an AJAX
> call to load a new page.

This exact question was asked not long ago:

http://groups.google.com/group/greasemonkey-users/t/218cd7561e5f79b4

Reply all
Reply to author
Forward
0 new messages