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

Function decodeBody

src/background/utils/requests.js:313–333  ·  view source on GitHub ↗

Polyfill for browser's inability to send complex types over extension messaging

([body, type, wasBlob])

Source from the content-addressed store, hash-verified

311
312/** Polyfill for browser's inability to send complex types over extension messaging */
313function decodeBody([body, type, wasBlob]) {
314 if (type === 'fd') {
315 // FF supports FormData over messaging
316 // Chrome doesn't - we use this code only with an empty FormData just to create the object
317 const res = new FormData();
318 body.forEach(entry => res.append(...entry));
319 body = res;
320 type = '';
321 } else if (type === 'usp') {
322 type = FORM_URLENCODED + ';' + CHARSET_UTF8;
323 } else if (type != null) {
324 const res = string2uint8array(undefined, body.slice(body.indexOf(',') + 1));
325 if (!wasBlob) {
326 type = body.match(/^data:(.+?);base64/)[1].replace(/(boundary=)[^;]+/,
327 // using a function so it runs only if "boundary" was found
328 (_, p1) => p1 + String.fromCharCode(...res.slice(2, res.indexOf(13))));
329 }
330 body = res;
331 }
332 return [body, type];
333}
334
335/**
336 * @param {Object} res

Callers 1

httpRequestFunction · 0.85

Calls 1

string2uint8arrayFunction · 0.90

Tested by

no test coverage detected