Преглед на файлове

fix: ignore localStorage error

Gerald преди 6 години
родител
ревизия
175d4abc1f
променени са 1 файла, в които са добавени 13 реда и са изтрити 3 реда
  1. 13 3
      src/common/ui/style/index.js

+ 13 - 3
src/common/ui/style/index.js

@@ -7,16 +7,26 @@ const CACHE_KEY = 'cacheCustomCSS';
 const setStyle = (css) => {
   if (css && !style) {
     style = document.createElement('style');
-    document.head.appendChild(style);
+    document.documentElement.appendChild(style);
   }
   if (css || style) {
     css = css || '';
     style.textContent = css;
-    localStorage.setItem(CACHE_KEY, css);
+    try {
+      localStorage.setItem(CACHE_KEY, css);
+    } catch {
+      // ignore
+    }
   }
 };
 
-setStyle(localStorage.getItem(CACHE_KEY));
+// In some versions of Firefox, `localStorage` is not allowed to be accessed
+// in Private Browsing mode.
+try {
+  setStyle(localStorage.getItem(CACHE_KEY));
+} catch {
+  // ignore
+}
 
 options.hook((changes) => {
   if ('customCSS' in changes) {