Browse Source

added a way to use the current profile in auto-settings rules

Gildas 6 years ago
parent
commit
2d62f88815
3 changed files with 50 additions and 27 deletions
  1. 11 3
      extension/core/bg/data/config.js
  2. 26 8
      extension/ui/bg/ui-menu.js
  3. 13 16
      extension/ui/bg/ui-options.js

+ 11 - 3
extension/core/bg/data/config.js

@@ -22,6 +22,7 @@
 
 singlefile.config = (() => {
 
+	const CURRENT_PROFILE_NAME = "-";
 	const DEFAULT_PROFILE_NAME = "__Default_Settings__";
 	const DISABLED_PROFILE_NAME = "__Disabled_Settings__";
 	const REGEXP_RULE_PREFIX = "regexp:";
@@ -69,6 +70,7 @@ singlefile.config = (() => {
 	return {
 		DEFAULT_PROFILE_NAME,
 		DISABLED_PROFILE_NAME,
+		CURRENT_PROFILE_NAME,
 		getRule,
 		getOptions,
 		getProfiles,
@@ -175,7 +177,8 @@ singlefile.config = (() => {
 		if (message.method.endsWith(".getConstants")) {
 			return {
 				DISABLED_PROFILE_NAME,
-				DEFAULT_PROFILE_NAME
+				DEFAULT_PROFILE_NAME,
+				CURRENT_PROFILE_NAME
 			};
 		}
 		if (message.method.endsWith(".getRules")) {
@@ -206,8 +209,13 @@ singlefile.config = (() => {
 
 	async function getOptions(url, autoSave) {
 		const [config, rule, tabsData] = await Promise.all([getConfig(), getRule(url), singlefile.tabsData.get()]);
-		const profileName = tabsData.profileName;
-		return rule ? config.profiles[rule[autoSave ? "autoSaveProfile" : "profile"]] : config.profiles[profileName || singlefile.config.DEFAULT_PROFILE_NAME];
+		const tabProfileName = tabsData.profileName || DEFAULT_PROFILE_NAME;
+		if (rule) {
+			const profileName = rule[autoSave ? "autoSaveProfile" : "profile"];
+			return config.profiles[profileName == CURRENT_PROFILE_NAME ? tabProfileName : profileName];
+		} else {
+			return config.profiles[tabProfileName];
+		}
 	}
 
 	async function updateProfile(profileName, profile) {

+ 26 - 8
extension/ui/bg/ui-menu.js

@@ -164,8 +164,8 @@ singlefile.ui.menu = (() => {
 					title: MENU_SELECT_PROFILE_MESSAGE,
 					contexts: defaultContexts,
 				});
-				let defaultProfileId = MENU_ID_SELECT_PROFILE_PREFIX + "default";
-				let defaultProfileChecked = !tabsData.profileName || tabsData.profileName == singlefile.config.DEFAULT_PROFILE_NAME;
+				const defaultProfileId = MENU_ID_SELECT_PROFILE_PREFIX + "default";
+				const defaultProfileChecked = !tabsData.profileName || tabsData.profileName == singlefile.config.DEFAULT_PROFILE_NAME;
 				menus.create({
 					id: defaultProfileId,
 					type: "radio",
@@ -185,17 +185,32 @@ singlefile.ui.menu = (() => {
 				if (tab && tab.url) {
 					rule = await singlefile.config.getRule(tab.url);
 				}
-				defaultProfileId = MENU_ID_ASSOCIATE_WITH_PROFILE_PREFIX + "default";
-				defaultProfileChecked = !rule || rule.profile == singlefile.config.DEFAULT_PROFILE_NAME;
+				const currentProfileId = MENU_ID_ASSOCIATE_WITH_PROFILE_PREFIX + "current";
+				const currentProfileIChecked = !rule || (rule.profile == singlefile.config.CURRENT_PROFILE_NAME);
 				menus.create({
-					id: defaultProfileId,
+					id: currentProfileId,
+					type: "radio",
+					contexts: defaultContexts,
+					title: singlefile.config.CURRENT_PROFILE_NAME,
+					checked: currentProfileIChecked,
+					parentId: MENU_ID_ASSOCIATE_WITH_PROFILE
+				});
+				menusCheckedState.set(currentProfileId, currentProfileIChecked);
+
+				const associatedDefaultProfileId = MENU_ID_ASSOCIATE_WITH_PROFILE_PREFIX + "default";
+				const associatedDefaultProfileChecked = Boolean(rule) && (rule.profile == singlefile.config.DEFAULT_PROFILE_NAME);
+				menus.create({
+					id: associatedDefaultProfileId,
 					type: "radio",
 					contexts: defaultContexts,
 					title: PROFILE_DEFAULT_SETTINGS_MESSAGE,
-					checked: defaultProfileChecked,
+					checked: associatedDefaultProfileChecked,
 					parentId: MENU_ID_ASSOCIATE_WITH_PROFILE
 				});
-				menusCheckedState.set(defaultProfileId, defaultProfileChecked);
+				menusCheckedState.set(associatedDefaultProfileId, associatedDefaultProfileChecked);
+
+
+
 				profileIndexes = new Map();
 				Object.keys(profiles).forEach((profileName, profileIndex) => {
 					if (profileName != singlefile.config.DEFAULT_PROFILE_NAME) {
@@ -211,7 +226,7 @@ singlefile.ui.menu = (() => {
 						});
 						menusCheckedState.set(profileId, profileChecked);
 						profileId = MENU_ID_ASSOCIATE_WITH_PROFILE_PREFIX + profileIndex;
-						profileChecked = rule && rule.profile == profileName;
+						profileChecked = Boolean(rule) && rule.profile == profileName;
 						menus.create({
 							id: profileId,
 							type: "radio",
@@ -344,6 +359,8 @@ singlefile.ui.menu = (() => {
 					let profileName;
 					if (profileId == "default") {
 						profileName = singlefile.config.DEFAULT_PROFILE_NAME;
+					} else if (profileId == "current") {
+						profileName = singlefile.config.CURRENT_PROFILE_NAME;
 					} else {
 						const profileIndex = Number(profileId);
 						profileName = Object.keys(profiles)[profileIndex];
@@ -414,6 +431,7 @@ singlefile.ui.menu = (() => {
 	}
 
 	async function updateCheckedValue(id, checked) {
+		checked = Boolean(checked);
 		const lastCheckedValue = menusCheckedState.get(id);
 		menusCheckedState.set(id, checked);
 		if (lastCheckedValue === undefined || lastCheckedValue != checked) {

+ 13 - 16
extension/ui/bg/ui-options.js

@@ -22,7 +22,7 @@
 
 (async () => {
 
-	const { DEFAULT_PROFILE_NAME, DISABLED_PROFILE_NAME } = await browser.runtime.sendMessage({ method: "config.getConstants" });
+	const { DEFAULT_PROFILE_NAME, DISABLED_PROFILE_NAME, CURRENT_PROFILE_NAME } = await browser.runtime.sendMessage({ method: "config.getConstants" });
 	const removeHiddenElementsLabel = document.getElementById("removeHiddenElementsLabel");
 	const removeUnusedStylesLabel = document.getElementById("removeUnusedStylesLabel");
 	const removeUnusedFontsLabel = document.getElementById("removeUnusedFontsLabel");
@@ -156,7 +156,7 @@
 	let pendingSave = Promise.resolve();
 	let autoSaveProfileChanged;
 	ruleProfileInput.onchange = () => {
-		if (!autoSaveProfileChanged) {
+		if (!autoSaveProfileChanged && ruleProfileInput.value != CURRENT_PROFILE_NAME) {
 			ruleAutoSaveProfileInput.value = ruleProfileInput.value;
 		}
 	};
@@ -438,7 +438,6 @@
 		const [profiles, rules] = await Promise.all([browser.runtime.sendMessage({ method: "config.getProfiles" }), browser.runtime.sendMessage({ method: "config.getRules" })]);
 		const selectedProfileName = profileName || profileNamesInput.value || DEFAULT_PROFILE_NAME;
 		Array.from(profileNamesInput.childNodes).forEach(node => node.remove());
-		const profileNames = Object.keys(profiles);
 		profileNamesInput.options.length = 0;
 		ruleProfileInput.options.length = 0;
 		ruleAutoSaveProfileInput.options.length = 0;
@@ -447,21 +446,19 @@
 		let optionElement = document.createElement("option");
 		optionElement.value = DEFAULT_PROFILE_NAME;
 		optionElement.textContent = browser.i18n.getMessage("profileDefaultSettings");
-		profileNamesInput.appendChild(optionElement);
-		ruleProfileInput.appendChild(optionElement.cloneNode(true));
-		ruleAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
-		ruleEditProfileInput.appendChild(optionElement.cloneNode(true));
-		ruleEditAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
-		profileNames.forEach(profileName => {
-			if (profileName != DEFAULT_PROFILE_NAME) {
-				const optionElement = document.createElement("option");
-				optionElement.value = optionElement.textContent = profileName;
+		[CURRENT_PROFILE_NAME].concat(...Object.keys(profiles)).forEach(profileName => {
+			const optionElement = document.createElement("option");
+			optionElement.value = optionElement.textContent = profileName;
+			if (profileName == DEFAULT_PROFILE_NAME) {
+				optionElement.textContent = browser.i18n.getMessage("profileDefaultSettings");
+			}
+			if (profileName != CURRENT_PROFILE_NAME) {
 				profileNamesInput.appendChild(optionElement);
-				ruleProfileInput.appendChild(optionElement.cloneNode(true));
-				ruleAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
-				ruleEditProfileInput.appendChild(optionElement.cloneNode(true));
-				ruleEditAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
 			}
+			ruleProfileInput.appendChild(optionElement.cloneNode(true));
+			ruleAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
+			ruleEditProfileInput.appendChild(optionElement.cloneNode(true));
+			ruleEditAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
 		});
 		profileNamesInput.disabled = profileNamesInput.options.length == 1;
 		optionElement = document.createElement("option");