Change: getExcludeRule may throw

This commit is contained in:
eight 2019-04-09 12:28:32 +08:00
parent 494641da34
commit 2a44f0d2a0

View File

@ -335,21 +335,27 @@ function createStyleElement(style) {
entry.classList.toggle('not-applied', style.excluded || style.sloppy); entry.classList.toggle('not-applied', style.excluded || style.sloppy);
entry.classList.toggle('regexp-partial', style.sloppy); entry.classList.toggle('regexp-partial', style.sloppy);
$('.exclude-by-domain-checkbox', entry).checked = styleExcluded(style, 'domain'); // setup exclude
$('.exclude-by-domain', entry).title = getExcludeRule('domain'); for (const type of ['site', 'domain', 'url']) {
try {
$(`.exclude-by-${type}-checkbox`, entry).checked = styleExcluded(style, type);
$(`.exclude-by-${type}`, entry).title = getExcludeRule(type);
} catch (err) {
$(`.exclude-by-${type}-checkbox`, entry).disabled = true;
$(`.exclude-by-${type}`, entry).title = err.message || String(err);
$(`.exclude-by-${type}`, entry).classList.add('disabled');
}
}
$('.exclude-by-site-checkbox', entry).checked = styleExcluded(style, 'site'); try {
$('.exclude-by-site', entry).title = getExcludeRule('site'); if (getExcludeRule('domain') === getExcludeRule('url')) {
$('.exclude-by-url-checkbox', entry).disabled = true;
const excludeByUrlCheckbox = $('.exclude-by-url-checkbox', entry); $('.exclude-by-url', entry).title = chrome.i18n.getMessage('excludeStyleByUrlRedundant');
const isRedundant = getExcludeRule('domain') === getExcludeRule('url'); $('.exclude-by-url', entry).classList.add('disabled');
excludeByUrlCheckbox.checked = !isRedundant && styleExcluded(style, 'url'); }
excludeByUrlCheckbox.disabled = isRedundant; } catch (err) {
// pass
const excludeByUrlLabel = $('.exclude-by-url', entry); }
excludeByUrlLabel.classList.toggle('disabled', isRedundant);
excludeByUrlLabel.title = isRedundant ?
chrome.i18n.getMessage('excludeStyleByUrlRedundant') : getExcludeRule('url');
return entry; return entry;
} }
@ -364,14 +370,20 @@ function styleExcluded({exclusions}, type) {
function getExcludeRule(type) { function getExcludeRule(type) {
const u = new URL(tabURL); const u = new URL(tabURL);
if (!u.host) {
throw new Error('current URL has no host');
}
if (type === 'domain') { if (type === 'domain') {
return u.protocol + '//' + u.host + '/*'; return u.protocol + '//' + u.host + '/*';
} }
if (type === 'site') { if (type === 'site') {
// FIXME: what should we do if `getRootDomain` return undefined? const rootDomain = getRootDomain(u.host);
return u.protocol + '//*.' + getRootDomain(u.host) + '/*'; if (!rootDomain) {
throw new Error('current URL has no root domain');
}
return u.protocol + '//*.' + rootDomain + '/*';
} }
return escapeGlob(u.origin + u.pathname) + '*'; return u.protocol + '//' + u.host + escapeGlob(u.pathname) + '*';
} }
function getRootDomain(domain) { function getRootDomain(domain) {