diff --git a/freesound/static/bw-frontend/src/components/uiThemeDetector.js b/freesound/static/bw-frontend/src/components/uiThemeDetector.js index 08e2ea8bc..606c443f7 100644 --- a/freesound/static/bw-frontend/src/components/uiThemeDetector.js +++ b/freesound/static/bw-frontend/src/components/uiThemeDetector.js @@ -20,17 +20,18 @@ const isUsingDarkTheme = () => { return styleLink.getAttribute('href').indexOf('dark') > -1; } -window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => { - const newColorScheme = event.matches ? "dark" : "light"; - if (styleLink.dataset.forcedTheme === undefined){ - if (newColorScheme == "dark"){ - switchToDarkMode(); - } else if (newColorScheme == "dark"){ - switchToLightMode(); +if (window.matchMedia('(prefers-color-scheme: dark)').addEventListener !== undefined){ // Some old browsers don't support that + window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => { + const newColorScheme = event.matches ? "dark" : "light"; + if (styleLink.dataset.forcedTheme === undefined){ + if (newColorScheme == "dark"){ + switchToDarkMode(); + } else if (newColorScheme == "dark"){ + switchToLightMode(); + } } - } -}); - + }); +} if (browserPrefersDarkTheme()){ document.cookie = "systemPrefersDarkTheme=yes;path=/"; diff --git a/freesound/static/bw-frontend/src/index.js b/freesound/static/bw-frontend/src/index.js index aecf93ae6..a546a7353 100644 --- a/freesound/static/bw-frontend/src/index.js +++ b/freesound/static/bw-frontend/src/index.js @@ -9,3 +9,4 @@ import 'normalize.css' import '../styles/index.scss' import './components' +import './utils/polyfills' \ No newline at end of file diff --git a/freesound/static/bw-frontend/src/utils/polyfills.js b/freesound/static/bw-frontend/src/utils/polyfills.js new file mode 100644 index 000000000..694500eaf --- /dev/null +++ b/freesound/static/bw-frontend/src/utils/polyfills.js @@ -0,0 +1,10 @@ +if (!String.prototype.replaceAll) { + String.prototype.replaceAll = function(str, newStr){ + // If a regex pattern + if (Object.prototype.toString.call(str).toLowerCase() === '[object regexp]') { + return this.replace(str, newStr); + } + // If a string + return this.replace(new RegExp(str, 'g'), newStr); + }; +} \ No newline at end of file