Skip to content

Commit

Permalink
Use regexFilter to restore wildcard behavior (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorz committed Jan 8, 2024
1 parent 5229698 commit bffcb61
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
4 changes: 2 additions & 2 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ chrome.runtime.onInstalled.addListener(async (details) => {
}
});

browser.storage.onChanged.addListener(async (changes, area) => {
browser.storage.local.onChanged.addListener(async () => {
console.debug("Ruleset changed");
if (area === "local") await applyRulesFromStorage();;
await applyRulesFromStorage();;
});
2 changes: 1 addition & 1 deletion options.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ <h3>Examples</h3>
<li><code>*</code> - match everything.</li>
<li><code>example.com</code> – match <code>example.com</code> only.</li>
<li>
<code>*.example.com</code> – match all subdomains of <code>example.com</code> but not itself.
<code>*.example.com</code> – match <code>example.com</code> and its subdomains.
</li>
</ul>
</section>
Expand Down
2 changes: 1 addition & 1 deletion options.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async function saveOptions(ev) {

// Check permission
const permissions = {
origins: rules.map((rule) => rule.urlFilter),
origins: rules.map((rule) => rule.permissionOrigins),
};
if (!await browser.permissions.request(permissions)) {
throw new Error("permssions rejected");
Expand Down
53 changes: 43 additions & 10 deletions rules.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,57 @@
export class Rule {
constructor(host, language) {
this.host = host || "";
this.language = language || "";
// "host" is legacy name, better be "expr"
// The true host name is `this.canonicalDomain`
this.host = host || '';
this.language = language || '';
}

get isUniversalWidlcard() {
return this.host === '*';
}

get isSubdomainWildcard() {
return this.host.startsWith('*.');
}

get canonicalDomain() {
if (this.host === '*') {
return '*';
} if (this.host.startsWith('*.')) {
return '*.' + new URL(`http://${this.host.slice(2)}`).host;
if (this.isUniversalWidlcard) {
return null;
} else if (this.isSubdomainWildcard) {
return new URL(`http://${this.host.slice(2)}`).host;
} else {
return new URL(`http://${this.host}`).host;
}
}

get urlFilter() {
return `*://${this.canonicalDomain}/*`;
get permissionOrigins() {
if (this.isUniversalWidlcard) {
return '*://*/*';
} else {
return `*://*.${this.canonicalDomain}/*`;
}
}

get regexFilter() {
if (this.isUniversalWidlcard) {
return "https?:\\/\\/.*";
}
const escapedHost = this.canonicalDomain.replace('.', '\\.');
if (this.isSubdomainWildcard) {
return `https?:\\/\\/([^\\/]+\\.)?${escapedHost}\\/.*`;
} else {
return `https?:\\/\\/${escapedHost}\\/.*`;
}
}

get rule() {
let priority = this.host.split(".").length;
if (!this.isUniversalWidlcard && !this.isSubdomainWildcard) {
// Let `example.com` precedent `*.example.com`
priority += 1;
}
console.debug(`Rule P${priority} ${this.language} [${this.host}] /${this.regexFilter}/`)

return {
id: Math.floor(Math.random() * Number.MAX_SAFE_INTEGER),
action: {
Expand All @@ -33,9 +66,9 @@ export class Rule {
},
condition: {
resourceTypes: ["main_frame", "sub_frame"],
urlFilter: this.urlFilter
regexFilter: this.regexFilter
},
priority: this.host.split(".").length
priority
};
}

Expand Down

0 comments on commit bffcb61

Please sign in to comment.