Skip to content

Commit

Permalink
More optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
iorate committed Mar 30, 2019
1 parent 831dd59 commit b40d94c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 20 deletions.
37 changes: 27 additions & 10 deletions common.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ const queryTabs = makeAsyncApi((queryInfo, callback) => {

/* Block Rules */

class SimpleURL {
constructor(href) {
const u = new URL(href);
this.scheme = u.protocol.slice(0, -1);
this.host = u.hostname;
this.path = `${u.pathname}${u.search}`;
}

get href() {
return `${this.scheme}://${this.host}${this.path}`;
}
}

class BlockRule {
constructor(raw) {
this.raw = raw;
Expand Down Expand Up @@ -75,27 +88,27 @@ class BlockRule {
const mp = this.matchPattern;
if (mp.host == '*') {
} else if (mp.host.startsWith('*.')) {
if (url.hostname != mp.host.slice(2) && !url.hostname.endsWith(mp.host.slice(1))) {
if (url.host != mp.host.slice(2) && !url.host.endsWith(mp.host.slice(1))) {
return false;
}
} else if (url.hostname != mp.host) {
} else if (url.host != mp.host) {
return false;
}
if (mp.scheme == '*') {
if (url.protocol != 'http:' && url.protocol != 'https:') {
if (url.scheme != 'http' && url.scheme != 'https') {
return false;
}
} else if (url.protocol != `${mp.scheme}:`) {
} else if (url.scheme != mp.scheme) {
return false;
}
return mp.path.test(`${url.pathname}${url.search}`);
return mp.path.test(url.path);
} else if (this.regExp) {
return this.regExp.test(String(url));
return this.regExp.test(url.href);
} else {
return false;
}
}
};
}

const loadBlockRules = async () => {
const {blacklist} = await getLocalStorage({blacklist: ''});
Expand All @@ -111,7 +124,11 @@ const saveBlockRules = async blockRules => {
};

const deriveBlockRule = url => {
const u = new URL(url);
const s = u.protocol.match(/^((https?)|ftp):$/);
return s ? (s[2] ? '*' : s[1]) + '://' + u.hostname + '/*' : null;
if (url.scheme == 'http' || url.scheme == 'https') {
return `*://${url.host}/*`;
} else if (url.scheme == 'ftp') {
return `${url.scheme}://${url.host}/*`;
} else {
return '';
}
};
7 changes: 4 additions & 3 deletions content.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class UBlacklist {
e.preventDefault();
e.stopPropagation();
if (this.blockRules) {
$('ubBlockInput').value = deriveBlockRule(pageUrl) || '';
$('ubBlockInput').value = deriveBlockRule(new SimpleURL(pageUrl));
$('ubBlockDialog').showModal();
}
});
Expand All @@ -139,8 +139,9 @@ class UBlacklist {
while (unblockSelect.firstChild) {
unblockSelect.removeChild(unblockSelect.firstChild);
}
const url = new SimpleURL(pageUrl);
this.blockRules.forEach((rule, index) => {
if (rule.test(new URL(pageUrl))) {
if (rule.test(url)) {
const option = document.createElement('option');
option.textContent = rule.raw;
option.value = String(index);
Expand Down Expand Up @@ -264,7 +265,7 @@ class UBlacklist {
}

judgeEntry(entry) {
const url = new URL(entry.dataset.ubPageUrl);
const url = new SimpleURL(entry.dataset.ubPageUrl);
if (this.blockRules.some(rule => rule.test(url))) {
entry.classList.add('ubBlockedEntry');
++this.blockedEntryCount;
Expand Down
20 changes: 13 additions & 7 deletions popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ for (const element of document.querySelectorAll('[data-i18n]')) {

(async () => {
const blockRules = await loadBlockRules();
const url = (await queryTabs({active: true, currentWindow: true}))[0].url;
const blocked = blockRules.some(rule => rule.test(new URL(url)));
if (!blocked) {
$('blockInput').value = deriveBlockRule(url) || '';
const url = new SimpleURL((await queryTabs({active: true, currentWindow: true}))[0].url);
const blockIndices = [];
blockRules.forEach((rule, index) => {
if (rule.test(url)) {
blockIndices.push(index);
}
});
if (!blockIndices.length) {
$('blockInput').value = deriveBlockRule(url);
$('blockForm').addEventListener('submit', event => {
event.preventDefault();
const raw = $('blockInput').value;
Expand All @@ -24,14 +29,15 @@ for (const element of document.querySelectorAll('[data-i18n]')) {
});
$('blockPopup').style.display = 'block';
} else {
blockRules.forEach((rule, index) => {
if (rule.test(new URL(url))) {
for (const index of blockIndices) {
const rule = blockRules[index];
if (rule.test(url)) {
const option = document.createElement('option');
option.textContent = rule.raw;
option.value = String(index);
$('unblockSelect').appendChild(option);
}
});
}
$('unblockForm').addEventListener('submit', event => {
event.preventDefault();
blockRules.splice(Number($('unblockSelect').value), 1);
Expand Down

0 comments on commit b40d94c

Please sign in to comment.