Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save XYShaoKang/d5ef2b3f9c6f20dbf250387df8dfcdeb to your computer and use it in GitHub Desktop.
Save XYShaoKang/d5ef2b3f9c6f20dbf250387df8dfcdeb to your computer and use it in GitHub Desktop.
  • 按 F12 打开开发者控制台
  • 点击Console标签
    • 如果是 Firefox 浏览器,点击控制台
  • 复制粘贴下面这段代码,按回车,即可正常加载到图片
// 修复 什么值得买 的文章中,引用外链的图片加载出现`403`错误的问题
$('#articleId img').map((_, el) => {
  // 获取图片 Url
  const src = $(el).attr('src')
  // 判断是否为外链
  if (/smzdm\.com/.test(src)) {
    return
  }
  // 图片的服务器会检查`Referer`,将`Referer`设置为`no-referrer`再请求可以正常获取到图片
  fetch(src, { referrerPolicy: 'no-referrer' })
    .then(function(response) {
      if (response.ok) {
        return response.blob()
      }
      throw new Error('图片获取失败')
    })
    .then(function(imgBlob) {
      var objectURL = URL.createObjectURL(imgBlob)
      el.src = objectURL
    })
    .catch(function(error) {
      console.log(error.message)
    })
})
@BlackFlame33
Copy link

亲测有效,学习到了,谢谢大佬

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment