替换所有图像的图像源
Replace images source for all images
我想使用 Greasemonkey 脚本替换与正则表达式匹配的所有图像的 src
属性。例:
替换http://aff.kooora.com/i/z3.gif
与http://example.com/aff.kooora.com/i/z3.gif
我没有脚本编写经验,但这就是我想出的:
var allImg=document.getElementsByTagName("img"), i=0, img;
var pattern = aff'.kooora'.com/([A-Za-z0-9/._]*);
while(img=allImg[i++])
{
result = pattern.exec(img);
if (result!=null)
img.src=img.src.replace(result, 'http://example.com/' + result);
}
它不起作用。
你的正则表达式是问题所在,你需要用斜杠包装它(否则解释器不知道它应该将其视为正则表达式):
var allImg=document.getElementsByTagName("img"), i=0, img;
var pattern = /^http':'/'/aff'.kooora'.com'/(.*)$/;
while (img = allImg[i++])
{
if (img.src.match(pattern)) {
img.src = img.src.replace(pattern, 'http://example.com/aff.kooora.com/$1');
}
}
只是一个替代方案。
在这个 Greasemonkey 脚本 ProxyLoadIMG 中,这是在不使用正则表达式的情况下完成的。
它被创建为通过代理加载图像。
只有图像。链接和其他内容保持不变。
var proxy = 'https://proxytea.appspot.com/'
for (var x=0; x<document.images.length; x++) {
imagem = document.images[x];
imagem.src = imagem.src.replace(location.protocol + '//', proxy);
}
http://aff.kooora.com/i/any.jpg 变得 https://proxytea.appspot.com/aff.kooora.com/i/any.jpg
http://aff.kooora.com/i/z3.gif 变得 https://proxytea.appspot.com/aff.kooora.com/i/z3.gif
等等...
无需正则表达式:)
使用这个:
function replaceImages(oldUrl, newUrl) {
var imgs = document.getElementsByTagName('img')
for (i = 0; i<imgs.length; i++) {
imgs[i].src = imgs[i].src.replace(oldUrl, remoteUrl)
}
}
replaceImages('http://aff.kooora.com/i/z3.gif', 'http://example.com/aff.kooora.com/i/z3.gif')
如果您正在处理网站的本地版本并且懒得下载其所有图像内容,这也很棒
相关文章:
- 使用javascript的图像替换循环
- 是否可以仅通过jQuery将图像替换为用户动态选择的另一个图像
- 用图像替换SVG
- 将href中的图像替换为其他元素中的图像
- 错误时用图像替换视频
- 以编程方式将fabric-js-canvas的图像替换为hq图像,并重新计算其他对象的坐标和大小
- jQuery 图像替换为类
- 可以用我自己的自定义图像替换光标
- jQuery-图像替换转换〔Safari中的问题〕
- d3.js用图像替换节点
- 用图像替换页面,而不是添加图像
- 可以动态设置图像替换文本吗
- JQuery - 单击时将背景图像替换为另一个背景图像
- 将鼠标悬停在另一个图像上时,将图像替换为另一个图像
- HTML:用图像替换文本微笑
- 将所有图像替换为一个图像 Javascript
- 在 jquery 中使用类和 ID 将一个图像替换为另一个图像
- 如何用自定义图像替换脸书按钮
- 在 PHP 中将一个外部图像替换为另一个内部图像
- 将图像替换为另一个图像,然后在超时后再次替换 - Javascript