如何在Greasemonkey中用img src url替换图像链接 [英] How to replace image links with img src url in Greasemonkey
本文介绍了如何在Greasemonkey中用img src url替换图像链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从标题中可能听起来像是一个重复的问题。但我要求的是帮助编写一个Greasemonkey脚本,该脚本在src url中包含所有包含缩略图一词的图像,用images替换缩略图,然后将新url放入href(目标)url。 / p>
到目前为止我所拥有的是:
for(var iImage = 0; iImage< document.images.length; iImage ++){
var imageUrl = document.images [iImage] .src;
if(imageUrl.indexOf(thumbnails)!= -1){
imageUrl = imageUrl.replace(缩略图,图片)
document.images [ iImage] .href = imageUrl;
}
}
任何帮助都将不胜感激。
解决方案
img标签不能有href,但你可以将它们附加到带有href属性的锚标签中:
for(var iImage = 0; iImage< document.images.length; iImage ++){
var imageUrl = document.images [iImage] .src;
if(imageUrl.indexOf(thumbnails)!= -1){
imageUrl = imageUrl.replace(thumbnails,images);
document.images [iImage] .outerHTML ='< a href ='+
+ imageUrl +'>'
+ document.images [iImage] .outerHTML +'< ; / A>';
}
}
From the title this may sound like a duplicate question. But what I am asking for is help writing a Greasemonkey script that takes all images containing the word "thumbnails" in the src url, replaces "thumbnails" with "images" but then putting the new url into the href (target) url.
What I have so far is:
for(var iImage=0;iImage<document.images.length;iImage++){
var imageUrl = document.images[iImage].src;
if (imageUrl.indexOf("thumbnails") != -1) {
imageUrl = imageUrl.replace("thumbnails","images")
document.images[iImage].href = imageUrl;
}
}
Any help would be appreciated.
解决方案
img tags cant have href, however you can append them into an anchor tag with href attribute:
for(var iImage=0;iImage<document.images.length;iImage++){
var imageUrl = document.images[iImage].src;
if (imageUrl.indexOf("thumbnails") != -1) {
imageUrl = imageUrl.replace("thumbnails","images");
document.images[iImage].outerHTML = '<a href ="' +
+ imageUrl + '" >'
+ document.images[iImage].outerHTML + '</a>';
}
}
这篇关于如何在Greasemonkey中用img src url替换图像链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文