【问题标题】:GreaseMonkey script to rewrite image links [duplicate]重写图像链接的 GreaseMonkey 脚本[重复]
【发布时间】:2011-06-22 12:01:12
【问题描述】:

可能重复:
How to replace image links with img src url in Greasemonkey

我是 javacript 新手,想编写一个 GreaseMonkey 脚本来重写网站上的某些图像链接,以显示原始大小的图像而不是缩略图。

图片链接格式如下:

<div class="feed_img"><a onclick="App.scaleImg(this,'7e87e5d5tw1difluxvqlzj');" href="javascript:;"><img src="http://ww3.sinaimg.cn/thumbnail/7e87e5d5tw1difluxvqlzj.jpg" class="imgicon" vimg="1"></a></div>

我想做的是替换

http://*/thumbnail/*.jpg 

http://*/large/*.jpg

谁能给我指导一下?

【问题讨论】:

  • 到目前为止你尝试过什么?搏一搏。如果您是 JavaScript 新手,这是一个很好的练习,如果您自己拼凑一个解决方案,您将学到更多。

标签: javascript firefox replace greasemonkey hyperlink


【解决方案1】:

您必须遍历所有 &lt;img&gt; 标记,并在其 src 属性中将 thumbnail 替换为 large

// This fetches all of the <img> tags and stores them in "tags".
var tags = document.getElementsByTagName('img');

// This loops over all of the <img> tags.
for (var i = 0; i < tags.length; i++) {

  // This replaces the src attribute of the tag with the modified one
  tags[i].src = tags[i].src.replace('thumbnail', 'large');
}

希望此代码有效。我使用的是基本的replace(),所以如果您想尝试正则表达式,我认为它也应该可以工作。

【讨论】:

  • 对我来说看起来不错,但我不认为在这种情况下使用正则表达式没有任何好处。
  • 这将清除页面上的所有图像!我想 OP 只想篡改“有效负载”图像。
  • @Brock:您可以添加一些检查以筛选出不需要的链接。我不是在为 OP 编写 all 代码...
  • 为什么不呢?更正确的代码只需要更改第一行。我会自己做,但问题是重复的,而且 OP 显然甚至没有进行粗略的搜索。
  • @Blender,我在脚本中添加了 DOMContentLoaded 事件,它并没有改变任何东西。我在某处读到 GM 脚本默认在 DOMContentLoaded 处运行,是真的吗?会不会是因为当 DOM 准备好时脚本会立即运行,但它并没有中止下载外部资源的操作?替换过程真的很短,比使用“加载”事件要早一点开始。
猜你喜欢
  • 2012-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多