Reputation: 33
I want to create a script that will search the entire page for an image called 'orange.png'
It would run onload and need to only run once. If it finds said image it would prompt an alert box stating it found the image.
I have partial code, but its not properly finding the image. Most of this code was just cut and pasted from other sources.
function findimg()
{
var imgs,i;
imgs=document.getElementsByTagName('img');
for(i in imgs)
{
if(/orange.png/.test(imgs[i].src))
{
alert("Image Found");
}
}
}
Edit: Im an idiot. I was making my function and was never calling it, thus it never ran. Anyway, Brock your solution works great! Thanks very much!
Upvotes: 3
Views: 246
Reputation: 93473
Your code would have worked but for Greasemonkey's sandbox, see: Greasemonkey Pitfalls. In GM, collections can't be iterated that way.
This code will work:
function findimg()
{
var orangeImages = document.querySelectorAll ('img[src*="orange.png"]')
if (orangeImages.length)
{
alert("Image Found");
}
}
Upvotes: 1
Reputation: 17507
Dive Into Greasemonkey has a chapter of common patterns. It includes your problem as well. Despite the warning about old content, the search for an element containing a specific attribute doesn't get old that easily!
Upvotes: 0