darkreader 4.9.71 → 4.9.75

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +2 -2
  3. package/darkreader.js +1039 -215
  4. package/package.json +16 -16
package/darkreader.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Dark Reader v4.9.71
2
+ * Dark Reader v4.9.75
3
3
  * https://darkreader.org/
4
4
  */
5
5
 
@@ -464,6 +464,22 @@
464
464
  });
465
465
  });
466
466
  }
467
+ function loadAsBlob(url, mimeType) {
468
+ return __awaiter(this, void 0, void 0, function () {
469
+ var response;
470
+ return __generator(this, function (_a) {
471
+ switch (_a.label) {
472
+ case 0:
473
+ return [4, getOKResponse(url, mimeType)];
474
+ case 1:
475
+ response = _a.sent();
476
+ return [4, response.blob()];
477
+ case 2:
478
+ return [2, _a.sent()];
479
+ }
480
+ });
481
+ });
482
+ }
467
483
  function readResponseAsDataURL(response) {
468
484
  return __awaiter(this, void 0, void 0, function () {
469
485
  var blob, dataURL;
@@ -835,6 +851,17 @@
835
851
  }
836
852
  return {add: add, cancel: cancel};
837
853
  }
854
+ var delayTokens = new Set();
855
+ function requestAnimationFrameOnce(token, callback) {
856
+ if (delayTokens.has(token)) {
857
+ return;
858
+ }
859
+ delayTokens.add(token);
860
+ requestAnimationFrame(function () {
861
+ delayTokens.delete(token);
862
+ callback();
863
+ });
864
+ }
838
865
 
839
866
  function getDuration(time) {
840
867
  var duration = 0;
@@ -2590,24 +2617,24 @@
2590
2617
  }
2591
2618
  });
2592
2619
 
2620
+ var MAX_FRAME_DURATION = 1000 / 60;
2593
2621
  var AsyncQueue = (function () {
2594
2622
  function AsyncQueue() {
2595
2623
  this.queue = [];
2596
2624
  this.timerId = null;
2597
- this.frameDuration = 1000 / 60;
2598
2625
  }
2599
- AsyncQueue.prototype.addToQueue = function (entry) {
2600
- this.queue.push(entry);
2601
- this.startQueue();
2626
+ AsyncQueue.prototype.addTask = function (task) {
2627
+ this.queue.push(task);
2628
+ this.scheduleFrame();
2602
2629
  };
2603
- AsyncQueue.prototype.stopQueue = function () {
2630
+ AsyncQueue.prototype.stop = function () {
2604
2631
  if (this.timerId !== null) {
2605
2632
  cancelAnimationFrame(this.timerId);
2606
2633
  this.timerId = null;
2607
2634
  }
2608
2635
  this.queue = [];
2609
2636
  };
2610
- AsyncQueue.prototype.startQueue = function () {
2637
+ AsyncQueue.prototype.scheduleFrame = function () {
2611
2638
  var _this = this;
2612
2639
  if (this.timerId) {
2613
2640
  return;
@@ -2618,8 +2645,8 @@
2618
2645
  var cb;
2619
2646
  while ((cb = _this.queue.shift())) {
2620
2647
  cb();
2621
- if (Date.now() - start >= _this.frameDuration) {
2622
- _this.startQueue();
2648
+ if (Date.now() - start >= MAX_FRAME_DURATION) {
2649
+ _this.scheduleFrame();
2623
2650
  break;
2624
2651
  }
2625
2652
  }
@@ -2637,53 +2664,97 @@
2637
2664
  2,
2638
2665
  new Promise(function (resolve, reject) {
2639
2666
  return __awaiter(_this, void 0, void 0, function () {
2640
- var dataURL, error_1, image_1, error_2;
2641
- return __generator(this, function (_a) {
2642
- switch (_a.label) {
2667
+ var dataURL_1, _a, blob_1, _b, image_1, _c, error_1;
2668
+ var _d, _e;
2669
+ return __generator(this, function (_f) {
2670
+ switch (_f.label) {
2643
2671
  case 0:
2672
+ _f.trys.push([0, 14, , 15]);
2644
2673
  if (!url.startsWith("data:"))
2645
2674
  return [3, 1];
2646
- dataURL = url;
2647
- return [3, 4];
2675
+ _a = url;
2676
+ return [3, 3];
2648
2677
  case 1:
2649
- _a.trys.push([1, 3, , 4]);
2650
- return [4, getImageDataURL(url)];
2678
+ return [4, getDataURL(url)];
2651
2679
  case 2:
2652
- dataURL = _a.sent();
2653
- return [3, 4];
2680
+ _a = _f.sent();
2681
+ _f.label = 3;
2654
2682
  case 3:
2655
- error_1 = _a.sent();
2656
- reject(error_1);
2657
- return [2];
2683
+ dataURL_1 = _a;
2684
+ if (
2685
+ !(
2686
+ (_d =
2687
+ tryConvertDataURLToBlobSync(
2688
+ dataURL_1
2689
+ )) !== null && _d !== void 0
2690
+ )
2691
+ )
2692
+ return [3, 4];
2693
+ _b = _d;
2694
+ return [3, 6];
2658
2695
  case 4:
2659
- _a.trys.push([4, 6, , 7]);
2660
- return [4, urlToImage(dataURL)];
2696
+ return [4, loadAsBlob(url)];
2661
2697
  case 5:
2662
- image_1 = _a.sent();
2663
- imageManager.addToQueue(function () {
2698
+ _b = _f.sent();
2699
+ _f.label = 6;
2700
+ case 6:
2701
+ blob_1 = _b;
2702
+ if (
2703
+ !dataURL_1.startsWith(
2704
+ "data:image/svg+xml"
2705
+ )
2706
+ )
2707
+ return [3, 8];
2708
+ return [4, loadImage(dataURL_1)];
2709
+ case 7:
2710
+ image_1 = _f.sent();
2711
+ return [3, 13];
2712
+ case 8:
2713
+ return [
2714
+ 4,
2715
+ tryCreateImageBitmap(blob_1)
2716
+ ];
2717
+ case 9:
2718
+ if (
2719
+ !(
2720
+ (_e = _f.sent()) !== null &&
2721
+ _e !== void 0
2722
+ )
2723
+ )
2724
+ return [3, 10];
2725
+ _c = _e;
2726
+ return [3, 12];
2727
+ case 10:
2728
+ return [4, loadImage(dataURL_1)];
2729
+ case 11:
2730
+ _c = _f.sent();
2731
+ _f.label = 12;
2732
+ case 12:
2733
+ image_1 = _c;
2734
+ _f.label = 13;
2735
+ case 13:
2736
+ imageManager.addTask(function () {
2664
2737
  var analysis =
2665
2738
  analyzeImage(image_1);
2666
2739
  resolve(
2667
2740
  __assign(
2668
2741
  {
2669
2742
  src: url,
2670
- dataURL:
2671
- analysis.isLarge
2672
- ? ""
2673
- : dataURL,
2674
- width: image_1.naturalWidth,
2675
- height: image_1.naturalHeight
2743
+ blob: blob_1,
2744
+ dataURL: dataURL_1,
2745
+ width: image_1.width,
2746
+ height: image_1.height
2676
2747
  },
2677
2748
  analysis
2678
2749
  )
2679
2750
  );
2680
2751
  });
2681
- return [3, 7];
2682
- case 6:
2683
- error_2 = _a.sent();
2684
- reject(error_2);
2685
- return [3, 7];
2686
- case 7:
2752
+ return [3, 15];
2753
+ case 14:
2754
+ error_1 = _f.sent();
2755
+ reject(error_1);
2756
+ return [3, 15];
2757
+ case 15:
2687
2758
  return [2];
2688
2759
  }
2689
2760
  });
@@ -2693,7 +2764,7 @@
2693
2764
  });
2694
2765
  });
2695
2766
  }
2696
- function getImageDataURL(url) {
2767
+ function getDataURL(url) {
2697
2768
  return __awaiter(this, void 0, void 0, function () {
2698
2769
  var parsedURL;
2699
2770
  return __generator(this, function (_a) {
@@ -2716,7 +2787,33 @@
2716
2787
  });
2717
2788
  });
2718
2789
  }
2719
- function urlToImage(url) {
2790
+ function tryCreateImageBitmap(blob) {
2791
+ return __awaiter(this, void 0, void 0, function () {
2792
+ var err_1;
2793
+ return __generator(this, function (_a) {
2794
+ switch (_a.label) {
2795
+ case 0:
2796
+ _a.trys.push([0, 2, , 3]);
2797
+ return [4, createImageBitmap(blob)];
2798
+ case 1:
2799
+ return [2, _a.sent()];
2800
+ case 2:
2801
+ err_1 = _a.sent();
2802
+ logWarn(
2803
+ "Unable to create image bitmap for type "
2804
+ .concat(blob.type, ": ")
2805
+ .concat(String(err_1))
2806
+ );
2807
+ return [2, null];
2808
+ case 3:
2809
+ return [2];
2810
+ }
2811
+ });
2812
+ });
2813
+ }
2814
+ var INCOMPLETE_DOC_LOADING_IMAGE_LIMIT = 256;
2815
+ var loadingImagesCount = 0;
2816
+ function loadImage(url) {
2720
2817
  return __awaiter(this, void 0, void 0, function () {
2721
2818
  return __generator(this, function (_a) {
2722
2819
  return [
@@ -2729,7 +2826,17 @@
2729
2826
  image.onerror = function () {
2730
2827
  return reject("Unable to load image ".concat(url));
2731
2828
  };
2732
- image.src = url;
2829
+ if (
2830
+ ++loadingImagesCount <=
2831
+ INCOMPLETE_DOC_LOADING_IMAGE_LIMIT ||
2832
+ isReadyStateComplete()
2833
+ ) {
2834
+ image.src = url;
2835
+ } else {
2836
+ addReadyStateCompleteListener(function () {
2837
+ return (image.src = url);
2838
+ });
2839
+ }
2733
2840
  })
2734
2841
  ];
2735
2842
  });
@@ -2756,10 +2863,16 @@
2756
2863
  if (!canvas) {
2757
2864
  createCanvas();
2758
2865
  }
2759
- var naturalWidth = image.naturalWidth,
2760
- naturalHeight = image.naturalHeight;
2761
- if (naturalHeight === 0 || naturalWidth === 0) {
2762
- logWarn("logWarn(Image is empty ".concat(image.currentSrc, ")"));
2866
+ var sw;
2867
+ var sh;
2868
+ if (image instanceof HTMLImageElement) {
2869
+ sw = image.naturalWidth;
2870
+ sh = image.naturalHeight;
2871
+ } else {
2872
+ sw = image.width;
2873
+ sh = image.height;
2874
+ }
2875
+ if (sw === 0 || sh === 0) {
2763
2876
  return {
2764
2877
  isDark: false,
2765
2878
  isLight: false,
@@ -2768,7 +2881,7 @@
2768
2881
  isTooLarge: false
2769
2882
  };
2770
2883
  }
2771
- if (naturalWidth * naturalHeight > LARGE_IMAGE_PIXELS_COUNT) {
2884
+ if (sw * sh > LARGE_IMAGE_PIXELS_COUNT) {
2772
2885
  return {
2773
2886
  isDark: false,
2774
2887
  isLight: false,
@@ -2776,25 +2889,15 @@
2776
2889
  isLarge: true
2777
2890
  };
2778
2891
  }
2779
- var naturalPixelsCount = naturalWidth * naturalHeight;
2892
+ var sourcePixelsCount = sw * sh;
2780
2893
  var k = Math.min(
2781
2894
  1,
2782
- Math.sqrt(MAX_ANALYSIS_PIXELS_COUNT / naturalPixelsCount)
2895
+ Math.sqrt(MAX_ANALYSIS_PIXELS_COUNT / sourcePixelsCount)
2783
2896
  );
2784
- var width = Math.ceil(naturalWidth * k);
2785
- var height = Math.ceil(naturalHeight * k);
2897
+ var width = Math.ceil(sw * k);
2898
+ var height = Math.ceil(sh * k);
2786
2899
  context.clearRect(0, 0, width, height);
2787
- context.drawImage(
2788
- image,
2789
- 0,
2790
- 0,
2791
- naturalWidth,
2792
- naturalHeight,
2793
- 0,
2794
- 0,
2795
- width,
2796
- height
2797
- );
2900
+ context.drawImage(image, 0, 0, sw, sh, 0, 0, width, height);
2798
2901
  var imageData = context.getImageData(0, 0, width, height);
2799
2902
  var d = imageData.data;
2800
2903
  var TRANSPARENT_ALPHA_THRESHOLD = 0.05;
@@ -2841,10 +2944,86 @@
2841
2944
  isLarge: false
2842
2945
  };
2843
2946
  }
2844
- function getFilteredImageDataURL(_a, theme) {
2947
+ var isBlobURLSupported = null;
2948
+ var canUseProxy = false;
2949
+ var blobURLCheckRequested = false;
2950
+ var blobURLCheckAwaiters = [];
2951
+ document.addEventListener(
2952
+ "__darkreader__inlineScriptsAllowed",
2953
+ function () {
2954
+ return (canUseProxy = true);
2955
+ },
2956
+ {once: true}
2957
+ );
2958
+ function requestBlobURLCheck() {
2959
+ return __awaiter(this, void 0, void 0, function () {
2960
+ return __generator(this, function (_a) {
2961
+ switch (_a.label) {
2962
+ case 0:
2963
+ if (!canUseProxy) {
2964
+ return [2];
2965
+ }
2966
+ if (!blobURLCheckRequested) return [3, 2];
2967
+ return [
2968
+ 4,
2969
+ new Promise(function (resolve) {
2970
+ return blobURLCheckAwaiters.push(resolve);
2971
+ })
2972
+ ];
2973
+ case 1:
2974
+ return [2, _a.sent()];
2975
+ case 2:
2976
+ blobURLCheckRequested = true;
2977
+ return [
2978
+ 4,
2979
+ new Promise(function (resolve) {
2980
+ document.addEventListener(
2981
+ "__darkreader__blobURLCheckResponse",
2982
+ function (e) {
2983
+ isBlobURLSupported =
2984
+ e.detail.blobURLAllowed;
2985
+ resolve();
2986
+ blobURLCheckAwaiters.forEach(
2987
+ function (r) {
2988
+ return r();
2989
+ }
2990
+ );
2991
+ blobURLCheckAwaiters.splice(0);
2992
+ },
2993
+ {once: true}
2994
+ );
2995
+ document.dispatchEvent(
2996
+ new CustomEvent(
2997
+ "__darkreader__blobURLCheckRequest"
2998
+ )
2999
+ );
3000
+ })
3001
+ ];
3002
+ case 3:
3003
+ _a.sent();
3004
+ return [2];
3005
+ }
3006
+ });
3007
+ });
3008
+ }
3009
+ function isBlobURLCheckResultReady() {
3010
+ return isBlobURLSupported != null || !canUseProxy;
3011
+ }
3012
+ function onCSPError(err) {
3013
+ if (err.blockedURI === "blob") {
3014
+ isBlobURLSupported = false;
3015
+ document.removeEventListener("securitypolicyviolation", onCSPError);
3016
+ }
3017
+ }
3018
+ document.addEventListener("securitypolicyviolation", onCSPError);
3019
+ var objectURLs = new Set();
3020
+ function getFilteredImageURL(_a, theme) {
2845
3021
  var dataURL = _a.dataURL,
2846
3022
  width = _a.width,
2847
3023
  height = _a.height;
3024
+ if (dataURL.startsWith("data:image/svg+xml")) {
3025
+ dataURL = escapeXML(dataURL);
3026
+ }
2848
3027
  var matrix = getSVGFilterMatrixValue(theme);
2849
3028
  var svg = [
2850
3029
  '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="'
@@ -2864,11 +3043,91 @@
2864
3043
  .concat(dataURL, '" />'),
2865
3044
  "</svg>"
2866
3045
  ].join("");
2867
- return "data:image/svg+xml;base64,".concat(btoa(svg));
3046
+ if (!isBlobURLSupported) {
3047
+ return "data:image/svg+xml;base64,".concat(btoa(svg));
3048
+ }
3049
+ var bytes = new Uint8Array(svg.length);
3050
+ for (var i = 0; i < svg.length; i++) {
3051
+ bytes[i] = svg.charCodeAt(i);
3052
+ }
3053
+ var blob = new Blob([bytes], {type: "image/svg+xml"});
3054
+ var objectURL = URL.createObjectURL(blob);
3055
+ objectURLs.add(objectURL);
3056
+ return objectURL;
3057
+ }
3058
+ var xmlEscapeChars = {
3059
+ "<": "&lt;",
3060
+ ">": "&gt;",
3061
+ "&": "&amp;",
3062
+ "'": "&apos;",
3063
+ '"': "&quot;"
3064
+ };
3065
+ function escapeXML(str) {
3066
+ return str.replace(/[<>&'"]/g, function (c) {
3067
+ var _a;
3068
+ return (_a = xmlEscapeChars[c]) !== null && _a !== void 0 ? _a : c;
3069
+ });
3070
+ }
3071
+ var dataURLBlobURLs = new Map();
3072
+ function tryConvertDataURLToBlobSync(dataURL) {
3073
+ var colonIndex = dataURL.indexOf(":");
3074
+ var semicolonIndex = dataURL.indexOf(";", colonIndex + 1);
3075
+ var commaIndex = dataURL.indexOf(",", semicolonIndex + 1);
3076
+ var encoding = dataURL
3077
+ .substring(semicolonIndex + 1, commaIndex)
3078
+ .toLocaleLowerCase();
3079
+ var mediaType = dataURL.substring(colonIndex + 1, semicolonIndex);
3080
+ if (encoding !== "base64" || !mediaType) {
3081
+ return null;
3082
+ }
3083
+ var characters = atob(dataURL.substring(commaIndex + 1));
3084
+ var bytes = new Uint8Array(characters.length);
3085
+ for (var i = 0; i < characters.length; i++) {
3086
+ bytes[i] = characters.charCodeAt(i);
3087
+ }
3088
+ return new Blob([bytes], {type: mediaType});
3089
+ }
3090
+ function tryConvertDataURLToBlobURL(dataURL) {
3091
+ return __awaiter(this, void 0, void 0, function () {
3092
+ var blobURL, blob, response;
3093
+ return __generator(this, function (_a) {
3094
+ switch (_a.label) {
3095
+ case 0:
3096
+ if (!isBlobURLSupported) {
3097
+ return [2, null];
3098
+ }
3099
+ blobURL = dataURLBlobURLs.get(dataURL);
3100
+ if (blobURL) {
3101
+ return [2, blobURL];
3102
+ }
3103
+ blob = tryConvertDataURLToBlobSync(dataURL);
3104
+ if (!!blob) return [3, 3];
3105
+ return [4, fetch(dataURL)];
3106
+ case 1:
3107
+ response = _a.sent();
3108
+ return [4, response.blob()];
3109
+ case 2:
3110
+ blob = _a.sent();
3111
+ _a.label = 3;
3112
+ case 3:
3113
+ blobURL = URL.createObjectURL(blob);
3114
+ dataURLBlobURLs.set(dataURL, blobURL);
3115
+ return [2, blobURL];
3116
+ }
3117
+ });
3118
+ });
2868
3119
  }
2869
3120
  function cleanImageProcessingCache() {
2870
- imageManager && imageManager.stopQueue();
3121
+ imageManager && imageManager.stop();
2871
3122
  removeCanvas();
3123
+ objectURLs.forEach(function (u) {
3124
+ return URL.revokeObjectURL(u);
3125
+ });
3126
+ objectURLs.clear();
3127
+ dataURLBlobURLs.forEach(function (u) {
3128
+ return URL.revokeObjectURL(u);
3129
+ });
3130
+ dataURLBlobURLs.clear();
2872
3131
  }
2873
3132
 
2874
3133
  var gradientLength = "gradient".length;
@@ -3509,24 +3768,35 @@
3509
3768
  ? void 0
3510
3769
  : _a.baseURI) || location.origin;
3511
3770
  url = getAbsoluteURL(baseURL, url);
3512
- var absoluteValue = 'url("'.concat(url, '")');
3513
3771
  return function (filter) {
3514
3772
  return __awaiter(_this, void 0, void 0, function () {
3515
- var imageDetails, awaiters_1, err_1, bgImageValue;
3773
+ var imageDetails,
3774
+ awaiters_1,
3775
+ err_1,
3776
+ bgImageValue,
3777
+ blobURL;
3516
3778
  return __generator(this, function (_a) {
3517
3779
  switch (_a.label) {
3518
3780
  case 0:
3519
3781
  if (isURLEmpty) {
3520
3782
  return [2, "url('')"];
3521
3783
  }
3784
+ imageDetails = null;
3522
3785
  if (!imageDetailsCache.has(url))
3523
3786
  return [3, 1];
3524
3787
  imageDetails = imageDetailsCache.get(url);
3525
- return [3, 7];
3788
+ return [3, 9];
3526
3789
  case 1:
3527
- _a.trys.push([1, 6, , 7]);
3528
- if (!awaitingForImageLoading.has(url))
3790
+ _a.trys.push([1, 8, , 9]);
3791
+ if (!!isBlobURLCheckResultReady())
3529
3792
  return [3, 3];
3793
+ return [4, requestBlobURLCheck()];
3794
+ case 2:
3795
+ _a.sent();
3796
+ _a.label = 3;
3797
+ case 3:
3798
+ if (!awaitingForImageLoading.has(url))
3799
+ return [3, 5];
3530
3800
  awaiters_1 =
3531
3801
  awaitingForImageLoading.get(url);
3532
3802
  return [
@@ -3535,16 +3805,16 @@
3535
3805
  return awaiters_1.push(resolve);
3536
3806
  })
3537
3807
  ];
3538
- case 2:
3808
+ case 4:
3539
3809
  imageDetails = _a.sent();
3540
3810
  if (!imageDetails) {
3541
3811
  return [2, null];
3542
3812
  }
3543
- return [3, 5];
3544
- case 3:
3813
+ return [3, 7];
3814
+ case 5:
3545
3815
  awaitingForImageLoading.set(url, []);
3546
3816
  return [4, getImageDetails(url)];
3547
- case 4:
3817
+ case 6:
3548
3818
  imageDetails = _a.sent();
3549
3819
  imageDetailsCache.set(url, imageDetails);
3550
3820
  awaitingForImageLoading
@@ -3553,13 +3823,13 @@
3553
3823
  return resolve(imageDetails);
3554
3824
  });
3555
3825
  awaitingForImageLoading.delete(url);
3556
- _a.label = 5;
3557
- case 5:
3826
+ _a.label = 7;
3827
+ case 7:
3558
3828
  if (isCancelled()) {
3559
3829
  return [2, null];
3560
3830
  }
3561
- return [3, 7];
3562
- case 6:
3831
+ return [3, 9];
3832
+ case 8:
3563
3833
  err_1 = _a.sent();
3564
3834
  logWarn(err_1);
3565
3835
  if (awaitingForImageLoading.has(url)) {
@@ -3570,14 +3840,31 @@
3570
3840
  });
3571
3841
  awaitingForImageLoading.delete(url);
3572
3842
  }
3573
- return [2, absoluteValue];
3574
- case 7:
3575
- bgImageValue =
3576
- getBgImageValue_1(
3843
+ return [3, 9];
3844
+ case 9:
3845
+ if (imageDetails) {
3846
+ bgImageValue = getBgImageValue_1(
3577
3847
  imageDetails,
3578
3848
  filter
3579
- ) || absoluteValue;
3580
- return [2, bgImageValue];
3849
+ );
3850
+ if (bgImageValue) {
3851
+ return [2, bgImageValue];
3852
+ }
3853
+ }
3854
+ if (!url.startsWith("data:"))
3855
+ return [3, 11];
3856
+ return [4, tryConvertDataURLToBlobURL(url)];
3857
+ case 10:
3858
+ blobURL = _a.sent();
3859
+ if (blobURL) {
3860
+ return [
3861
+ 2,
3862
+ 'url("'.concat(blobURL, '")')
3863
+ ];
3864
+ }
3865
+ _a.label = 11;
3866
+ case 11:
3867
+ return [2, 'url("'.concat(url, '")')];
3581
3868
  }
3582
3869
  });
3583
3870
  });
@@ -3590,21 +3877,20 @@
3590
3877
  isLarge = imageDetails.isLarge,
3591
3878
  width = imageDetails.width;
3592
3879
  var result;
3880
+ var logSrc = imageDetails.src.startsWith("data:")
3881
+ ? "data:"
3882
+ : imageDetails.src;
3593
3883
  if (isLarge) {
3594
- logInfo(
3595
- "Not modifying too large image ".concat(
3596
- imageDetails.src
3597
- )
3598
- );
3599
- result = 'url("'.concat(imageDetails.src, '")');
3884
+ logInfo("Not modifying too large image ".concat(logSrc));
3885
+ result = null;
3600
3886
  } else if (
3601
3887
  isDark &&
3602
3888
  isTransparent &&
3603
3889
  filter.mode === 1 &&
3604
3890
  width > 2
3605
3891
  ) {
3606
- logInfo("Inverting dark image ".concat(imageDetails.src));
3607
- var inverted = getFilteredImageDataURL(
3892
+ logInfo("Inverting dark image ".concat(logSrc));
3893
+ var inverted = getFilteredImageURL(
3608
3894
  imageDetails,
3609
3895
  __assign(__assign({}, filter), {
3610
3896
  sepia: clamp(filter.sepia + 10, 0, 100)
@@ -3612,14 +3898,12 @@
3612
3898
  );
3613
3899
  result = 'url("'.concat(inverted, '")');
3614
3900
  } else if (isLight && !isTransparent && filter.mode === 1) {
3615
- logInfo("Dimming light image ".concat(imageDetails.src));
3616
- var dimmed = getFilteredImageDataURL(imageDetails, filter);
3901
+ logInfo("Dimming light image ".concat(logSrc));
3902
+ var dimmed = getFilteredImageURL(imageDetails, filter);
3617
3903
  result = 'url("'.concat(dimmed, '")');
3618
3904
  } else if (filter.mode === 0 && isLight) {
3619
- logInfo(
3620
- "Applying filter to image ".concat(imageDetails.src)
3621
- );
3622
- var filtered = getFilteredImageDataURL(
3905
+ logInfo("Applying filter to image ".concat(logSrc));
3906
+ var filtered = getFilteredImageURL(
3623
3907
  imageDetails,
3624
3908
  __assign(__assign({}, filter), {
3625
3909
  brightness: clamp(filter.brightness - 10, 5, 200),
@@ -3628,11 +3912,7 @@
3628
3912
  );
3629
3913
  result = 'url("'.concat(filtered, '")');
3630
3914
  } else {
3631
- logInfo(
3632
- "Not modifying too large image ".concat(
3633
- imageDetails.src
3634
- )
3635
- );
3915
+ logInfo("Not modifying the image ".concat(logSrc));
3636
3916
  result = null;
3637
3917
  }
3638
3918
  return result;
@@ -3805,6 +4085,7 @@
3805
4085
  function VariablesStore() {
3806
4086
  this.varTypes = new Map();
3807
4087
  this.rulesQueue = [];
4088
+ this.inlineStyleQueue = [];
3808
4089
  this.definedVars = new Set();
3809
4090
  this.varRefs = new Map();
3810
4091
  this.unknownColorVars = new Set();
@@ -3818,6 +4099,7 @@
3818
4099
  VariablesStore.prototype.clear = function () {
3819
4100
  this.varTypes.clear();
3820
4101
  this.rulesQueue.splice(0);
4102
+ this.inlineStyleQueue.splice(0);
3821
4103
  this.definedVars.clear();
3822
4104
  this.varRefs.clear();
3823
4105
  this.unknownColorVars.clear();
@@ -3837,13 +4119,21 @@
3837
4119
  VariablesStore.prototype.addRulesForMatching = function (rules) {
3838
4120
  this.rulesQueue.push(rules);
3839
4121
  };
4122
+ VariablesStore.prototype.addInlineStyleForMatching = function (style) {
4123
+ this.inlineStyleQueue.push(style);
4124
+ };
3840
4125
  VariablesStore.prototype.matchVariablesAndDependents = function () {
3841
4126
  var _this = this;
4127
+ if (
4128
+ this.rulesQueue.length === 0 &&
4129
+ this.inlineStyleQueue.length === 0
4130
+ ) {
4131
+ return;
4132
+ }
3842
4133
  this.changedTypeVars.clear();
3843
4134
  this.initialVarTypes = new Map(this.varTypes);
3844
4135
  this.collectRootVariables();
3845
- this.collectVariablesAndVarDep(this.rulesQueue);
3846
- this.rulesQueue.splice(0);
4136
+ this.collectVariablesAndVarDep();
3847
4137
  this.collectRootVarDependents();
3848
4138
  this.varRefs.forEach(function (refs, v) {
3849
4139
  refs.forEach(function (r) {
@@ -4176,29 +4466,49 @@
4176
4466
  this.typeChangeSubscriptions.get(varName).delete(callback);
4177
4467
  }
4178
4468
  };
4179
- VariablesStore.prototype.collectVariablesAndVarDep = function (
4180
- ruleList
4181
- ) {
4469
+ VariablesStore.prototype.collectVariablesAndVarDep = function () {
4182
4470
  var _this = this;
4183
- ruleList.forEach(function (rules) {
4471
+ this.rulesQueue.forEach(function (rules) {
4184
4472
  iterateCSSRules(rules, function (rule) {
4185
- rule.style &&
4186
- iterateCSSDeclarations(
4187
- rule.style,
4188
- function (property, value) {
4189
- if (isVariable(property)) {
4190
- _this.inspectVariable(property, value);
4191
- }
4192
- if (isVarDependant(value)) {
4193
- _this.inspectVarDependant(property, value);
4194
- }
4195
- }
4196
- );
4473
+ if (rule.style) {
4474
+ _this.collectVarsFromCSSDeclarations(rule.style);
4475
+ }
4197
4476
  });
4198
4477
  });
4478
+ this.inlineStyleQueue.forEach(function (style) {
4479
+ _this.collectVarsFromCSSDeclarations(style);
4480
+ });
4481
+ this.rulesQueue.splice(0);
4482
+ this.inlineStyleQueue.splice(0);
4483
+ };
4484
+ VariablesStore.prototype.collectVarsFromCSSDeclarations = function (
4485
+ style
4486
+ ) {
4487
+ var _this = this;
4488
+ iterateCSSDeclarations(style, function (property, value) {
4489
+ if (isVariable(property)) {
4490
+ _this.inspectVariable(property, value);
4491
+ }
4492
+ if (isVarDependant(value)) {
4493
+ _this.inspectVarDependant(property, value);
4494
+ }
4495
+ });
4496
+ };
4497
+ VariablesStore.prototype.shouldProcessRootVariables = function () {
4498
+ var _a;
4499
+ return (
4500
+ this.rulesQueue.length > 0 &&
4501
+ ((_a = document.documentElement.getAttribute("style")) ===
4502
+ null || _a === void 0
4503
+ ? void 0
4504
+ : _a.includes("--"))
4505
+ );
4199
4506
  };
4200
4507
  VariablesStore.prototype.collectRootVariables = function () {
4201
4508
  var _this = this;
4509
+ if (!this.shouldProcessRootVariables()) {
4510
+ return;
4511
+ }
4202
4512
  iterateCSSDeclarations(
4203
4513
  document.documentElement.style,
4204
4514
  function (property, value) {
@@ -4247,6 +4557,9 @@
4247
4557
  };
4248
4558
  VariablesStore.prototype.collectRootVarDependents = function () {
4249
4559
  var _this = this;
4560
+ if (!this.shouldProcessRootVariables()) {
4561
+ return;
4562
+ }
4250
4563
  iterateCSSDeclarations(
4251
4564
  document.documentElement.style,
4252
4565
  function (property, value) {
@@ -4654,7 +4967,7 @@
4654
4967
  return replaced;
4655
4968
  }
4656
4969
 
4657
- var overrides = {
4970
+ var overrides$1 = {
4658
4971
  "background-color": {
4659
4972
  customProp: "--darkreader-inline-bgcolor",
4660
4973
  cssProp: "background-color",
@@ -4721,7 +5034,14 @@
4721
5034
  dataAttr: "data-darkreader-inline-stopcolor"
4722
5035
  }
4723
5036
  };
4724
- var overridesList = Object.values(overrides);
5037
+ var shorthandOverrides = {
5038
+ background: {
5039
+ customProp: "--darkreader-inline-bg",
5040
+ cssProp: "background",
5041
+ dataAttr: "data-darkreader-inline-bg"
5042
+ }
5043
+ };
5044
+ var overridesList = Object.values(overrides$1);
4725
5045
  var normalizedPropList = {};
4726
5046
  overridesList.forEach(function (_a) {
4727
5047
  var cssProp = _a.cssProp,
@@ -4740,7 +5060,10 @@
4740
5060
  return "[".concat(attr, "]");
4741
5061
  }).join(", ");
4742
5062
  function getInlineOverrideStyle() {
4743
- return overridesList
5063
+ var allOverrides = overridesList.concat(
5064
+ Object.values(shorthandOverrides)
5065
+ );
5066
+ return allOverrides
4744
5067
  .map(function (_a) {
4745
5068
  var dataAttr = _a.dataAttr,
4746
5069
  customProp = _a.customProp,
@@ -4815,6 +5138,7 @@
4815
5138
  shadowRootDiscovered
4816
5139
  );
4817
5140
  });
5141
+ variablesStore.matchVariablesAndDependents();
4818
5142
  }
4819
5143
  var treeObserver = createOptimizedTreeObserver(root, {
4820
5144
  onMinorMutations: function (_a) {
@@ -4847,6 +5171,7 @@
4847
5171
  elementStyleDidChange(target);
4848
5172
  }
4849
5173
  });
5174
+ variablesStore.matchVariablesAndDependents();
4850
5175
  });
4851
5176
  var attrObserver = new MutationObserver(function (mutations) {
4852
5177
  if (timeoutId) {
@@ -4931,16 +5256,13 @@
4931
5256
  ignoreInlineSelectors,
4932
5257
  ignoreImageSelectors
4933
5258
  ) {
5259
+ var _a;
4934
5260
  var cacheKey = getInlineStyleCacheKey(element, theme);
4935
5261
  if (cacheKey === inlineStyleCache.get(element)) {
4936
5262
  return;
4937
5263
  }
4938
- var unsetProps = new Set(Object.keys(overrides));
5264
+ var unsetProps = new Set(Object.keys(overrides$1));
4939
5265
  function setCustomProp(targetCSSProp, modifierCSSProp, cssVal) {
4940
- var isPropertyVariable = targetCSSProp.startsWith("--");
4941
- var _a = isPropertyVariable ? {} : overrides[targetCSSProp],
4942
- customProp = _a.customProp,
4943
- dataAttr = _a.dataAttr;
4944
5266
  var mod = getModifiableCSSDeclaration(
4945
5267
  modifierCSSProp,
4946
5268
  cssVal,
@@ -4952,30 +5274,65 @@
4952
5274
  if (!mod) {
4953
5275
  return;
4954
5276
  }
4955
- var value = mod.value;
4956
- if (typeof value === "function") {
4957
- value = value(theme);
4958
- }
4959
- if (isPropertyVariable && typeof value === "object") {
4960
- var typedValue = value;
4961
- typedValue.declarations.forEach(function (_a) {
4962
- var property = _a.property,
4963
- value = _a.value;
4964
- !(value instanceof Promise) &&
4965
- element.style.setProperty(property, value);
4966
- });
4967
- } else {
5277
+ function setStaticValue(value) {
5278
+ var _a;
5279
+ var _b =
5280
+ (_a = overrides$1[targetCSSProp]) !== null &&
5281
+ _a !== void 0
5282
+ ? _a
5283
+ : shorthandOverrides[targetCSSProp],
5284
+ customProp = _b.customProp,
5285
+ dataAttr = _b.dataAttr;
4968
5286
  element.style.setProperty(customProp, value);
4969
5287
  if (!element.hasAttribute(dataAttr)) {
4970
5288
  element.setAttribute(dataAttr, "");
4971
5289
  }
4972
5290
  unsetProps.delete(targetCSSProp);
4973
5291
  }
5292
+ function setVarDeclaration(mod) {
5293
+ var prevDeclarations = [];
5294
+ function setProps(declarations) {
5295
+ prevDeclarations.forEach(function (_a) {
5296
+ var property = _a.property;
5297
+ element.style.removeProperty(property);
5298
+ });
5299
+ declarations.forEach(function (_a) {
5300
+ var property = _a.property,
5301
+ value = _a.value;
5302
+ if (!(value instanceof Promise)) {
5303
+ element.style.setProperty(property, value);
5304
+ }
5305
+ });
5306
+ prevDeclarations = declarations;
5307
+ }
5308
+ setProps(mod.declarations);
5309
+ mod.onTypeChange.addListener(setProps);
5310
+ }
5311
+ function setAsyncValue(promise) {
5312
+ promise.then(function (value) {
5313
+ if (
5314
+ value &&
5315
+ targetCSSProp === "background" &&
5316
+ value.startsWith("var(--darkreader-bg--")
5317
+ ) {
5318
+ setStaticValue(value);
5319
+ }
5320
+ });
5321
+ }
5322
+ var value =
5323
+ typeof mod.value === "function" ? mod.value(theme) : mod.value;
5324
+ if (typeof value === "string") {
5325
+ setStaticValue(value);
5326
+ } else if (value instanceof Promise) {
5327
+ setAsyncValue(value);
5328
+ } else if (typeof value === "object") {
5329
+ setVarDeclaration(value);
5330
+ }
4974
5331
  }
4975
5332
  if (ignoreInlineSelectors.length > 0) {
4976
5333
  if (shouldIgnoreInlineStyle(element, ignoreInlineSelectors)) {
4977
5334
  unsetProps.forEach(function (cssProp) {
4978
- element.removeAttribute(overrides[cssProp].dataAttr);
5335
+ element.removeAttribute(overrides$1[cssProp].dataAttr);
4979
5336
  });
4980
5337
  return;
4981
5338
  }
@@ -5054,19 +5411,24 @@
5054
5411
  return;
5055
5412
  }
5056
5413
  if (
5057
- overrides.hasOwnProperty(property) ||
5414
+ overrides$1.hasOwnProperty(property) ||
5058
5415
  (property.startsWith("--") && !normalizedPropList[property])
5059
5416
  ) {
5060
5417
  setCustomProp(property, property, value);
5418
+ } else if (
5419
+ property === "background" &&
5420
+ value.includes("var(")
5421
+ ) {
5422
+ setCustomProp("background", "background", value);
5061
5423
  } else {
5062
- var overridenProp = normalizedPropList[property];
5424
+ var overriddenProp = normalizedPropList[property];
5063
5425
  if (
5064
- overridenProp &&
5065
- !element.style.getPropertyValue(overridenProp) &&
5066
- !element.hasAttribute(overridenProp)
5426
+ overriddenProp &&
5427
+ !element.style.getPropertyValue(overriddenProp) &&
5428
+ !element.hasAttribute(overriddenProp)
5067
5429
  ) {
5068
5430
  if (
5069
- overridenProp === "background-color" &&
5431
+ overriddenProp === "background-color" &&
5070
5432
  element.hasAttribute("bgcolor")
5071
5433
  ) {
5072
5434
  return;
@@ -5086,8 +5448,15 @@
5086
5448
  element.style.getPropertyValue("fill")
5087
5449
  );
5088
5450
  }
5451
+ if (
5452
+ (_a = element.getAttribute("style")) === null || _a === void 0
5453
+ ? void 0
5454
+ : _a.includes("--")
5455
+ ) {
5456
+ variablesStore.addInlineStyleForMatching(element.style);
5457
+ }
5089
5458
  forEach(unsetProps, function (cssProp) {
5090
- element.removeAttribute(overrides[cssProp].dataAttr);
5459
+ element.removeAttribute(overrides$1[cssProp].dataAttr);
5091
5460
  });
5092
5461
  inlineStyleCache.set(element, getInlineStyleCacheKey(element, theme));
5093
5462
  }
@@ -5254,25 +5623,9 @@
5254
5623
  }
5255
5624
  renderId++;
5256
5625
  function setRule(target, index, rule) {
5626
+ var e_1, _a;
5257
5627
  var selector = rule.selector,
5258
5628
  declarations = rule.declarations;
5259
- var getDeclarationText = function (dec) {
5260
- var property = dec.property,
5261
- value = dec.value,
5262
- important = dec.important,
5263
- sourceValue = dec.sourceValue;
5264
- return ""
5265
- .concat(property, ": ")
5266
- .concat(value == null ? sourceValue : value)
5267
- .concat(important ? " !important" : "", ";");
5268
- };
5269
- var cssRulesText = "";
5270
- declarations.forEach(function (declarations) {
5271
- cssRulesText += "".concat(
5272
- getDeclarationText(declarations),
5273
- " "
5274
- );
5275
- });
5276
5629
  var selectorText = selector;
5277
5630
  if (
5278
5631
  isChromium &&
@@ -5284,9 +5637,40 @@
5284
5637
  ) {
5285
5638
  selectorText = ".darkreader-unsupported-selector";
5286
5639
  }
5287
- var ruleText = ""
5288
- .concat(selectorText, " { ")
5289
- .concat(cssRulesText, " }");
5640
+ var ruleText = "".concat(selectorText, " {");
5641
+ try {
5642
+ for (
5643
+ var declarations_1 = __values(declarations),
5644
+ declarations_1_1 = declarations_1.next();
5645
+ !declarations_1_1.done;
5646
+ declarations_1_1 = declarations_1.next()
5647
+ ) {
5648
+ var dec = declarations_1_1.value;
5649
+ var property = dec.property,
5650
+ value = dec.value,
5651
+ important = dec.important;
5652
+ if (value) {
5653
+ ruleText += " "
5654
+ .concat(property, ": ")
5655
+ .concat(value)
5656
+ .concat(important ? " !important" : "", ";");
5657
+ }
5658
+ }
5659
+ } catch (e_1_1) {
5660
+ e_1 = {error: e_1_1};
5661
+ } finally {
5662
+ try {
5663
+ if (
5664
+ declarations_1_1 &&
5665
+ !declarations_1_1.done &&
5666
+ (_a = declarations_1.return)
5667
+ )
5668
+ _a.call(declarations_1);
5669
+ } finally {
5670
+ if (e_1) throw e_1.error;
5671
+ }
5672
+ }
5673
+ ruleText += " }";
5290
5674
  target.insertRule(ruleText, index);
5291
5675
  }
5292
5676
  var asyncDeclarations = new Map();
@@ -6600,10 +6984,22 @@
6600
6984
  unsubscribeFromDefineCustomElements();
6601
6985
  }
6602
6986
 
6603
- var adoptedStyleOverrides = new WeakMap();
6604
- var overrideList = new WeakSet();
6987
+ var overrides = new WeakSet();
6988
+ function hasAdoptedStyleSheets(node) {
6989
+ return (
6990
+ Array.isArray(node.adoptedStyleSheets) &&
6991
+ node.adoptedStyleSheets.length > 0
6992
+ );
6993
+ }
6605
6994
  function createAdoptedStyleSheetOverride(node) {
6606
6995
  var cancelAsyncOperations = false;
6996
+ function iterateSourceSheets(iterator) {
6997
+ node.adoptedStyleSheets.forEach(function (sheet) {
6998
+ if (!overrides.has(sheet)) {
6999
+ iterator(sheet);
7000
+ }
7001
+ });
7002
+ }
6607
7003
  function injectSheet(sheet, override) {
6608
7004
  var newSheets = __spreadArray(
6609
7005
  [],
@@ -6611,54 +7007,74 @@
6611
7007
  false
6612
7008
  );
6613
7009
  var sheetIndex = newSheets.indexOf(sheet);
6614
- var existingIndex = newSheets.indexOf(override);
6615
- if (sheetIndex === existingIndex - 1) {
6616
- return;
6617
- }
6618
- if (existingIndex >= 0) {
6619
- newSheets.splice(existingIndex, 1);
7010
+ var overrideIndex = newSheets.indexOf(override);
7011
+ if (overrideIndex >= 0) {
7012
+ newSheets.splice(overrideIndex, 1);
6620
7013
  }
6621
7014
  newSheets.splice(sheetIndex + 1, 0, override);
6622
7015
  node.adoptedStyleSheets = newSheets;
6623
7016
  }
6624
- function destroy() {
6625
- cancelAsyncOperations = true;
7017
+ function clear() {
6626
7018
  var newSheets = __spreadArray(
6627
7019
  [],
6628
7020
  __read(node.adoptedStyleSheets),
6629
7021
  false
6630
7022
  );
6631
- node.adoptedStyleSheets.forEach(function (adoptedStyleSheet) {
6632
- if (overrideList.has(adoptedStyleSheet)) {
6633
- var existingIndex = newSheets.indexOf(adoptedStyleSheet);
6634
- if (existingIndex >= 0) {
6635
- newSheets.splice(existingIndex, 1);
6636
- }
6637
- adoptedStyleOverrides.delete(adoptedStyleSheet);
6638
- overrideList.delete(adoptedStyleSheet);
7023
+ for (var i = newSheets.length - 1; i >= 0; i--) {
7024
+ var sheet = newSheets[i];
7025
+ if (overrides.has(sheet)) {
7026
+ newSheets.splice(i, 1);
7027
+ overrides.delete(sheet);
6639
7028
  }
7029
+ }
7030
+ if (node.adoptedStyleSheets.length !== newSheets.length) {
7031
+ node.adoptedStyleSheets = newSheets;
7032
+ }
7033
+ }
7034
+ function destroy() {
7035
+ cancelAsyncOperations = true;
7036
+ clear();
7037
+ if (frameId) {
7038
+ cancelAnimationFrame(frameId);
7039
+ frameId = null;
7040
+ }
7041
+ }
7042
+ var rulesChangeKey = 0;
7043
+ function getRulesChangeKey() {
7044
+ var count = 0;
7045
+ iterateSourceSheets(function (sheet) {
7046
+ count += sheet.cssRules.length;
6640
7047
  });
6641
- node.adoptedStyleSheets = newSheets;
7048
+ if (count === 1) {
7049
+ var rule = node.adoptedStyleSheets[0].cssRules[0];
7050
+ return rule instanceof CSSStyleRule ? rule.style.length : count;
7051
+ }
7052
+ return count;
6642
7053
  }
6643
7054
  function render(theme, ignoreImageAnalysis) {
6644
- node.adoptedStyleSheets.forEach(function (sheet) {
6645
- if (overrideList.has(sheet)) {
6646
- return;
7055
+ clear();
7056
+ var _loop_1 = function (i) {
7057
+ var sheet = node.adoptedStyleSheets[i];
7058
+ if (overrides.has(sheet)) {
7059
+ return "continue";
6647
7060
  }
6648
- var rules = sheet.rules;
7061
+ var rules = sheet.cssRules;
6649
7062
  var override = new CSSStyleSheet();
6650
- function prepareOverridesSheet() {
6651
- for (var i = override.cssRules.length - 1; i >= 0; i--) {
6652
- override.deleteRule(i);
7063
+ var prepareSheet = function () {
7064
+ for (
7065
+ var i_1 = override.cssRules.length - 1;
7066
+ i_1 >= 0;
7067
+ i_1--
7068
+ ) {
7069
+ override.deleteRule(i_1);
6653
7070
  }
6654
7071
  injectSheet(sheet, override);
6655
- adoptedStyleOverrides.set(sheet, override);
6656
- overrideList.add(override);
7072
+ overrides.add(override);
6657
7073
  return override;
6658
- }
7074
+ };
6659
7075
  var sheetModifier = createStyleSheetModifier();
6660
7076
  sheetModifier.modifySheet({
6661
- prepareSheet: prepareOverridesSheet,
7077
+ prepareSheet: prepareSheet,
6662
7078
  sourceCSSRules: rules,
6663
7079
  theme: theme,
6664
7080
  ignoreImageAnalysis: ignoreImageAnalysis,
@@ -6667,11 +7083,145 @@
6667
7083
  return cancelAsyncOperations;
6668
7084
  }
6669
7085
  });
7086
+ };
7087
+ for (var i = node.adoptedStyleSheets.length - 1; i >= 0; i--) {
7088
+ _loop_1(i);
7089
+ }
7090
+ rulesChangeKey = getRulesChangeKey();
7091
+ }
7092
+ function checkForUpdates() {
7093
+ return getRulesChangeKey() !== rulesChangeKey;
7094
+ }
7095
+ var frameId = null;
7096
+ function watch(callback) {
7097
+ frameId = requestAnimationFrame(function () {
7098
+ if (checkForUpdates()) {
7099
+ var sheets = node.adoptedStyleSheets.filter(function (s) {
7100
+ return !overrides.has(s);
7101
+ });
7102
+ callback(sheets);
7103
+ }
7104
+ watch(callback);
7105
+ });
7106
+ }
7107
+ return {
7108
+ render: render,
7109
+ destroy: destroy,
7110
+ watch: watch
7111
+ };
7112
+ }
7113
+ var StyleSheetCommandBuilder = (function () {
7114
+ function StyleSheetCommandBuilder(onChange) {
7115
+ this.cssRules = [];
7116
+ this.commands = [];
7117
+ this.onChange = onChange;
7118
+ }
7119
+ StyleSheetCommandBuilder.prototype.insertRule = function (
7120
+ cssText,
7121
+ index
7122
+ ) {
7123
+ if (index === void 0) {
7124
+ index = 0;
7125
+ }
7126
+ this.commands.push({
7127
+ type: "insert",
7128
+ index: index,
7129
+ cssText: cssText
7130
+ });
7131
+ this.cssRules.splice(
7132
+ index,
7133
+ 0,
7134
+ new StyleSheetCommandBuilder(this.onChange)
7135
+ );
7136
+ this.onChange();
7137
+ return index;
7138
+ };
7139
+ StyleSheetCommandBuilder.prototype.deleteRule = function (index) {
7140
+ this.commands.push({type: "delete", index: index});
7141
+ this.cssRules.splice(index, 1);
7142
+ this.onChange();
7143
+ };
7144
+ StyleSheetCommandBuilder.prototype.replaceSync = function (cssText) {
7145
+ this.commands.splice(0);
7146
+ this.commands.push({type: "replace", cssText: cssText});
7147
+ if (cssText === "") {
7148
+ this.cssRules.splice(0);
7149
+ } else {
7150
+ throw new Error(
7151
+ "StyleSheetCommandBuilder.replaceSync() is not fully supported"
7152
+ );
7153
+ }
7154
+ this.onChange();
7155
+ };
7156
+ StyleSheetCommandBuilder.prototype.getDeepCSSCommands = function () {
7157
+ var deep = [];
7158
+ this.commands.forEach(function (command) {
7159
+ deep.push({
7160
+ type: command.type,
7161
+ cssText: command.type !== "delete" ? command.cssText : "",
7162
+ path: command.type === "replace" ? [] : [command.index]
7163
+ });
7164
+ });
7165
+ this.cssRules.forEach(function (rule, i) {
7166
+ var childCommands = rule.getDeepCSSCommands();
7167
+ childCommands.forEach(function (c) {
7168
+ return c.path.unshift(i);
7169
+ });
7170
+ });
7171
+ return deep;
7172
+ };
7173
+ StyleSheetCommandBuilder.prototype.clearDeepCSSCommands = function () {
7174
+ this.commands.splice(0);
7175
+ this.cssRules.forEach(function (rule) {
7176
+ return rule.clearDeepCSSCommands();
7177
+ });
7178
+ };
7179
+ return StyleSheetCommandBuilder;
7180
+ })();
7181
+ function createAdoptedStyleSheetFallback(onChange) {
7182
+ var cancelAsyncOperations = false;
7183
+ var sourceCSSRules = [];
7184
+ var lastTheme;
7185
+ var lastIgnoreImageAnalysis;
7186
+ function updateCSS(cssRules) {
7187
+ sourceCSSRules = cssRules;
7188
+ if (lastTheme && lastIgnoreImageAnalysis) {
7189
+ render(lastTheme, lastIgnoreImageAnalysis);
7190
+ }
7191
+ }
7192
+ var builder = new StyleSheetCommandBuilder(onChange);
7193
+ function render(theme, ignoreImageAnalysis) {
7194
+ lastTheme = theme;
7195
+ lastIgnoreImageAnalysis = ignoreImageAnalysis;
7196
+ var prepareSheet = function () {
7197
+ builder.replaceSync("");
7198
+ return builder;
7199
+ };
7200
+ var sheetModifier = createStyleSheetModifier();
7201
+ sheetModifier.modifySheet({
7202
+ prepareSheet: prepareSheet,
7203
+ sourceCSSRules: sourceCSSRules,
7204
+ theme: theme,
7205
+ ignoreImageAnalysis: ignoreImageAnalysis,
7206
+ force: false,
7207
+ isAsyncCancelled: function () {
7208
+ return cancelAsyncOperations;
7209
+ }
6670
7210
  });
6671
7211
  }
7212
+ function commands() {
7213
+ var commands = builder.getDeepCSSCommands();
7214
+ builder.clearDeepCSSCommands();
7215
+ return commands;
7216
+ }
7217
+ function destroy() {
7218
+ cancelAsyncOperations = true;
7219
+ }
6672
7220
  return {
6673
7221
  render: render,
6674
- destroy: destroy
7222
+ destroy: destroy,
7223
+ updateCSS: updateCSS,
7224
+ commands: commands
6675
7225
  };
6676
7226
  }
6677
7227
 
@@ -6698,6 +7248,14 @@
6698
7248
  CSSStyleSheet.prototype,
6699
7249
  "removeRule"
6700
7250
  );
7251
+ var replaceDescriptor = Object.getOwnPropertyDescriptor(
7252
+ CSSStyleSheet.prototype,
7253
+ "replace"
7254
+ );
7255
+ var replaceSyncDescriptor = Object.getOwnPropertyDescriptor(
7256
+ CSSStyleSheet.prototype,
7257
+ "replaceSync"
7258
+ );
6701
7259
  var documentStyleSheetsDescriptor = enableStyleSheetsProxy
6702
7260
  ? Object.getOwnPropertyDescriptor(Document.prototype, "styleSheets")
6703
7261
  : null;
@@ -6730,6 +7288,7 @@
6730
7288
  var childNodesDescriptor = shouldProxyChildNodes
6731
7289
  ? Object.getOwnPropertyDescriptor(Node.prototype, "childNodes")
6732
7290
  : null;
7291
+ var cleaners = [];
6733
7292
  var cleanUp = function () {
6734
7293
  Object.defineProperty(
6735
7294
  CSSStyleSheet.prototype,
@@ -6751,11 +7310,25 @@
6751
7310
  "removeRule",
6752
7311
  removeRuleDescriptor
6753
7312
  );
7313
+ Object.defineProperty(
7314
+ CSSStyleSheet.prototype,
7315
+ "replace",
7316
+ replaceDescriptor
7317
+ );
7318
+ Object.defineProperty(
7319
+ CSSStyleSheet.prototype,
7320
+ "replaceSync",
7321
+ replaceSyncDescriptor
7322
+ );
6754
7323
  document.removeEventListener("__darkreader__cleanUp", cleanUp);
6755
7324
  document.removeEventListener(
6756
7325
  "__darkreader__addUndefinedResolver",
6757
7326
  addUndefinedResolver
6758
7327
  );
7328
+ document.removeEventListener(
7329
+ "__darkreader__blobURLCheckRequest",
7330
+ checkBlobURLSupport
7331
+ );
6759
7332
  if (enableStyleSheetsProxy) {
6760
7333
  Object.defineProperty(
6761
7334
  Document.prototype,
@@ -6784,6 +7357,10 @@
6784
7357
  childNodesDescriptor
6785
7358
  );
6786
7359
  }
7360
+ cleaners.forEach(function (clean) {
7361
+ return clean();
7362
+ });
7363
+ cleaners.splice(0);
6787
7364
  };
6788
7365
  var addUndefinedResolverInner = function (tag) {
6789
7366
  customElements.whenDefined(tag).then(function () {
@@ -6805,6 +7382,11 @@
6805
7382
  addUndefinedResolver,
6806
7383
  {passive: true}
6807
7384
  );
7385
+ document.addEventListener(
7386
+ "__darkreader__blobURLCheckRequest",
7387
+ checkBlobURLSupport,
7388
+ {once: true}
7389
+ );
6808
7390
  var updateSheetEvent = new Event("__darkreader__updateSheet");
6809
7391
  function proxyAddRule(selector, style, index) {
6810
7392
  addRuleDescriptor.value.call(this, selector, style, index);
@@ -6860,6 +7442,36 @@
6860
7442
  this.ownerNode.dispatchEvent(updateSheetEvent);
6861
7443
  }
6862
7444
  }
7445
+ function proxyReplace(cssText) {
7446
+ var _this = this;
7447
+ var returnValue = replaceDescriptor.value.call(this, cssText);
7448
+ if (
7449
+ this.ownerNode &&
7450
+ !(
7451
+ this.ownerNode.classList &&
7452
+ this.ownerNode.classList.contains("darkreader")
7453
+ ) &&
7454
+ returnValue &&
7455
+ returnValue instanceof Promise
7456
+ ) {
7457
+ returnValue.then(function () {
7458
+ return _this.ownerNode.dispatchEvent(updateSheetEvent);
7459
+ });
7460
+ }
7461
+ return returnValue;
7462
+ }
7463
+ function proxyReplaceSync(cssText) {
7464
+ replaceSyncDescriptor.value.call(this, cssText);
7465
+ if (
7466
+ this.ownerNode &&
7467
+ !(
7468
+ this.ownerNode.classList &&
7469
+ this.ownerNode.classList.contains("darkreader")
7470
+ )
7471
+ ) {
7472
+ this.ownerNode.dispatchEvent(updateSheetEvent);
7473
+ }
7474
+ }
6863
7475
  function proxyDocumentStyleSheets() {
6864
7476
  var _this = this;
6865
7477
  var getCurrentValue = function () {
@@ -6955,31 +7567,99 @@
6955
7567
  NodeList.prototype
6956
7568
  );
6957
7569
  }
7570
+ function checkBlobURLSupport() {
7571
+ return __awaiter(this, void 0, void 0, function () {
7572
+ var svg, bytes, i, blob, objectURL, blobURLAllowed, image_1;
7573
+ return __generator(this, function (_a) {
7574
+ switch (_a.label) {
7575
+ case 0:
7576
+ svg =
7577
+ '<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"><rect width="1" height="1" fill="transparent"/></svg>';
7578
+ bytes = new Uint8Array(svg.length);
7579
+ for (i = 0; i < svg.length; i++) {
7580
+ bytes[i] = svg.charCodeAt(i);
7581
+ }
7582
+ blob = new Blob([bytes], {type: "image/svg+xml"});
7583
+ objectURL = URL.createObjectURL(blob);
7584
+ _a.label = 1;
7585
+ case 1:
7586
+ _a.trys.push([1, 3, , 4]);
7587
+ image_1 = new Image();
7588
+ return [
7589
+ 4,
7590
+ new Promise(function (resolve, reject) {
7591
+ image_1.onload = function () {
7592
+ return resolve();
7593
+ };
7594
+ image_1.onerror = function () {
7595
+ return reject();
7596
+ };
7597
+ image_1.src = objectURL;
7598
+ })
7599
+ ];
7600
+ case 2:
7601
+ _a.sent();
7602
+ blobURLAllowed = true;
7603
+ return [3, 4];
7604
+ case 3:
7605
+ _a.sent();
7606
+ blobURLAllowed = false;
7607
+ return [3, 4];
7608
+ case 4:
7609
+ document.dispatchEvent(
7610
+ new CustomEvent(
7611
+ "__darkreader__blobURLCheckResponse",
7612
+ {detail: {blobURLAllowed: blobURLAllowed}}
7613
+ )
7614
+ );
7615
+ return [2];
7616
+ }
7617
+ });
7618
+ });
7619
+ }
6958
7620
  Object.defineProperty(
6959
7621
  CSSStyleSheet.prototype,
6960
7622
  "addRule",
6961
- Object.assign({}, addRuleDescriptor, {value: proxyAddRule})
7623
+ __assign(__assign({}, addRuleDescriptor), {value: proxyAddRule})
6962
7624
  );
6963
7625
  Object.defineProperty(
6964
7626
  CSSStyleSheet.prototype,
6965
7627
  "insertRule",
6966
- Object.assign({}, insertRuleDescriptor, {value: proxyInsertRule})
7628
+ __assign(__assign({}, insertRuleDescriptor), {
7629
+ value: proxyInsertRule
7630
+ })
6967
7631
  );
6968
7632
  Object.defineProperty(
6969
7633
  CSSStyleSheet.prototype,
6970
7634
  "deleteRule",
6971
- Object.assign({}, deleteRuleDescriptor, {value: proxyDeleteRule})
7635
+ __assign(__assign({}, deleteRuleDescriptor), {
7636
+ value: proxyDeleteRule
7637
+ })
6972
7638
  );
6973
7639
  Object.defineProperty(
6974
7640
  CSSStyleSheet.prototype,
6975
7641
  "removeRule",
6976
- Object.assign({}, removeRuleDescriptor, {value: proxyRemoveRule})
7642
+ __assign(__assign({}, removeRuleDescriptor), {
7643
+ value: proxyRemoveRule
7644
+ })
7645
+ );
7646
+ Object.defineProperty(
7647
+ CSSStyleSheet.prototype,
7648
+ "replace",
7649
+ __assign(__assign({}, replaceDescriptor), {value: proxyReplace})
7650
+ );
7651
+ Object.defineProperty(
7652
+ CSSStyleSheet.prototype,
7653
+ "replaceSync",
7654
+ __assign(__assign({}, replaceSyncDescriptor), {
7655
+ value: proxyReplaceSync
7656
+ })
6977
7657
  );
6978
7658
  if (enableStyleSheetsProxy) {
6979
7659
  Object.defineProperty(
6980
7660
  Document.prototype,
6981
7661
  "styleSheets",
6982
- Object.assign({}, documentStyleSheetsDescriptor, {
7662
+ __assign(__assign({}, documentStyleSheetsDescriptor), {
6983
7663
  get: proxyDocumentStyleSheets
6984
7664
  })
6985
7665
  );
@@ -6988,7 +7668,7 @@
6988
7668
  Object.defineProperty(
6989
7669
  CustomElementRegistry.prototype,
6990
7670
  "define",
6991
- Object.assign({}, customElementRegistryDefineDescriptor, {
7671
+ __assign(__assign({}, customElementRegistryDefineDescriptor), {
6992
7672
  value: proxyCustomElementRegistryDefine
6993
7673
  })
6994
7674
  );
@@ -6997,7 +7677,7 @@
6997
7677
  Object.defineProperty(
6998
7678
  Element.prototype,
6999
7679
  "getElementsByTagName",
7000
- Object.assign({}, getElementsByTagNameDescriptor, {
7680
+ __assign(__assign({}, getElementsByTagNameDescriptor), {
7001
7681
  value: proxyGetElementsByTagName
7002
7682
  })
7003
7683
  );
@@ -7006,7 +7686,9 @@
7006
7686
  Object.defineProperty(
7007
7687
  Node.prototype,
7008
7688
  "childNodes",
7009
- Object.assign({}, childNodesDescriptor, {get: proxyChildNodes})
7689
+ __assign(__assign({}, childNodesDescriptor), {
7690
+ get: proxyChildNodes
7691
+ })
7010
7692
  );
7011
7693
  }
7012
7694
  }
@@ -7075,6 +7757,9 @@
7075
7757
  var INSTANCE_ID = generateUID();
7076
7758
  var styleManagers = new Map();
7077
7759
  var adoptedStyleManagers = [];
7760
+ var adoptedStyleFallbacks = new Map();
7761
+ var adoptedStyleNodeIds = new WeakMap();
7762
+ var adoptedStyleChangeTokens = new WeakMap();
7078
7763
  var filter = null;
7079
7764
  var fixes = null;
7080
7765
  var isIFrame$1 = null;
@@ -7411,6 +8096,56 @@
7411
8096
  );
7412
8097
  });
7413
8098
  handleAdoptedStyleSheets(document);
8099
+ variablesStore.matchVariablesAndDependents();
8100
+ if (isFirefox) {
8101
+ var MATCH_VAR_1 = Symbol();
8102
+ var onAdoptedCSSChange_1 = function (e) {
8103
+ var _a = e.detail,
8104
+ node = _a.node,
8105
+ id = _a.id,
8106
+ cssRules = _a.cssRules,
8107
+ entries = _a.entries;
8108
+ if (Array.isArray(entries)) {
8109
+ entries.forEach(function (e) {
8110
+ var cssRules = e[2];
8111
+ variablesStore.addRulesForMatching(cssRules);
8112
+ });
8113
+ variablesStore.matchVariablesAndDependents();
8114
+ } else if (cssRules) {
8115
+ variablesStore.addRulesForMatching(cssRules);
8116
+ requestAnimationFrameOnce(MATCH_VAR_1, function () {
8117
+ return variablesStore.matchVariablesAndDependents();
8118
+ });
8119
+ }
8120
+ var tuples = Array.isArray(entries)
8121
+ ? entries
8122
+ : node && cssRules
8123
+ ? [[node, id, cssRules]]
8124
+ : [];
8125
+ tuples.forEach(function (_a) {
8126
+ var _b = __read(_a, 3),
8127
+ node = _b[0],
8128
+ id = _b[1],
8129
+ cssRules = _b[2];
8130
+ adoptedStyleNodeIds.set(node, id);
8131
+ var fallback = getAdoptedStyleSheetFallback(node);
8132
+ fallback.updateCSS(cssRules);
8133
+ });
8134
+ };
8135
+ document.addEventListener(
8136
+ "__darkreader__adoptedStyleSheetsChange",
8137
+ onAdoptedCSSChange_1
8138
+ );
8139
+ cleaners.push(function () {
8140
+ return document.removeEventListener(
8141
+ "__darkreader__adoptedStyleSheetsChange",
8142
+ onAdoptedCSSChange_1
8143
+ );
8144
+ });
8145
+ document.dispatchEvent(
8146
+ new CustomEvent("__darkreader__startAdoptedStyleSheetsWatcher")
8147
+ );
8148
+ }
7414
8149
  }
7415
8150
  var loadingStylesCounter = 0;
7416
8151
  var loadingStyles = new Set();
@@ -7501,16 +8236,94 @@
7501
8236
  }
7502
8237
  changeMetaThemeColorWhenAvailable(filter);
7503
8238
  }
8239
+ var pendingAdoptedVarMatch = false;
7504
8240
  function handleAdoptedStyleSheets(node) {
7505
- try {
7506
- if (Array.isArray(node.adoptedStyleSheets)) {
7507
- if (node.adoptedStyleSheets.length > 0) {
7508
- var newManger = createAdoptedStyleSheetOverride(node);
7509
- adoptedStyleManagers.push(newManger);
7510
- newManger.render(filter, ignoredImageAnalysisSelectors);
8241
+ var theme = filter;
8242
+ if (isFirefox) {
8243
+ var fallback = getAdoptedStyleSheetFallback(node);
8244
+ fallback.render(theme, ignoredImageAnalysisSelectors);
8245
+ return;
8246
+ }
8247
+ if (hasAdoptedStyleSheets(node)) {
8248
+ node.adoptedStyleSheets.forEach(function (s) {
8249
+ variablesStore.addRulesForMatching(s.cssRules);
8250
+ });
8251
+ var newManger_1 = createAdoptedStyleSheetOverride(node);
8252
+ adoptedStyleManagers.push(newManger_1);
8253
+ newManger_1.render(theme, ignoredImageAnalysisSelectors);
8254
+ newManger_1.watch(function (sheets) {
8255
+ sheets.forEach(function (s) {
8256
+ variablesStore.addRulesForMatching(s.cssRules);
8257
+ });
8258
+ newManger_1.render(theme, ignoredImageAnalysisSelectors);
8259
+ pendingAdoptedVarMatch = true;
8260
+ });
8261
+ potentialAdoptedStyleNodes.delete(node);
8262
+ } else if (!potentialAdoptedStyleNodes.has(node)) {
8263
+ potentialAdoptedStyleNodes.add(node);
8264
+ }
8265
+ }
8266
+ var potentialAdoptedStyleNodes = new Set();
8267
+ var potentialAdoptedStyleFrameId = null;
8268
+ function watchPotentialAdoptedStyleNodes() {
8269
+ potentialAdoptedStyleFrameId = requestAnimationFrame(function () {
8270
+ var changed = false;
8271
+ potentialAdoptedStyleNodes.forEach(function (node) {
8272
+ if (node.isConnected) {
8273
+ handleAdoptedStyleSheets(node);
8274
+ changed = true;
8275
+ } else {
8276
+ potentialAdoptedStyleNodes.delete(node);
7511
8277
  }
8278
+ });
8279
+ if (changed || pendingAdoptedVarMatch) {
8280
+ variablesStore.matchVariablesAndDependents();
8281
+ pendingAdoptedVarMatch = false;
7512
8282
  }
7513
- } catch (err) {}
8283
+ watchPotentialAdoptedStyleNodes();
8284
+ });
8285
+ }
8286
+ function stopWatchingPotentialAdoptedStyleNodes() {
8287
+ potentialAdoptedStyleFrameId &&
8288
+ cancelAnimationFrame(potentialAdoptedStyleFrameId);
8289
+ potentialAdoptedStyleNodes.clear();
8290
+ if (isFirefox) {
8291
+ document.dispatchEvent(
8292
+ new CustomEvent("__darkreader__stopAdoptedStyleSheetsWatcher")
8293
+ );
8294
+ }
8295
+ }
8296
+ function getAdoptedStyleChangeToken(node) {
8297
+ if (adoptedStyleChangeTokens.has(node)) {
8298
+ return adoptedStyleChangeTokens.get(node);
8299
+ }
8300
+ var token = Symbol();
8301
+ adoptedStyleChangeTokens.set(node, token);
8302
+ return token;
8303
+ }
8304
+ function getAdoptedStyleSheetFallback(node) {
8305
+ var fallback = adoptedStyleFallbacks.get(node);
8306
+ if (!fallback) {
8307
+ fallback = createAdoptedStyleSheetFallback(function () {
8308
+ var token = getAdoptedStyleChangeToken(node);
8309
+ requestAnimationFrameOnce(token, function () {
8310
+ var id = adoptedStyleNodeIds.get(node);
8311
+ var commands =
8312
+ fallback === null || fallback === void 0
8313
+ ? void 0
8314
+ : fallback.commands();
8315
+ var data = {id: id, commands: commands};
8316
+ document.dispatchEvent(
8317
+ new CustomEvent(
8318
+ "__darkreader__adoptedStyleSheetCommands",
8319
+ {detail: JSON.stringify(data)}
8320
+ )
8321
+ );
8322
+ });
8323
+ });
8324
+ adoptedStyleFallbacks.set(node, fallback);
8325
+ }
8326
+ return fallback;
7514
8327
  }
7515
8328
  function watchForUpdates() {
7516
8329
  var managedStyles = Array.from(styleManagers.keys());
@@ -7566,6 +8379,7 @@
7566
8379
  handleAdoptedStyleSheets(shadowRoot);
7567
8380
  }
7568
8381
  );
8382
+ watchPotentialAdoptedStyleNodes();
7569
8383
  watchForInlineStyles(
7570
8384
  function (element) {
7571
8385
  overrideInlineStyle(
@@ -7710,6 +8524,7 @@
7710
8524
  document.dispatchEvent(new CustomEvent("__darkreader__cleanUp"));
7711
8525
  removeNode(document.head.querySelector(".darkreader--proxy"));
7712
8526
  }
8527
+ var cleaners = [];
7713
8528
  function removeDynamicTheme() {
7714
8529
  document.documentElement.removeAttribute("data-darkreader-mode");
7715
8530
  document.documentElement.removeAttribute("data-darkreader-scheme");
@@ -7739,10 +8554,19 @@
7739
8554
  cleanLoadingLinks();
7740
8555
  forEach(document.querySelectorAll(".darkreader"), removeNode);
7741
8556
  adoptedStyleManagers.forEach(function (manager) {
7742
- manager.destroy();
8557
+ return manager.destroy();
7743
8558
  });
7744
8559
  adoptedStyleManagers.splice(0);
8560
+ adoptedStyleFallbacks.forEach(function (fallback) {
8561
+ return fallback.destroy();
8562
+ });
8563
+ adoptedStyleFallbacks.clear();
8564
+ stopWatchingPotentialAdoptedStyleNodes();
7745
8565
  metaObserver && metaObserver.disconnect();
8566
+ cleaners.forEach(function (clean) {
8567
+ return clean();
8568
+ });
8569
+ cleaners.splice(0);
7746
8570
  }
7747
8571
  function cleanDynamicThemeCache() {
7748
8572
  variablesStore.clear();