(function () {
if (window.__vsDlInstalled) return;
window.__vsDlInstalled = true;
var TAG = 'vs-download-v1';
var MAX_ENTRIES = 8;
var MAX_BLOBS = 16;
var MAX_BYTES = 64 * 1024 * 1024;
var isTop = false;
try { isTop = window.top === window; } catch (e) { isTop = false; }
var NATIVE = new WeakMap();
(function installToStringGuard() {
try {
var origToString = Function.prototype.toString;
var guard = function toString() {
var real = NATIVE.get(this);
return origToString.call(real || this);
};
NATIVE.set(guard, origToString);
Function.prototype.toString = guard;
} catch (e) { }
})();
function passAsNative(replacement, original) {
try { NATIVE.set(replacement, original); } catch (e) { }
return replacement;
}
window.__vsHideGlobals = function (names) {
for (var i = 0; i < names.length; i += 1) {
var n = names[i];
if (!Object.prototype.hasOwnProperty.call(window, n)) continue;
try {
var d = Object.getOwnPropertyDescriptor(window, n);
if (!d || d.enumerable === false || d.configurable === false) continue;
d.enumerable = false;
Object.defineProperty(window, n, d);
} catch (e) { }
}
};
passAsNative(window.__vsHideGlobals, Object.defineProperty);
var blobs = new Map();
function remember(url, blob) {
try {
blobs.set(url, blob);
while (blobs.size > MAX_BLOBS) blobs.delete(blobs.keys().next().value);
} catch (e) { }
}
var origCreate = typeof URL !== 'undefined' && URL.createObjectURL;
if (origCreate) {
URL.createObjectURL = passAsNative(function createObjectURL(obj) {
var url = origCreate.call(URL, obj);
try {
if (typeof Blob !== 'undefined' && obj instanceof Blob) remember(url, obj);
} catch (e) { }
return url;
}, origCreate);
}
if (isTop && !window.__vsDl) {
window.__vsDl = {
list: [],
seq: 0,
push: function (entry) {
this.seq += 1;
entry.id = this.seq;
this.list.push(entry);
while (this.list.length > MAX_ENTRIES) this.list.shift();
return entry.id;
},
get: function (id) {
for (var i = 0; i < this.list.length; i += 1) {
if (this.list[i].id === id) return this.list[i];
}
return null;
},
meta: function (id) {
var e = id ? this.get(id) : this.list[this.list.length - 1];
if (!e) return 'null';
return JSON.stringify({
id: e.id,
name: e.name || 'download',
mime: e.mime || '',
url: e.url || '',
err: e.err || null,
size: e.bytes ? e.bytes.length : 0,
done: !!e.done,
ts: e.ts || 0,
});
},
index: function () {
return JSON.stringify(this.list.map(function (e) {
return {
id: e.id,
name: e.name || 'download',
mime: e.mime || '',
url: e.url || '',
err: e.err || null,
size: e.bytes ? e.bytes.length : 0,
done: !!e.done,
ts: e.ts || 0,
};
}));
},
read: function (id, off, len) {
var e = this.get(id);
if (!e || !e.bytes) return '';
var slice = e.bytes.subarray(off, off + len);
var s = '';
var STEP = 0x8000; for (var i = 0; i < slice.length; i += STEP) {
s += String.fromCharCode.apply(null, slice.subarray(i, i + STEP));
}
return btoa(s);
},
drop: function (id) {
this.list = this.list.filter(function (e) { return e.id !== id; });
return 'ok';
},
clear: function () { this.list = []; return 'ok'; },
};
}
function record(entry) {
entry.ts = Date.now();
entry.done = true;
if (isTop) {
window.__vsDl.push(entry);
return;
}
try {
window.top.postMessage({
__vs: TAG,
name: entry.name,
mime: entry.mime,
url: entry.url,
err: entry.err || null,
bytes: entry.bytes || null,
}, '*');
} catch (e) { }
}
if (isTop) {
window.addEventListener('message', function (ev) {
var d = ev.data;
if (!d || d.__vs !== TAG) return;
window.__vsDl.push({
name: d.name,
mime: d.mime,
url: d.url,
err: d.err,
bytes: d.bytes ? new Uint8Array(d.bytes) : null,
ts: Date.now(),
done: true,
});
}, true);
}
function nameFromDisposition(cd) {
if (!cd) return null;
var star = /filename\*\s*=\s*[^']*'[^']*'([^;]+)/i.exec(cd);
if (star) {
try { return decodeURIComponent(star[1].trim()); } catch (e) { }
}
var plain = /filename\s*=\s*"([^"]*)"|filename\s*=\s*([^;]+)/i.exec(cd);
if (plain) return (plain[1] || plain[2] || '').trim() || null;
return null;
}
function nameFromUrl(url) {
try {
if (url.slice(0, 5) === 'blob:' || url.slice(0, 5) === 'data:') return null;
var u = new URL(url, location.href);
var last = u.pathname.split('/').filter(Boolean).pop();
return last ? decodeURIComponent(last) : null;
} catch (e) { return null; }
}
function grab(url, hint, id) {
var finish = function (entry) {
entry.url = url;
entry.name = entry.name || hint || nameFromUrl(url) || 'download';
if (isTop && id) {
var slot = window.__vsDl.get(id);
if (slot) {
slot.name = entry.name;
slot.mime = entry.mime || '';
slot.bytes = entry.bytes || null;
slot.err = entry.err || null;
slot.ts = Date.now();
slot.done = true;
return;
}
}
record(entry);
};
var known = blobs.get(url);
var source = known
? Promise.resolve({ blob: known, cd: null, ct: known.type })
: fetch(url, { credentials: 'include' }).then(function (res) {
if (!res.ok) throw new Error('HTTP ' + res.status + ' ' + res.statusText);
var cd = null;
var ct = null;
try {
cd = res.headers.get('content-disposition');
ct = res.headers.get('content-type');
} catch (e) { }
return res.blob().then(function (b) { return { blob: b, cd: cd, ct: ct }; });
});
source.then(function (r) {
if (r.blob.size > MAX_BYTES) {
throw new Error('download exceeds ' + MAX_BYTES + ' bytes (' + r.blob.size + ')');
}
return r.blob.arrayBuffer().then(function (buf) {
finish({
name: nameFromDisposition(r.cd) || hint || nameFromUrl(url),
mime: r.blob.type || r.ct || '',
bytes: new Uint8Array(buf),
});
});
}).catch(function (e) {
finish({ err: String((e && e.message) || e) });
});
}
if (isTop) {
window.__vsDl.fetch = function (url, hint) {
var id = this.push({ name: hint || nameFromUrl(url) || 'download', url: url, done: false });
grab(url, hint, id);
return String(id);
};
}
var lastUrl = '';
var lastAt = 0;
function intercept(url, hint) {
if (!url) return;
var now = Date.now();
if (url === lastUrl && now - lastAt < 500) return;
lastUrl = url;
lastAt = now;
grab(url, hint, 0);
}
function anchorIntent(a) {
if (!a || !a.getAttribute) return;
var href = a.getAttribute('href');
if (!href) return;
var abs = a.href || href;
var isBlob = abs.slice(0, 5) === 'blob:' || abs.slice(0, 5) === 'data:';
if (!a.hasAttribute('download') && !isBlob) return;
intercept(abs, a.getAttribute('download') || null);
}
document.addEventListener('click', function (ev) {
var t = ev.target;
while (t && t.nodeType === 1 && t.tagName !== 'A') t = t.parentNode;
if (t && t.nodeType === 1) anchorIntent(t);
}, true);
try {
var protoClick = HTMLAnchorElement.prototype.click;
HTMLAnchorElement.prototype.click = passAsNative(function click() {
try { anchorIntent(this); } catch (e) { }
return protoClick.apply(this, arguments);
}, protoClick);
} catch (e) { }
try {
var origOpen = window.open;
window.open = passAsNative(function open(url) {
try {
if (url && (String(url).slice(0, 5) === 'blob:' || String(url).slice(0, 5) === 'data:')) {
intercept(String(url), null);
}
} catch (e) { }
return origOpen.apply(window, arguments);
}, origOpen);
} catch (e) { }
window.__vsHideGlobals([
'__vsHideGlobals',
'__vsDlInstalled', '__vsDl',
'__vsRAFShimmed', '__vsFlushRAF',
'__vsInspectorInstalled',
'__vsWebAuthn',
'__vsDirty', '__vsDirtyObserver', '__vsRefCounter',
'__vsFindRef', '__vsLastResult',
]);
})()