Downloading an image using XMLHttpRequest in a userscript

前端 未结 4 2182
走了就别回头了
走了就别回头了 2020-12-05 05:34

First of all there is a question with the same title here on SO but its not what I\'m looking for and it doesn\'t have a complete answer either.

So here\'s my questi

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-05 06:05

    Modern browsers have the Blob object:

    GM_xmlhttpRequest({
      method: "GET",
      url: url,
      headers: { referer: url, origin: url },
      responseType: 'blob',
      onload: function(resp) {
        var img = document.createElement('img');
        img.src = window.URL.createObjectURL(resp.response);
        document.body.appendChild(img);
      }
    });
    

    The headers param will set the referrer so you can load referrer locked images.

提交回复
热议问题