MCPcopy Create free account
hub / github.com/violentmonkey/violentmonkey / decodeResource

Function decodeResource

src/injected/content/util.js:64–95  ·  view source on GitHub ↗
(raw, isBlob, isUserUrl)

Source from the content-addressed store, hash-verified

62 * @return {string|Uint8Array|Blob|string}
63 */
64export const decodeResource = (raw, isBlob, isUserUrl) => {
65 let res;
66 const pos = raw::stringIndexOf(',');
67 const mimeType = pos < 0 ? '' : raw::slice(isUserUrl ? 5/*data:*/ : 0, pos);
68 const mimeData = pos < 0 ? raw : raw::slice(pos + 1);
69 if (isBlob === false) {
70 return `data:${mimeType};base64,${mimeData}`;
71 }
72 res = !isUserUrl || mimeType::stringIndexOf('base64') >= 0
73 ? safeAtob(mimeData)
74 : mimeData;
75 // TODO: do the check in BG and cache/store the result because safe-guarding all the stuff
76 // regexp picks from an instance internally is inordinately complicated
77 if (/[\x80-\xFF]/::regexpTest(res)) {
78 if (U8_fromBase64) {
79 res = U8_fromBase64(mimeData);
80 } else {
81 const len = res.length;
82 const bytes = new SafeUint8Array(len);
83 for (let i = 0; i < len; i += 1) {
84 bytes[i] = safeCharCodeAt(res, i);
85 }
86 res = bytes;
87 }
88 if (!isBlob) res = new SafeTextDecoder()::tdDecode(res);
89 }
90 if (isBlob && isBlob !== SafeUint8Array) {
91 res = makeSafeBlob(res, mimeType);
92 if (isBlob !== SafeBlob) res = createObjectURL(res);
93 }
94 return res;
95};
96
97/**
98 * Used by `injected`

Callers 3

requestVirtualUrlFunction · 0.90
GetResourceFunction · 0.90

Calls 2

makeSafeBlobFunction · 0.85
createObjectURLFunction · 0.85

Tested by

no test coverage detected