Skip to content

Commit

Permalink
Merge pull request #108 from LoveIsGrief/44-add-preference-to-choose-…
Browse files Browse the repository at this point in the history
…matching-hostname-or-complete-URL

Add preference to match using hostname only
  • Loading branch information
kintesh committed Oct 12, 2019
2 parents 9270a7e + c27daa7 commit f244931
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/Storage/HostStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class HostStorage extends PrefixStorage {
this.SET_KEY = 'host';
}

get(url) {
get(url, matchDomainOnly) {
return super.getAll().then(maps => {
const sorted = sortMaps(Object.keys(maps).map(key => maps[key]));
// Sorts by domain length, then by path length
return sorted.find(matchesSavedMap.bind(null, url)) || {};
return sorted.find(matchesSavedMap.bind(null, url, matchDomainOnly)) || {};
});
}

Expand Down
7 changes: 3 additions & 4 deletions src/containers.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ async function handle(url, tabId) {
} else if (creatingUrl) {
delete creatingTabs[tabId];
}

let [hostMap, preferences, identities, currentTab] = await Promise.all([
Storage.get(url),
PreferenceStorage.getAll(true),
let preferences = await PreferenceStorage.getAll(true);
let [hostMap, identities, currentTab] = await Promise.all([
Storage.get(url, preferences.matchDomainOnly),
ContextualIdentity.getAll(),
Tabs.get(tabId),
]);
Expand Down
6 changes: 6 additions & 0 deletions src/ui-preferences/preferences.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
[
{
"type": "bool",
"name": "matchDomainOnly",
"label": "Match domain only",
"description": "When matching rules to a URL, only use the domain as criteria"
},
{
"type": "bool",
"name": "keepOldTabs",
Expand Down
19 changes: 14 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,35 @@ export const urlKeyFromUrl = (url) => {
* - glob
* - standard
*
* @param url {URL}
* @param url {String}
* @param matchDomainOnly {Boolean=}
* @param map
* @return {*}
*/
export const matchesSavedMap = (url, map) => {
export const matchesSavedMap = (url, matchDomainOnly, map) => {
const savedHost = map.host;
let toMatch = url;
if (matchDomainOnly) {
toMatch = new window.URL(url).host;
}

if (savedHost[0] === PREFIX_REGEX) {
const regex = savedHost.substr(1);
try {
return new RegExp(regex).test(url);
return new RegExp(regex).test(toMatch);
} catch (e) {
console.error('couldn\'t test regex', regex, e);
}
} else if (savedHost[0] === PREFIX_GLOB) {
// turning glob into regex isn't the worst thing:
// 1. * becomes .*
// 2. ? becomes .?
return new RegExp(savedHost.substr(1).replace(/\*/g, '.*').replace(/\?/g, '.?')).test(url);
return new RegExp(savedHost.substr(1)
.replace(/\*/g, '.*')
.replace(/\?/g, '.?'))
.test(toMatch);
} else {
const key = urlKeyFromUrl(url);
const key = urlKeyFromUrl(toMatch);
const _url = ((key.indexOf('/') === -1) ? key.concat('/') : key).toLowerCase();
const mapHost = ((map.host.indexOf('/') === -1) ? map.host.concat('/') : map.host).toLowerCase();
return domainMatch(_url, mapHost) && pathMatch(_url, mapHost);
Expand Down

0 comments on commit f244931

Please sign in to comment.