先说结论
$$('img').forEach(async (img) => {
try {
const src = img.src;
// Fetch the image as a blob.
const fetchResponse = await fetch(src);
const blob = await fetchResponse.blob();
const mimeType = blob.type;
// Figure out a name for it from the src and the mime-type.
const start = src.lastIndexOf('/') + 1;
const end = src.indexOf('.', start);
let name = src.substring(start, end === -1 ? undefined : end);
name = name.replace(/[^a-zA-Z0-9]+/g, '-');
name += '.' + mimeType.substring(mimeType.lastIndexOf('/') + 1);
// Download the blob using a <a> element.
const a = document.createElement('a');
a.setAttribute('href', URL.createObjectURL(blob));
a.setAttribute('download', name);
a.click();
} catch (e) {}
});
这个技巧并不限定于任何浏览器,只要可以执行 JavaScript,就可以在任何地方运行。如果要下载网页上的所有图片,可以打开 Devtools 工具,粘贴以下代码,然后按 Enter:
演示
打开一个网页
一键全部下载
下载失败如何办(有的网页会下载失败,可能是网页上的 CSP 策略阻止了)
将代码粘贴到 Sources 下的 Snoppets 下: