Browse Source

fix: ignore localStorage error

Gerald 6 years ago
parent
commit
175d4abc1f
1 changed files with 13 additions and 3 deletions
  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) => {
 const setStyle = (css) => {
   if (css && !style) {
   if (css && !style) {
     style = document.createElement('style');
     style = document.createElement('style');
-    document.head.appendChild(style);
+    document.documentElement.appendChild(style);
   }
   }
   if (css || style) {
   if (css || style) {
     css = css || '';
     css = css || '';
     style.textContent = 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) => {
 options.hook((changes) => {
   if ('customCSS' in changes) {
   if ('customCSS' in changes) {