Sfoglia il codice sorgente

add hidden option `processInForeground`
(see https://github.com/gildas-lormeau/SingleFile/issues/1218)

Gildas 2 anni fa
parent
commit
65a5686db4
2 ha cambiato i file con 18 aggiunte e 10 eliminazioni
  1. 7 2
      src/core/bg/business.js
  2. 11 8
      src/core/bg/config.js

+ 7 - 2
src/core/bg/business.js

@@ -44,7 +44,7 @@ const extensionScriptFiles = [
 ];
 
 const tasks = [];
-let currentTaskId = 0, maxParallelWorkers;
+let currentTaskId = 0, maxParallelWorkers, processInForeground;
 ui.init({ isSavingTab, saveTabs, saveUrls, cancelTab, openEditor, saveSelectedLinks, batchSaveUrls });
 
 export {
@@ -176,7 +176,9 @@ function openEditor(tab) {
 
 async function initMaxParallelWorkers() {
 	if (!maxParallelWorkers) {
-		maxParallelWorkers = (await config.get()).maxParallelWorkers;
+		const configData = await config.get();
+		processInForeground = configData.processInForeground;
+		maxParallelWorkers = processInForeground ? 1 : configData.maxParallelWorkers;
 	}
 }
 
@@ -213,6 +215,9 @@ async function runTask(taskInfo) {
 	}
 	taskInfo.options.taskId = taskId;
 	try {
+		if (processInForeground) {
+			await browser.tabs.update(taskInfo.tab.id, { active: true });
+		}
 		await browser.tabs.sendMessage(taskInfo.tab.id, { method: taskInfo.method, options: taskInfo.options });
 	} catch (error) {
 		if (error && (!error.message || !isIgnoredError(error))) {

+ 11 - 8
src/core/bg/config.js

@@ -216,6 +216,9 @@ async function upgrade(ignoreOldProfiles) {
 	if (!config.maxParallelWorkers) {
 		await configStorage.set({ maxParallelWorkers: navigator.hardwareConcurrency || 4 });
 	}
+	if (!config.processInForeground) {
+		await configStorage.set({ processInForeground: true });
+	}
 }
 
 async function getRule(url, ignoreWildcard) {
@@ -231,10 +234,10 @@ async function getRule(url, ignoreWildcard) {
 
 async function getConfig() {
 	await pendingUpgradePromise;
-	const { maxParallelWorkers } = await configStorage.get(["maxParallelWorkers"]);
+	const { maxParallelWorkers, processInForeground } = await configStorage.get(["maxParallelWorkers", "processInForeground"]);
 	const rules = await getRules();
 	const profiles = await getProfiles();
-	return { profiles, rules, maxParallelWorkers };
+	return { profiles, rules, maxParallelWorkers, processInForeground };
 }
 
 function sortRules(ruleLeft, ruleRight) {
@@ -312,7 +315,7 @@ async function onMessage(message) {
 		const syncConfig = await browser.storage.sync.get();
 		if (!syncConfig || !syncConfig.rules) {
 			const profileKeyNames = await getProfileKeyNames();
-			const localConfig = await browser.storage.local.get(["rules", "maxParallelWorkers", ...profileKeyNames]);
+			const localConfig = await browser.storage.local.get(["rules", "maxParallelWorkers", "processInForeground", ...profileKeyNames]);
 			await browser.storage.sync.set(localConfig);
 		}
 		configStorage = browser.storage.sync;
@@ -324,7 +327,7 @@ async function onMessage(message) {
 		const syncConfig = await browser.storage.sync.get();
 		const localConfig = await browser.storage.local.get();
 		if (syncConfig && syncConfig.rules && (!localConfig || !localConfig.rules)) {
-			await browser.storage.local.set({ rules: syncConfig.rules, maxParallelWorkers: syncConfig.maxParallelWorkers });
+			await browser.storage.local.set({ rules: syncConfig.rules, maxParallelWorkers: syncConfig.maxParallelWorkers, processInForeground: syncConfig.processInForeground });
 			const profiles = {};
 			// syncConfig.profileNames.forEach(profileKeyName => profiles[PROFILE_NAME_PREFIX + profileKeyName] = syncConfig[profileKeyName]);
 			await browser.storage.local.set(profiles);
@@ -534,7 +537,7 @@ async function resetProfiles() {
 	delete allTabsData.profileName;
 	await tabsData.set(allTabsData);
 	let profileKeyNames = await getProfileKeyNames();
-	await configStorage.remove([...profileKeyNames, "rules", "maxParallelWorkers"]);
+	await configStorage.remove([...profileKeyNames, "rules", "maxParallelWorkers", "processInForeground"]);
 	await upgrade(true);
 }
 
@@ -548,7 +551,7 @@ async function resetProfile(profileName) {
 
 async function exportConfig() {
 	const config = await getConfig();
-	const textContent = JSON.stringify({ profiles: config.profiles, rules: config.rules, maxParallelWorkers: config.maxParallelWorkers }, null, 2);
+	const textContent = JSON.stringify({ profiles: config.profiles, rules: config.rules, maxParallelWorkers: config.maxParallelWorkers, processInForeground: config.processInForeground }, null, 2);
 	const filename = `singlefile-settings-${(new Date()).toISOString().replace(/:/g, "_")}.json`;
 	if (IS_NOT_SAFARI) {
 		const url = URL.createObjectURL(new Blob([textContent], { type: "text/json" }));
@@ -578,8 +581,8 @@ async function importConfig(config) {
 		delete allTabsData.profileName;
 		await tabsData.set(allTabsData);
 	}
-	await configStorage.remove([...profileKeyNames, "rules", "maxParallelWorkers"]);
-	const newConfig = { rules: config.rules, maxParallelWorkers: config.maxParallelWorkers };
+	await configStorage.remove([...profileKeyNames, "rules", "maxParallelWorkers", "processInForeground"]);
+	const newConfig = { rules: config.rules, maxParallelWorkers: config.maxParallelWorkers, processInForeground: config.processInForeground };
 	Object.keys(config.profiles).forEach(profileName => newConfig[PROFILE_NAME_PREFIX + profileName] = config.profiles[profileName]);
 	await configStorage.set(newConfig);
 	await upgrade();