Skip to content

Commit

Permalink
Deduplicate code for identifying URL's domain
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyrat committed Jul 1, 2020
1 parent e605e00 commit f89f2e1
Showing 1 changed file with 12 additions and 32 deletions.
44 changes: 12 additions & 32 deletions common/SearcherAll.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { KeeLog } from "./Logger";
import { KeeState } from "../store/KeeState";
import { SearchConfig } from "./model/SearchConfig";
import { utils } from "./utils";
import { EntrySummary } from "./model/EntrySummary";
import { resolveConfig, tokenise, calculateMatchScore } from "./SearchUtils";
import { Group } from "./model/Group";
import { KeeURL } from "./KeeURL";

export class SearcherAll {
constructor(private state: KeeState, config: Partial<SearchConfig>) {
Expand Down Expand Up @@ -74,36 +74,16 @@ export class SearcherAll {
if (!item.uRLs || item.uRLs.length <= 0) return false;

for (const filterDomain of filterDomains) {
try {
const filteredItems = item.uRLs.filter(itemURL => {
try {
let itemHostname: string;
// We're only really interested in the domain so can be lax about
// protocols and just prepend if necessary in order to make valid URLs
if (
!itemURL.startsWith("https://") &&
!itemURL.startsWith("http://") &&
!itemURL.startsWith("file://")
) {
itemHostname = new URL("http://" + itemURL).hostname;
} else {
itemHostname = new URL(itemURL).hostname;
}
const itemIsIPAddress = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/.test(
itemHostname
);
const itemDomain = itemIsIPAddress
? itemHostname
: utils.psl.getDomain(itemHostname);
return filterDomain === itemDomain;
} catch (e) {
return false;
} // ignore invalid URLs
});
if (filteredItems.length > 0) return true;
} catch (e) {
// ignore invalid URLs
}
const filteredUrls = item.uRLs.filter(itemURL => {
try {
if (itemURL.indexOf(filterDomain) < 0) return false;
const url = KeeURL.fromString(itemURL);
return url.domainOrIPAddress === filterDomain;
} catch (e) {
return false;
} // ignore invalid URLs
});
if (filteredUrls.length > 0) return true;
}
return false;
};
Expand All @@ -119,7 +99,7 @@ export class SearcherAll {
const dbFileName = databases[i].fileName;
this.treeTraversal(root, keywords, 0, addResult.bind(this), 0, dbFileName, filter);
}
onComplete(results);
if (onComplete) onComplete(results);
}

if (onComplete) {
Expand Down

0 comments on commit f89f2e1

Please sign in to comment.