Ver código fonte

added support for regular expressions in rule URLs

Gildas 7 anos atrás
pai
commit
de40469033
2 arquivos alterados com 24 adições e 5 exclusões
  1. 16 2
      extension/core/bg/config.js
  2. 8 3
      extension/ui/pages/help.html

+ 16 - 2
extension/core/bg/config.js

@@ -24,6 +24,7 @@ singlefile.config = (() => {
 
 	const DEFAULT_PROFILE_NAME = "__Default_Settings__";
 	const DISABLED_PROFILE_NAME = "__Disabled_Settings__";
+	const REGEXP_RULE_PREFIX = "regexp:";
 
 	const DEFAULT_CONFIG = {
 		removeHiddenElements: true,
@@ -136,6 +137,14 @@ singlefile.config = (() => {
 		return browser.storage.local.get(["profiles", "rules"]);
 	}
 
+	function sortRules(ruleLeft, ruleRight) {
+		ruleRight.url.length - ruleLeft.url.length;
+	}
+
+	function testRegExpRule(rule) {
+		return rule.url.toLowerCase().startsWith(REGEXP_RULE_PREFIX);
+	}
+
 	return {
 		DISABLED_PROFILE_NAME,
 		DEFAULT_PROFILE_NAME,
@@ -153,8 +162,13 @@ singlefile.config = (() => {
 		},
 		async getOptions(profileName, url, autoSave) {
 			const config = await getConfig();
-			const urlRule = config.rules.sort((ruleLeft, ruleRight) => ruleRight.url.length - ruleLeft.url.length).find(rule => url && url.includes(rule.url));
-			return urlRule ? config.profiles[urlRule[autoSave ? "autoSaveProfile" : "profile"]] : config.profiles[profileName || singlefile.config.DEFAULT_PROFILE_NAME];
+			const regExpRules = config.rules.filter(rule => testRegExpRule(rule));
+			let rule = regExpRules.sort(sortRules).find(rule => url && url.match(new RegExp(rule.url.split(REGEXP_RULE_PREFIX)[1])));
+			if (!rule) {
+				const normalRules = config.rules.filter(rule => !testRegExpRule(rule));
+				rule = normalRules.sort(sortRules).find(rule => url && url.includes(rule.url));
+			}
+			return rule ? config.profiles[rule[autoSave ? "autoSaveProfile" : "profile"]] : config.profiles[profileName || singlefile.config.DEFAULT_PROFILE_NAME];
 		},
 		async updateProfile(profileName, profile) {
 			const config = await getConfig();

+ 8 - 3
extension/ui/pages/help.html

@@ -401,9 +401,14 @@
 				<p>Auto-settings rules.</p>
 				<ul>
 					<li>
-						The auto-settings rules allow you to associate a complete or partial URL with a profile. It also allows to
-						associate a specific profile for the auto-save, or to disable it (check "display 'Auto-save profile' column").
-						When two or more rules match a URL of a page to save, the rule with the longest URL will be chosen.
+						The auto-settings rules let you associate a complete or partial URL with a profile. It also allows to associate a
+						specific profile for the auto-save, or to disable it (check "display 'Auto-save profile' column"). When two or
+						more rules match a URL of a page to save, the rule with the longest URL will be chosen.
+					</li>
+					<li>
+						You can use regular expressions by prefixing the URL with "regexp:" (e.g. "regexp:.*\.com" to match all URLs
+						containing ".com"). When one or more rules based on URLs and one or more rules based on regular expressions match
+						a URL, the longest rule based on regular expression will be chosen.
 					</li>
 				</ul>
 				<p>Form buttons</p>