【问题标题】:Greasemonkey script to replace text in html [duplicate]Greasemonkey脚本替换html中的文本[重复]
【发布时间】:2015-08-31 11:55:18
【问题描述】:

我想制作 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

请帮帮我,谢谢。

【问题讨论】:

  • 你已经尝试过了吗?您能否分享您迄今为止所做的工作以帮助其他人指导您正确的方向
  • 这在 javascript 中很容易实现,但您至少必须尝试一下,我们才能为您提供帮助。
  • 对不起,我试过这个$("body").children().each(function () { $(this).html( $(this).html().replace('data-gifsrc','src') ); });,但它不起作用。
  • 发布 MCVE 并链接到实际具有该结构的目标页面。 (默认 thechive.com 页面似乎没有。)
  • 这里是:link 谢谢

标签: greasemonkey


【解决方案1】:

虽然我在上述网站上找不到 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);
  }
}

【讨论】:

  • 这里有一个帖子有 data-gifsrc 属性link 我试过你的脚本,但它不起作用。不知道为什么?
  • 错字lenght 而不是length
猜你喜欢
  • 2014-12-31
  • 2015-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多