【发布时间】:2015-10-24 20:57:44
【问题描述】:
我正在尝试修改特定图像,因为该站点显示的文件分辨率低于原始文件,并替换该图像的链接 href - 在该链接的末尾附加一些内容以使其可下载。
当您右键单击/保存(文件另存为 content.jpg)时,该网站不会正确地将有关图像的信息发送到浏览器,但他们有一个下载链接,可以将文件名正确发送到浏览器(它所做的只是将&dl=1 附加到 URL 的末尾)
我找到了一些示例代码,并对其进行了修改以执行我需要的 img src 更改,但在附加到链接 href 时遇到问题。
页面上的链接类型很多,因此需要特定的 URL 替换:
整个页面的可点击链接(请勿触摸):
example.com/view/UNIQUEID图片来源(低分辨率):example.com/subdir/preview?page=UNIQUEID
图片来源(原始资源):example.com/subdir/content?page=UNIQUEID
我只希望将图像源从content/preview更改为content/content以在浏览器中显示全分辨率图像,但还要添加一个href链接(这可以从img src复制为其相同的链接,但也可以在它的末尾附加一些东西而不影响任何其他链接。)
这是我目前所拥有的用于替换特定 img src 的内容:
image = document.getElementsByTagName("img");
for (i = 0; i < image.length; i++) if (image[i].parentNode.href) {
//Remove onclick attribute, cancelling their own Image Viewer
image[i].parentNode.removeAttribute('onclick');
//Replace regular sized preview image, with the full sized image
image[i].src = image[i].src.replace('example.com/subdir/preview?page=','example.com/subdir/content?page=');
image[i].parentNode.removeAttribute('width');
image[i].parentNode.removeAttribute('height');
}
现在我发现添加
image[i].parentNode.href = image[i].src + '&dl=1';
在我当前的脚本工作结束时。但它会影响每张图片,因此会破坏很多其他东西。
有什么建议可以简单地将 &dl=1 附加到现在替换的 'subdir/content?page=UNIQUEID' href 链接的末尾?
TLDR:
期待改变:
<a href="http://example.com/subdir/content?page=12345" onclick="imageviewer.open(); return false;">
<img src="http://example.com/subdir/preview?page=12345&filename=this+is+the+filename.jpg">
</a>
进入:
<a href="http://example.com/subdir/content?page=12345&dl=1">
<img src="http://example.com/subdir/content?page=12345&filename=this+is+the+filename.jpg">
</a>
【问题讨论】:
标签: javascript hyperlink greasemonkey