0

i want to make Greasemonkey script to replace text in html like this:

<img src="http://example.com/image.jpg" data-gifsrc="http://example.com/image.gif">

to

<img src="http://example.com/image.gif">

in website thechive.com

please help me, thanks.

anhmjn
  • 3
  • 2
  • 3
    Have you already tried something ? could you share what you have done so far to help other guide you in the right direction – dvhh Aug 31 '15 at 03:06
  • 1
    This is pretty easy to achieve in javascript, but you have to at least try something before we can help you. – PC Luddite Aug 31 '15 at 03:09
  • sorry, i've tried this `$("body").children().each(function () { $(this).html( $(this).html().replace('data-gifsrc','src') ); });` and it's not working. – anhmjn Aug 31 '15 at 03:19
  • Post an MCVE and link to a target page that actually has that structure. (Default thechive.com pages don't seem to.) – Brock Adams Aug 31 '15 at 03:31
  • here is it: [link](http://thechive.com/2015/08/28/animals-can-be-real-dckheads-sometimes-17-gifs/) thank you – anhmjn Aug 31 '15 at 03:34

1 Answers1

1

While I cannot find the data-gifsrc attribute on the mentioned website, youi cannot assume the JQuery would be present and available for Greasemonkey.

// attribute name
var attrName='data-gifsrc';

// list all images
var imgs=document.getElementsByTagName("img");

// loop every images
for(var i=0;i<imgs.length;i++) {
  // check for attribute
  if(imgs[i].attributes[attrName]) {
    // set image source
    imgs[i].src=imgs[i].attributes[attrName].value;
    // remove attribute
    imgs[i].attributes.removeNamedItem(attrName);
  }
}
dvhh
  • 4,724
  • 27
  • 33
  • here is a post has data-gifsrc attribute [link](http://thechive.com/2015/08/28/animals-can-be-real-dckheads-sometimes-17-gifs/) i've tried your script but it not working. i don't know why? – anhmjn Aug 31 '15 at 03:36
  • typo `lenght` instead of `length` – dvhh Aug 31 '15 at 03:51