browser.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. var initPrefersColorScheme = (function () {
  2. 'use strict';
  3. var colorIndexRegExp = /((?:not )?all and )?(\(color-index: *(22|48|70)\))/i;
  4. var prefersColorSchemeRegExp = /prefers-color-scheme:/i;
  5. var prefersColorSchemeInit = function prefersColorSchemeInit(initialColorScheme) {
  6. var mediaQueryString = '(prefers-color-scheme: dark)';
  7. var mediaQueryList = window.matchMedia && matchMedia(mediaQueryString);
  8. var hasNativeSupport = mediaQueryList && mediaQueryList.media === mediaQueryString;
  9. var mediaQueryListener = function mediaQueryListener() {
  10. set(mediaQueryList.matches ? 'dark' : 'light');
  11. };
  12. var removeListener = function removeListener() {
  13. if (mediaQueryList) {
  14. mediaQueryList.removeListener(mediaQueryListener);
  15. }
  16. };
  17. var set = function set(colorScheme) {
  18. if (colorScheme !== currentColorScheme) {
  19. currentColorScheme = colorScheme;
  20. if (typeof result.onChange === 'function') {
  21. result.onChange();
  22. }
  23. }
  24. [].forEach.call(document.styleSheets || [], function (styleSheet) {
  25. [].forEach.call(styleSheet.cssRules || [], function (cssRule) {
  26. var colorSchemeMatch = prefersColorSchemeRegExp.test(Object(cssRule.media).mediaText);
  27. if (colorSchemeMatch) {
  28. var index = [].indexOf.call(cssRule.parentStyleSheet.cssRules, cssRule);
  29. cssRule.parentStyleSheet.deleteRule(index);
  30. } else {
  31. var colorIndexMatch = (Object(cssRule.media).mediaText || '').match(colorIndexRegExp);
  32. if (colorIndexMatch) {
  33. cssRule.media.mediaText = ((/^dark$/i.test(colorScheme) ? colorIndexMatch[3] === '48' : /^light$/i.test(colorScheme) ? colorIndexMatch[3] === '70' : colorIndexMatch[3] === '22') ? 'not all and ' : '') + cssRule.media.mediaText.replace(colorIndexRegExp, '$2');
  34. }
  35. }
  36. });
  37. });
  38. };
  39. var result = Object.defineProperty({
  40. hasNativeSupport: hasNativeSupport,
  41. removeListener: removeListener
  42. }, 'scheme', {
  43. get: function get() {
  44. return currentColorScheme;
  45. },
  46. set: set
  47. }); // initialize the color scheme using the provided value, the system value, or light
  48. var currentColorScheme = initialColorScheme || (mediaQueryList && mediaQueryList.matches ? 'dark' : 'light');
  49. set(currentColorScheme); // listen for system changes
  50. if (mediaQueryList) {
  51. mediaQueryList.addListener(mediaQueryListener);
  52. }
  53. return result;
  54. };
  55. return prefersColorSchemeInit;
  56. }());
  57. //# sourceMappingURL=browser.js.map