Add tab communication

This commit is contained in:
Rob Garrison 2018-07-15 12:59:51 -05:00
parent 983a7bc219
commit bfe54ab4c4
5 changed files with 22 additions and 5 deletions

View File

@ -298,7 +298,6 @@ function filterStylesInternal({
const filtered = asHash ? {length: 0} : []; const filtered = asHash ? {length: 0} : [];
const needSections = asHash || matchUrl !== null; const needSections = asHash || matchUrl !== null;
const matchUrlBase = matchUrl && matchUrl.includes('#') && matchUrl.split('#', 1)[0]; const matchUrlBase = matchUrl && matchUrl.includes('#') && matchUrl.split('#', 1)[0];
let style; let style;
for (let i = 0; (style = styles[i]); i++) { for (let i = 0; (style = styles[i]); i++) {
if ((enabled === null || style.enabled === enabled) if ((enabled === null || style.enabled === enabled)
@ -483,9 +482,14 @@ function getApplicableSections({
// but the spec is outdated and doesn't account for SPA sites // but the spec is outdated and doesn't account for SPA sites
// so we only respect it in case of url("http://exact.url/without/hash") // so we only respect it in case of url("http://exact.url/without/hash")
}) { }) {
if (!skipUrlCheck && !URLS.supported(matchUrl) || omitCode !== false && isPageExcluded(matchUrl, style.exclusions)) { const excluded = isPageExcluded(matchUrl, style.exclusions);
if (!skipUrlCheck && !URLS.supported(matchUrl) || omitCode !== false && excluded) {
return []; return [];
} }
// Show excluded style in popup
if (excluded) {
return [''];
}
const sections = []; const sections = [];
for (const section of style.sections) { for (const section of style.sections) {
const {urls, domains, urlPrefixes, regexps, code} = section; const {urls, domains, urlPrefixes, regexps, code} = section;

View File

@ -190,6 +190,12 @@ function onRuntimeMessage(request) {
case 'editDeleteText': case 'editDeleteText':
document.execCommand('delete'); document.execCommand('delete');
break; break;
case 'exclusionsUpdated':
debounce(() => exclusions.update({
list: Object.keys(request.style.exclusions),
isUpdating: false
}), 100);
break;
} }
} }

View File

@ -16,13 +16,17 @@ const exclusions = (() => {
// get exclusions from a select element // get exclusions from a select element
function get() { function get() {
exclusions.list = {}; const list = {};
$$('#excluded-wrap input').forEach(input => { $$('#excluded-wrap input').forEach(input => {
const url = input.value; const url = input.value;
if (url && validExclusionRegex.test(url)) { if (url && validExclusionRegex.test(url)) {
exclusions.list[url] = createRegExp(url); list[url] = createRegExp(url);
} }
}); });
exclusions.list = Object.keys(list).sort().reduce((acc, ex) => {
acc[ex] = list[ex];
return acc;
}, {});
return exclusions.list; return exclusions.list;
} }
@ -54,7 +58,7 @@ const exclusions = (() => {
const block = $('#excluded-wrap'); const block = $('#excluded-wrap');
block.textContent = ''; block.textContent = '';
const container = document.createDocumentFragment(); const container = document.createDocumentFragment();
list.forEach(value => { list.sort().forEach(value => {
addExclusionEntry({container, value}); addExclusionEntry({container, value});
}); });
block.appendChild(container); block.appendChild(container);
@ -158,6 +162,7 @@ const exclusions = (() => {
style.exclusions = exclusionList; style.exclusions = exclusionList;
style.reason = 'exclusionsUpdated'; style.reason = 'exclusionsUpdated';
API.saveStyle(style); API.saveStyle(style);
notifyAllTabs({method: 'exclusionsUpdated', style, id});
}); });
} }

View File

@ -162,6 +162,7 @@ const popupExclusions = (() => {
entry.styleMeta = style; entry.styleMeta = style;
entry.classList.toggle('excluded', isExcluded(tabURL, style.exclusions)); entry.classList.toggle('excluded', isExcluded(tabURL, style.exclusions));
} }
notifyAllTabs({method: 'exclusionsUpdated', style, id: entry.styleId});
} }
return Promise.resolve(); return Promise.resolve();
} }

View File

@ -35,6 +35,7 @@ function onRuntimeMessage(msg) {
switch (msg.method) { switch (msg.method) {
case 'styleAdded': case 'styleAdded':
case 'styleUpdated': case 'styleUpdated':
case 'exclusionsUpdated':
if (msg.reason === 'editPreview') return; if (msg.reason === 'editPreview') return;
handleUpdate(msg.style); handleUpdate(msg.style);
break; break;