Explorar el Código

catch and ignore exceptions when using the localStorage API

Gildas hace 7 años
padre
commit
2f45144b2d
Se han modificado 1 ficheros con 30 adiciones y 6 borrados
  1. 30 6
      extension/ui/bg/ui-options.js

+ 30 - 6
extension/ui/bg/ui-options.js

@@ -196,27 +196,27 @@
 			ruleEditButton.disabled = true;
 		}
 	};
-	if (localStorage.getItem("optionShowAutoSaveProfile")) {
+	if (getLocalStorageItem("optionShowAutoSaveProfile")) {
 		showAutoSaveProfileInput.checked = true;
 		rulesContainerElement.classList.remove("compact");
 	}
 	showAutoSaveProfileInput.addEventListener("click", () => {
 		if (showAutoSaveProfileInput.checked) {
-			localStorage.setItem("optionShowAutoSaveProfile", 1);
+			setLocalStorageItem("optionShowAutoSaveProfile", 1);
 			rulesContainerElement.classList.remove("compact");
 		} else {
-			localStorage.removeItem("optionShowAutoSaveProfile");
+			removeLocalStorageItem("optionShowAutoSaveProfile");
 			rulesContainerElement.classList.add("compact");
 		}
 	}, false);
-	if (localStorage.getItem("optionShowAllProfiles")) {
+	if (getLocalStorageItem("optionShowAllProfiles")) {
 		showAllProfilesInput.checked = true;
 	}
 	showAllProfilesInput.addEventListener("click", () => {
 		if (showAllProfilesInput.checked) {
-			localStorage.setItem("optionShowAllProfiles", 1);
+			setLocalStorageItem("optionShowAllProfiles", 1);
 		} else {
-			localStorage.removeItem("optionShowAllProfiles");
+			removeLocalStorageItem("optionShowAllProfiles");
 		}
 	}, false);
 	addProfileButton.addEventListener("click", async () => {
@@ -703,4 +703,28 @@
 		});
 	}
 
+	function getLocalStorageItem(key) {
+		try {
+			return localStorage.getItem(key);
+		} catch (error) {
+			// ignored
+		}
+	}
+
+	function setLocalStorageItem(key, value) {
+		try {
+			return localStorage.setItem(key, value);
+		} catch (error) {
+			// ignored
+		}
+	}
+
+	function removeLocalStorageItem(key) {
+		try {
+			return localStorage.removeItem(key);
+		} catch (error) {
+			// ignored
+		}
+	}
+
 })();