Request: Background image to img

Subscribe to Request: Background image to img 5 posts, 3 voices

 
kankankan User

Hello,

I would like to have a script that makes this line:

<div style="position: relative; overflow: hidden; width:640px;height:480px;background:url('http://g1.someurl.com/mirror/9/000/681/90006813320.jpg');"><a href="/photos.php?username=something&image_id=2942668&folder_id=308050"><img src="/images/pixel.gif" style="border:none;" width="640" height="480" alt="" /></a></div>

to:

<div style="position: relative; overflow: hidden; width:640px;height:480px;"><a href="/photos.php?username=something&image_id=2942668&folder_id=308050"><img src="http://g1.someurl.com/mirror/9/000/681/90006813320.jpg" style="border:none;" width="640" height="480" alt="" /></a></div>

Possible? :D

 
FurYy Scriptwright

One possible way:

(function(){

	var divs=document.getElementsByTagName('div');

	for(var i=0;i<divs>-1){
				url=url.substr(url.indexOf("url")+4);
				url=url.substr(0,url.indexOf(")"));
				GM_log(url);
				GM_log(divs[i].getElementsByTagName('img')[0].src);
				divs[i].getElementsByTagName('img')[0].src=url;
			}
		}
	}
})();

divs[i].getElementsByTagName('img') should get imgs only from that DIV, though i have not used it that way :P

 
FurYy Scriptwright

I'm yet to discover a nicer way to get the url from style.background :P
[Note to self : there's Edit post too , doh]

 
LouCypher Scriptwright

var divs = document.evaluate("//div[contains(@style, 'background')]",
                             document, null, 6, null);
for (var i = 0; i < divs.snapshotLength; i++) {
  var div = divs.snapshotItem(i);
  var img = div.getElementsByTagName("img")[0];
  var src = div.style.backgroundImage.match(/\((\S+(?=\)))/)[1];
  div.style.backgroundImage = "none";
  img.src = src;
}

 
kankankan User

thanks!!! amazing what you people can do!:)