MCPcopy Index your code
hub / github.com/darkreader/darkreader / tryConvertDataURLToBlobSync

Function tryConvertDataURLToBlobSync

src/inject/dynamic-theme/image.ts:357–380  ·  view source on GitHub ↗
(dataURL: string)

Source from the content-addressed store, hash-verified

355const dataURLBlobURLs = new Map<number, string>();
356
357function tryConvertDataURLToBlobSync(dataURL: string): Blob | null {
358 const colonIndex = dataURL.indexOf(':');
359 const semicolonIndex = dataURL.indexOf(';', colonIndex + 1);
360 const commaIndex = dataURL.indexOf(',', semicolonIndex + 1);
361 const encoding = dataURL.substring(semicolonIndex + 1, commaIndex).toLocaleLowerCase();
362 const mediaType = dataURL.substring(colonIndex + 1, semicolonIndex);
363
364 // It should be possible to easily convert UTF-8,
365 // though it is unclear if decodeURIComponent will be necessary
366 // and if it will be performant enough for big Data URLs
367 if (encoding !== 'base64' || !mediaType) {
368 return null;
369 }
370 let base64Content = dataURL.substring(commaIndex + 1);
371 if (base64Content.includes('%')) {
372 base64Content = decodeURIComponent(base64Content);
373 }
374 const characters = atob(base64Content);
375 const bytes = new Uint8Array(characters.length);
376 for (let i = 0; i < characters.length; i++) {
377 bytes[i] = characters.charCodeAt(i);
378 }
379 return new Blob([bytes], {type: mediaType});
380}
381
382export async function tryConvertDataURLToBlobURL(dataURL: string): Promise<string | null> {
383 if (!isBlobURLSupported) {

Callers 2

getImageDetailsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected