MCPcopy Index your code
hub / github.com/hoothin/UserScripts / urlToBlob

Function urlToBlob

Picviewer CE+/dist.user.js:12597–12654  ·  view source on GitHub ↗
(url, cb, forcePng, tryTimes = 0)

Source from the content-addressed store, hash-verified

12595 return ext;
12596 }
12597 function urlToBlob(url, cb, forcePng, tryTimes = 0) {
12598 tryTimes++;
12599 if (tryTimes > 3) {
12600 return cb(null, '');
12601 }
12602 _GM_xmlhttpRequest({
12603 method: 'GET',
12604 url: url.trim(),
12605 responseType:'blob',
12606 timeout:20000,
12607 headers: {
12608 origin: location.origin,
12609 referer: location.href,
12610 accept: "*/*"
12611 },
12612 cookie: cookie,
12613 onload: function(d) {
12614 let blob = d.response;
12615 if (!blob.type) return urlToBlob(url, cb, forcePng, tryTimes);
12616 let ext = extFromMimeAndUrl(blob.type, url);
12617 if (blob.type && blob.type.indexOf("text/html") === 0 && (blob.size || 0) < 100000) {
12618 urlToBlobWithFetch(url, cb);
12619 return;
12620 }
12621 if (ext === "none") ext = "webp";
12622 let conversion = formatDict.get(ext);
12623 if (canvas && (conversion || forcePng)) {
12624 var self = this;
12625 var a = new FileReader();
12626 a.readAsDataURL(blob);
12627 a.onload = function (e) {
12628 dataURLToCanvas(e.target.result, canvas => {
12629 canvas.toBlob(nblob => {
12630 if (!nblob) {
12631 cb(blob, ext || "png");
12632 } else {
12633 cb(nblob, conversion || "png");
12634 }
12635 }, "image/" + (conversion || "png"));
12636 });
12637 };
12638 a.onerror = function (e){
12639 urlToBlob(url, cb, forcePng, tryTimes);
12640 }
12641 } else if (!blob || !blob.size) {
12642 urlToBlob(url, cb, forcePng, tryTimes);
12643 } else {
12644 cb(blob, ext);
12645 }
12646 },
12647 onerror: function(){
12648 urlToBlob(url, cb, forcePng, tryTimes);
12649 },
12650 ontimeout: function(){
12651 urlToBlob(url, cb, forcePng, tryTimes);
12652 }
12653 });
12654 }

Callers 4

dist.user.jsFile · 0.70
downloadImgFunction · 0.70
crosHandlerFunction · 0.70
copyDataFunction · 0.70

Calls 3

extFromMimeAndUrlFunction · 0.70
urlToBlobWithFetchFunction · 0.70
dataURLToCanvasFunction · 0.70

Tested by

no test coverage detected