1

I want to write a greasemonkey script/chrome user script that takes any images with a certain syntax and replace a section of the url and puts it back in the dom.

Namely I have a page full of thumbnails, e.g. www.example.com/small/randomstuff and I want to replace the imades with the equivilant www.example.com/large/randomstuff

How would I do this?

2 Answers 2

6

After page was loaded.

var imgs = document.getElementsByTagName('img');
var len =imgs.length;
for(var i=0;i<len;i++){
  imgs[i].src=imgs[i].src.replace("small","large");
}
Sign up to request clarification or add additional context in comments.

6 Comments

actually, this is the more correct answer, as the OP wants all thumbnails replaced
You can use the alternative for syntax, and avoid having to create the len variable, by doing for(var i in imgs).
@Atli Assuming you will use in the future some kind of library (like JQ or Mootools) this will make your code less portable, as those libraries add properties to the Array object.
@Atli: I've always read that you should avoid the for( x in y ) loop in JavaScript because it's quite inefficient. Maybe not a big deal here, but... yeah.
You definitely don't want for...in here. Quite apart from the prototype-extension issue with libraries, it will give you other non-numeric properties of NodeList, like item, namedItem and length. Don't use for...in against Array or other sequences, it is always the wrong thing. Incidentally you can use document.images as an old-school short-cut for the getElementsByTagName call.
|
2

To replace a image source url, you can do something like this:

var imgs = document.getElementsByTagName('img')
imgs[0].src = "http://www.example.com/large/randomstuff"

1 Comment

I gave you an upvote as it does the meat of the work, but Itay Moavs answer does everything

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.