Przeglądaj źródła

removed/refactored try/catch blocks

Gildas 6 lat temu
rodzic
commit
a1c1f93fea

+ 20 - 24
extension/ui/bg/ui-menu.js

@@ -66,6 +66,7 @@ singlefile.extension.ui.bg.menu = (() => {
 	const menusCheckedState = new Map();
 	const menusTitleState = new Map();
 	let profileIndexes = new Map();
+	let menusCreated;
 	initialize();
 	return {
 		onMessage,
@@ -306,6 +307,7 @@ singlefile.extension.ui.bg.menu = (() => {
 			});
 			menusCheckedState.set(MENU_ID_AUTO_SAVE_ALL, false);
 		}
+		menusCreated = true;
 	}
 
 	async function initialize() {
@@ -411,25 +413,25 @@ singlefile.extension.ui.bg.menu = (() => {
 
 	async function refreshTab(tab) {
 		const config = singlefile.extension.core.bg.config;
-		if (BROWSER_MENUS_API_SUPPORTED) {
+		if (BROWSER_MENUS_API_SUPPORTED && menusCreated) {
 			const tabsData = await singlefile.extension.core.bg.tabsData.get(tab.id);
 			const promises = [];
-			try {
-				promises.push(updateCheckedValue(MENU_ID_AUTO_SAVE_DISABLED, !tabsData[tab.id].autoSave));
-				promises.push(updateCheckedValue(MENU_ID_AUTO_SAVE_TAB, tabsData[tab.id].autoSave));
-				promises.push(updateCheckedValue(MENU_ID_AUTO_SAVE_UNPINNED, Boolean(tabsData.autoSaveUnpinned)));
-				promises.push(updateCheckedValue(MENU_ID_AUTO_SAVE_ALL, Boolean(tabsData.autoSaveAll)));
-				if (tab && tab.url) {
-					let selectedEntryId = MENU_ID_ASSOCIATE_WITH_PROFILE_PREFIX + "default";
-					let title = MENU_CREATE_DOMAIN_RULE_MESSAGE;
-					const [profiles, rule] = await Promise.all([config.getProfiles(), config.getRule(tab.url)]);
-					if (rule) {
-						const profileIndex = profileIndexes.get(rule.profile);
-						if (profileIndex) {
-							selectedEntryId = MENU_ID_ASSOCIATE_WITH_PROFILE_PREFIX + profileIndex;
-							title = MENU_UPDATE_RULE_MESSAGE;
-						}
+			promises.push(updateCheckedValue(MENU_ID_AUTO_SAVE_DISABLED, !tabsData[tab.id].autoSave));
+			promises.push(updateCheckedValue(MENU_ID_AUTO_SAVE_TAB, tabsData[tab.id].autoSave));
+			promises.push(updateCheckedValue(MENU_ID_AUTO_SAVE_UNPINNED, Boolean(tabsData.autoSaveUnpinned)));
+			promises.push(updateCheckedValue(MENU_ID_AUTO_SAVE_ALL, Boolean(tabsData.autoSaveAll)));
+			if (tab && tab.url) {
+				let selectedEntryId = MENU_ID_ASSOCIATE_WITH_PROFILE_PREFIX + "default";
+				let title = MENU_CREATE_DOMAIN_RULE_MESSAGE;
+				const [profiles, rule] = await Promise.all([config.getProfiles(), config.getRule(tab.url)]);
+				if (rule) {
+					const profileIndex = profileIndexes.get(rule.profile);
+					if (profileIndex) {
+						selectedEntryId = MENU_ID_ASSOCIATE_WITH_PROFILE_PREFIX + profileIndex;
+						title = MENU_UPDATE_RULE_MESSAGE;
 					}
+				}
+				if (Object.keys(profiles).length > 1) {
 					Object.keys(profiles).forEach((profileName, profileIndex) => {
 						if (profileName == config.DEFAULT_PROFILE_NAME) {
 							promises.push(updateCheckedValue(MENU_ID_ASSOCIATE_WITH_PROFILE_PREFIX + "default", selectedEntryId == MENU_ID_ASSOCIATE_WITH_PROFILE_PREFIX + "default"));
@@ -439,10 +441,8 @@ singlefile.extension.ui.bg.menu = (() => {
 					});
 					promises.push(updateTitleValue(MENU_ID_ASSOCIATE_WITH_PROFILE, title));
 				}
-				await Promise.all(promises);
-			} catch (error) {
-				// ignored
 			}
+			await Promise.all(promises);
 		}
 	}
 
@@ -461,11 +461,7 @@ singlefile.extension.ui.bg.menu = (() => {
 		const lastCheckedValue = menusCheckedState.get(id);
 		menusCheckedState.set(id, checked);
 		if (lastCheckedValue === undefined || lastCheckedValue != checked) {
-			try {
-				await menus.update(id, { checked });
-			} catch (error) {
-				// ignored
-			}
+			await menus.update(id, { checked });
 		}
 	}
 

+ 35 - 28
extension/ui/bg/ui-options.js

@@ -173,22 +173,22 @@
 		if (await confirm(browser.i18n.getMessage("optionsDeleteDisplayedRulesConfirm"))) {
 			await browser.runtime.sendMessage({ method: "config.deleteRules", profileName: !showAllProfilesInput.checked && profileNamesInput.value });
 			await refresh();
-			refreshExternalComponents();
+			await refreshExternalComponents();
 		}
 	}, false);
 	createURLElement.onsubmit = async event => {
 		event.preventDefault();
 		try {
 			await browser.runtime.sendMessage({ method: "config.addRule", url: ruleUrlInput.value, profileName: ruleProfileInput.value, autoSaveProfileName: ruleAutoSaveProfileInput.value });
-			ruleUrlInput.value = "";
-			ruleProfileInput.value = ruleAutoSaveProfileInput.value = DEFAULT_PROFILE_NAME;
-			autoSaveProfileChanged = false;
-			await refresh();
-			refreshExternalComponents();
-			ruleUrlInput.focus();
 		} catch (error) {
 			// ignored
 		}
+		ruleUrlInput.value = "";
+		ruleProfileInput.value = ruleAutoSaveProfileInput.value = DEFAULT_PROFILE_NAME;
+		autoSaveProfileChanged = false;
+		await refresh();
+		await refreshExternalComponents();
+		ruleUrlInput.focus();
 	};
 	ruleUrlInput.onclick = ruleUrlInput.onkeyup = ruleUrlInput.onchange = async () => {
 		ruleAddButton.disabled = !ruleUrlInput.value;
@@ -232,27 +232,27 @@
 		if (profileName) {
 			try {
 				await browser.runtime.sendMessage({ method: "config.createProfile", profileName });
-				if (sidePanelDisplay) {
-					await refresh();
-				} else {
-					await refresh(profileName);
-				}
-				refreshExternalComponents();
 			} catch (error) {
 				// ignored
 			}
+			if (sidePanelDisplay) {
+				await refresh();
+			} else {
+				await refresh(profileName);
+			}
+			await refreshExternalComponents();
 		}
 	}, false);
 	deleteProfileButton.addEventListener("click", async () => {
 		if (await confirm(browser.i18n.getMessage("profileDeleteConfirm"))) {
 			try {
 				await browser.runtime.sendMessage({ method: "config.deleteProfile", profileName: profileNamesInput.value });
-				profileNamesInput.value = null;
-				await refresh();
-				refreshExternalComponents();
 			} catch (error) {
 				// ignored
 			}
+			profileNamesInput.value = null;
+			await refresh();
+			await refreshExternalComponents();
 		}
 	}, false);
 	renameProfileButton.addEventListener("click", async () => {
@@ -260,11 +260,11 @@
 		if (profileName) {
 			try {
 				await browser.runtime.sendMessage({ method: "config.renameProfile", profileName: profileNamesInput.value, newProfileName: profileName });
-				await refresh(profileName);
-				refreshExternalComponents();
 			} catch (error) {
 				// ignored
 			}
+			await refresh(profileName);
+			await refreshExternalComponents();
 		}
 	}, false);
 	resetButton.addEventListener("click", async () => {
@@ -273,12 +273,12 @@
 			if (choice == "all") {
 				await browser.runtime.sendMessage({ method: "config.resetProfiles" });
 				await refresh(DEFAULT_PROFILE_NAME);
-				refreshExternalComponents();
+				await refreshExternalComponents();
 			}
 			if (choice == "current") {
 				await browser.runtime.sendMessage({ method: "config.resetProfile", profileName: profileNamesInput.value });
 				await refresh();
-				refreshExternalComponents();
+				await refreshExternalComponents();
 			}
 			await update();
 		}
@@ -298,7 +298,7 @@
 				const config = JSON.parse(serializedConfig);
 				await browser.runtime.sendMessage({ method: "config.importConfig", config });
 				await refresh(DEFAULT_PROFILE_NAME);
-				refreshExternalComponents();
+				await refreshExternalComponents();
 				fileInput.value = "";
 			}
 		};
@@ -351,6 +351,9 @@
 					await browser.runtime.sendMessage({ method: "ui.refreshMenu" });
 				}
 			} else {
+				if (target == contextMenuEnabledInput) {
+					await browser.runtime.sendMessage({ method: "ui.refreshMenu" });
+				}
 				await refresh();
 			}
 		}
@@ -500,7 +503,7 @@
 					if (await confirm(browser.i18n.getMessage("optionsDeleteRuleConfirm"))) {
 						await browser.runtime.sendMessage({ method: "config.deleteRule", url: rule.url });
 						await refresh();
-						refreshExternalComponents();
+						await refreshExternalComponents();
 					}
 				}, false);
 				ruleUpdateButton.title = browser.i18n.getMessage("optionsUpdateRuleTooltip");
@@ -518,7 +521,7 @@
 							rulesElement.appendChild(editURLElement);
 							await browser.runtime.sendMessage({ method: "config.updateRule", url: rule.url, newUrl: ruleEditUrlInput.value, profileName: ruleEditProfileInput.value, autoSaveProfileName: ruleEditAutoSaveProfileInput.value });
 							await refresh();
-							refreshExternalComponents();
+							await refreshExternalComponents();
 							ruleUrlInput.focus();
 						};
 					}
@@ -636,11 +639,15 @@
 	}
 
 	async function refreshExternalComponents() {
-		await browser.runtime.sendMessage({ method: "ui.refreshMenu" });
-		if (sidePanelDisplay) {
-			await browser.runtime.sendMessage({ method: "options.refresh", profileName: profileNamesInput.value });
-		} else {
-			await browser.runtime.sendMessage({ method: "options.refreshPanel", profileName: profileNamesInput.value });
+		try {
+			await browser.runtime.sendMessage({ method: "ui.refreshMenu" });
+			if (sidePanelDisplay) {
+				await browser.runtime.sendMessage({ method: "options.refresh", profileName: profileNamesInput.value });
+			} else {
+				await browser.runtime.sendMessage({ method: "options.refreshPanel", profileName: profileNamesInput.value });
+			}
+		} catch (error) {
+			// ignored
 		}
 	}
 

+ 14 - 14
lib/single-file/single-file-core.js

@@ -1571,21 +1571,21 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 						// ignored
 					}
 					if (Util.testValidURL(resourceURL)) {
-						try {
-							const { content } = await batchRequest.addURL(resourceURL);
-							const hashMatch = originalResourceURL.match(REGEXP_URL_HASH);
-							if (hashMatch && hashMatch[0]) {
-								const svgDoc = util.parseSVGContent(content);
-								const symbolElement = svgDoc.querySelector(hashMatch[0]);
-								if (symbolElement) {
-									resourceElement.setAttribute(attributeName, hashMatch[0]);
-									resourceElement.parentElement.insertBefore(symbolElement, resourceElement.parentElement.firstChild);
-								}
-							} else {
-								resourceElement.setAttribute(attributeName, PREFIX_DATA_URI_IMAGE_SVG + "," + content);
+						const { content } = await batchRequest.addURL(resourceURL);
+						const hashMatch = originalResourceURL.match(REGEXP_URL_HASH);
+						if (hashMatch && hashMatch[0]) {
+							let symbolElement;
+							try {
+								symbolElement = util.parseSVGContent(content).querySelector(hashMatch[0]);
+							} catch (error) {
+								// ignored
 							}
-						} catch (error) {
-							// ignored
+							if (symbolElement) {
+								resourceElement.setAttribute(attributeName, hashMatch[0]);
+								resourceElement.parentElement.insertBefore(symbolElement, resourceElement.parentElement.firstChild);
+							}
+						} else {
+							resourceElement.setAttribute(attributeName, PREFIX_DATA_URI_IMAGE_SVG + "," + content);
 						}
 					}
 				} else if (resourceURL == options.url) {