Greasemonkey 脚本替换 html 中的文本

Greasemonkey script to replace text in html

我想制作 Greasemonkey 脚本来替换 html 中的文本,如下所示:

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

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

在网站上 thechive.com

请帮助我,谢谢。

虽然我在提到的网站上找不到 data-gifsrc 属性,但您不能假设 JQuery 存在并且可用于 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);
  }
}