Greasy Fork

Greasy Fork is available in English.

跨域获取图片地址的blob

通过发送消息的方式提供图片url转blob的方法

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         跨域获取图片地址的blob
// @version      1.0.2
// @description  通过发送消息的方式提供图片url转blob的方法
// @author       wilder
// @match        *://*/*
// @grant        GM_xmlhttpRequest
// @namespace http://greasyfork.icu/users/1211834
// ==/UserScript==

(function() {
    'use strict';
    window.addEventListener('message',(e)=>{
        if(e.data && e.data.type === 'getImageBlob'){
            console.log('准备解析',e.data)
           getImageBlob(e.data.url).then(blob=>{
               console.log('是不是blob',blob)
               window.postMessage({
                  blob: blob,
                  type: e.data.type + '_result'
               },'*')
           })
        }
    })
})();

function getImageBlob(url){
    return new Promise((resolve,reject)=>{
        GM_xmlhttpRequest({
            method:'get',
            url:url,
            responseType:'blob',
            onload(res){
               resolve(res.response)
            }
        })
    })
}

function getImageBase64(blob) {

  return new Promise((resolve,reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(blob);
    reader.onload = () => {
      const base64 = reader.result;
      resolve(base64);
    }
    reader.onerror = error => reject(error);
  });
}