Răsfoiți Sursa

use async/await

Gildas 7 ani în urmă
părinte
comite
02077f60d2
2 a modificat fișierele cu 44 adăugiri și 47 ștergeri
  1. 4 4
      extension/ui/bg/options.js
  2. 40 43
      extension/ui/bg/ui.js

+ 4 - 4
extension/ui/bg/options.js

@@ -40,10 +40,10 @@
 		const maxResourceSizeInput = document.getElementById("maxResourceSizeInput");
 		const maxResourceSizeEnabledInput = document.getElementById("maxResourceSizeEnabledInput");
 		let pendingSave = Promise.resolve();
-		document.getElementById("resetButton").addEventListener("click", () => {
-			bgPage.singlefile.config.reset()
-				.then(refresh)
-				.then(update);
+		document.getElementById("resetButton").addEventListener("click", async () => {
+			await bgPage.singlefile.config.reset();
+			await refresh();
+			await update();
 		}, false);
 		maxResourceSizeEnabledInput.addEventListener("click", () => maxResourceSizeInput.disabled = !maxResourceSizeEnabledInput.checked, false);
 		document.getElementById("popupContent").onchange = update;

+ 40 - 43
extension/ui/bg/ui.js

@@ -80,53 +80,50 @@ singlefile.ui = (() => {
 	});
 	return { update: refreshContextMenu };
 
-	function refreshContextMenu() {
-		singlefile.config.get()
-			.then(config => {
-				if (config.contextMenuEnabled) {
-					browser.contextMenus.create({
-						id: MENU_ID_SAVE_PAGE,
-						contexts: ["page"],
-						title: "Save page"
-					});
-					browser.contextMenus.create({
-						id: MENU_ID_SAVE_SELECTED,
-						contexts: ["selection"],
-						title: "Save selection"
-					});
-				} else {
-					browser.contextMenus.removeAll();
-				}
+	async function refreshContextMenu() {
+		const config = await singlefile.config.get();
+		if (config.contextMenuEnabled) {
+			browser.contextMenus.create({
+				id: MENU_ID_SAVE_PAGE,
+				contexts: ["page"],
+				title: "Save page"
+			});
+			browser.contextMenus.create({
+				id: MENU_ID_SAVE_SELECTED,
+				contexts: ["selection"],
+				title: "Save selection"
 			});
+		} else {
+			browser.contextMenus.removeAll();
+		}
 	}
 
-	function processTab(tab, options) {
+	async function processTab(tab, options) {
 		const tabId = tab.id;
-		singlefile.core.processTab(tab, options)
-			.then(() => {
-				tabs[tabId] = {
-					id: tabId,
-					text: "...",
-					color: DEFAULT_COLOR,
-					title: "initializing...",
-					path: DEFAULT_ICON_PATH,
-					progress: -1,
-					barProgress: -1
-				};
-				refreshBadge(tabId);
-			})
-			.catch(() => {
-				tabs[tabId] = {
-					id: tabId,
-					text: "↻",
-					color: [255, 141, 1, 255],
-					title: "reload the page",
-					path: DEFAULT_ICON_PATH,
-					progress: -1,
-					barProgress: -1
-				};
-				refreshBadge(tabId);
-			});
+		try {
+			await singlefile.core.processTab(tab, options);
+			tabs[tabId] = {
+				id: tabId,
+				text: "...",
+				color: DEFAULT_COLOR,
+				title: "initializing...",
+				path: DEFAULT_ICON_PATH,
+				progress: -1,
+				barProgress: -1
+			};
+			refreshBadge(tabId);
+		} catch (e) {
+			tabs[tabId] = {
+				id: tabId,
+				text: "↻",
+				color: [255, 141, 1, 255],
+				title: "reload the page",
+				path: DEFAULT_ICON_PATH,
+				progress: -1,
+				barProgress: -1
+			};
+			refreshBadge(tabId);
+		}
 	}
 
 	function onTabError(tabId) {