MCPcopy
hub / github.com/darkreader/darkreader / getURLModifier

Function getURLModifier

src/inject/dynamic-theme/modify-css.ts:484–577  ·  view source on GitHub ↗
(urlValue: string)

Source from the content-addressed store, hash-verified

482 };
483
484 const getURLModifier = (urlValue: string) => {
485 if (!didTryLoadCache) {
486 didTryLoadCache = true;
487 readImageDetailsCache(imageDetailsCache);
488 }
489
490 let url = getCSSURLValue(urlValue);
491 const isURLEmpty = url.length === 0;
492 const {parentStyleSheet} = rule;
493 const ownerNode = parentStyleSheet?.ownerNode;
494 const scope = (parentStyleSheet && getSheetScope(parentStyleSheet)) ?? document;
495 const baseURL = (parentStyleSheet && parentStyleSheet.href) ?
496 getCSSBaseBath(parentStyleSheet.href) :
497 ownerNode?.baseURI || location.origin;
498 url = getAbsoluteURL(baseURL, url);
499
500 return async (theme: Theme): Promise<string | null> => {
501 if (isURLEmpty) {
502 return "url('')";
503 }
504
505 let selector = rule.selectorText;
506 if (selector) {
507 if (selector.includes('::before')) {
508 selector = selector.replaceAll('::before', '');
509 }
510 if (selector.includes('::after')) {
511 selector = selector.replaceAll('::after', '');
512 }
513 if (!scope.querySelector(selector)) {
514 await new Promise<void>((resolve) => {
515 if (imageSelectorQueue.has(selector)) {
516 imageSelectorQueue.get(selector)!.push(resolve);
517 } else {
518 imageSelectorQueue.set(selector, [resolve]);
519 imageSelectorValues.set(selector, urlValue);
520 }
521 });
522 }
523 }
524
525 let imageDetails: ImageDetails | null = null;
526 if (imageDetailsCache.has(url)) {
527 imageDetails = imageDetailsCache.get(url)!;
528 } else {
529 try {
530 if (!isBlobURLCheckResultReady()) {
531 await requestBlobURLCheck();
532 }
533 if (awaitingForImageLoading.has(url)) {
534 const awaiters = awaitingForImageLoading.get(url)!;
535 imageDetails = await new Promise<ImageDetails | null>((resolve) => awaiters.push(resolve));
536 if (!imageDetails) {
537 return null;
538 }
539 } else {
540 awaitingForImageLoading.set(url, []);
541 imageDetails = await getImageDetails(url);

Callers 1

getBgImageModifierFunction · 0.85

Calls 15

readImageDetailsCacheFunction · 0.90
getCSSURLValueFunction · 0.90
getSheetScopeFunction · 0.90
getCSSBaseBathFunction · 0.90
getAbsoluteURLFunction · 0.90
requestBlobURLCheckFunction · 0.90
getImageDetailsFunction · 0.90
writeImageDetailsCacheFunction · 0.90
logWarnFunction · 0.90
getBgImageValueFunction · 0.85

Tested by

no test coverage detected