Skip to content

Commit bffcb61

Browse files
committed
Use regexFilter to restore wildcard behavior (#11)
1 parent 5229698 commit bffcb61

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

background.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ chrome.runtime.onInstalled.addListener(async (details) => {
1212
}
1313
});
1414

15-
browser.storage.onChanged.addListener(async (changes, area) => {
15+
browser.storage.local.onChanged.addListener(async () => {
1616
console.debug("Ruleset changed");
17-
if (area === "local") await applyRulesFromStorage();;
17+
await applyRulesFromStorage();;
1818
});

options.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ <h3>Examples</h3>
3737
<li><code>*</code> - match everything.</li>
3838
<li><code>example.com</code> – match <code>example.com</code> only.</li>
3939
<li>
40-
<code>*.example.com</code> – match all subdomains of <code>example.com</code> but not itself.
40+
<code>*.example.com</code> – match <code>example.com</code> and its subdomains.
4141
</li>
4242
</ul>
4343
</section>

options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async function saveOptions(ev) {
2020

2121
// Check permission
2222
const permissions = {
23-
origins: rules.map((rule) => rule.urlFilter),
23+
origins: rules.map((rule) => rule.permissionOrigins),
2424
};
2525
if (!await browser.permissions.request(permissions)) {
2626
throw new Error("permssions rejected");

rules.js

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,57 @@
11
export class Rule {
22
constructor(host, language) {
3-
this.host = host || "";
4-
this.language = language || "";
3+
// "host" is legacy name, better be "expr"
4+
// The true host name is `this.canonicalDomain`
5+
this.host = host || '';
6+
this.language = language || '';
7+
}
8+
9+
get isUniversalWidlcard() {
10+
return this.host === '*';
11+
}
12+
13+
get isSubdomainWildcard() {
14+
return this.host.startsWith('*.');
515
}
616

717
get canonicalDomain() {
8-
if (this.host === '*') {
9-
return '*';
10-
} if (this.host.startsWith('*.')) {
11-
return '*.' + new URL(`http://${this.host.slice(2)}`).host;
18+
if (this.isUniversalWidlcard) {
19+
return null;
20+
} else if (this.isSubdomainWildcard) {
21+
return new URL(`http://${this.host.slice(2)}`).host;
1222
} else {
1323
return new URL(`http://${this.host}`).host;
1424
}
1525
}
1626

17-
get urlFilter() {
18-
return `*://${this.canonicalDomain}/*`;
27+
get permissionOrigins() {
28+
if (this.isUniversalWidlcard) {
29+
return '*://*/*';
30+
} else {
31+
return `*://*.${this.canonicalDomain}/*`;
32+
}
33+
}
34+
35+
get regexFilter() {
36+
if (this.isUniversalWidlcard) {
37+
return "https?:\\/\\/.*";
38+
}
39+
const escapedHost = this.canonicalDomain.replace('.', '\\.');
40+
if (this.isSubdomainWildcard) {
41+
return `https?:\\/\\/([^\\/]+\\.)?${escapedHost}\\/.*`;
42+
} else {
43+
return `https?:\\/\\/${escapedHost}\\/.*`;
44+
}
1945
}
2046

2147
get rule() {
48+
let priority = this.host.split(".").length;
49+
if (!this.isUniversalWidlcard && !this.isSubdomainWildcard) {
50+
// Let `example.com` precedent `*.example.com`
51+
priority += 1;
52+
}
53+
console.debug(`Rule P${priority} ${this.language} [${this.host}] /${this.regexFilter}/`)
54+
2255
return {
2356
id: Math.floor(Math.random() * Number.MAX_SAFE_INTEGER),
2457
action: {
@@ -33,9 +66,9 @@ export class Rule {
3366
},
3467
condition: {
3568
resourceTypes: ["main_frame", "sub_frame"],
36-
urlFilter: this.urlFilter
69+
regexFilter: this.regexFilter
3770
},
38-
priority: this.host.split(".").length
71+
priority
3972
};
4073
}
4174

0 commit comments

Comments
 (0)