在开发油猴脚本中遇到的问题

<img src="https://pic1.zhimg.com/v2-4f35bcc829265049eb5fd81b8cee6d54_b.jpg" data-caption="" data-size="small" data-rawwidth="1080" data-rawheight="2244" class="origin_image zh-lightbox-thumb" width="1080" data-original="https://pic1.zhimg.com/v2-4f35bcc829265049eb5fd81b8cee6d54_r.jpg">

期望通过 get 请求获取图片文件对象将其转换为 base64文本 然后替换图片src
期望的效果

<img src="base64的图片" data-caption="" data-size="small" data-rawwidth="1080" data-rawheight="2244" class="origin_image zh-lightbox-thumb" width="1080" data-original="https://pic1.zhimg.com/v2-4f35bcc829265049eb5fd81b8cee6d54_r.jpg">

function getFileFromUrl(url) {
              var blob = null;
              var xhr = new XMLHttpRequest();
              xhr.open("GET", url);
              xhr.setRequestHeader('Accept', 'image/png');
              xhr.responseType = "blob";
              // 加载时处理
              xhr.onload = () => {
              	// 获取返回结果
                  blob = xhr.response;
                  var file= new File([blob], "a.png", { type: 'image/png' });
                  // 返回结果
                  return file;
              };
              xhr.onerror = (e) => {
                  return e
              };
              // 发送
              xhr.send();
      }
}, seed)}

这个应该会输出图片对象,不知有什么问题
求解决

盲猜,跨域了

用 GM_xmlhttpRequest 比较好。

确实。 :face_with_thermometer:

我试试