2018-01-25 01:42:02 +00:00
|
|
|
/*
|
|
|
|
global messageBox resolveWith
|
2018-04-19 18:00:27 +00:00
|
|
|
gloabl editor showHelp getSectionsHashes
|
|
|
|
global popupExclusions
|
2018-01-25 01:42:02 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const exclusions = (() => {
|
|
|
|
|
2018-04-19 18:00:27 +00:00
|
|
|
// `\S*\*\S*` => `foo*`, `*bar`, `f*bar`
|
|
|
|
// `\S+\.\S+` => `foo.bar`, `f.b`
|
|
|
|
// see https://regex101.com/r/NUuwiu/2
|
|
|
|
const validExclusionRegex = /^(\S*\*\S*|\S+\.\S+)$/;
|
|
|
|
// ms to wait before validating user input
|
|
|
|
const saveDelay = 250;
|
|
|
|
|
2018-01-25 01:42:02 +00:00
|
|
|
// get exclusions from a select element
|
2018-04-19 18:00:27 +00:00
|
|
|
function get() {
|
|
|
|
exclusions.list = {};
|
|
|
|
$$('#excluded-wrap input').forEach(input => {
|
|
|
|
const url = input.value;
|
|
|
|
if (url && validExclusionRegex.test(url)) {
|
|
|
|
exclusions.list[url] = createRegExp(url);
|
|
|
|
}
|
2018-01-25 01:42:02 +00:00
|
|
|
});
|
2018-04-19 18:00:27 +00:00
|
|
|
return exclusions.list;
|
2018-01-25 01:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function createRegExp(url) {
|
2018-04-19 18:00:27 +00:00
|
|
|
// Include boundaries to prevent `e.c` from matching `google.com`
|
|
|
|
const prefix = url.startsWith('^') ? '' : '\\b';
|
|
|
|
const suffix = url.endsWith('$') ? '' : '\\b';
|
|
|
|
// Only escape `.`; alter `*`; all other regex allowed
|
|
|
|
return `${prefix}${url.replace(/\./g, '\\.').replace(/\*/g, '.*?')}${suffix}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function addExclusionEntry({container, value, insertAfter}) {
|
|
|
|
const item = template.exclusionEntry.cloneNode(true);
|
|
|
|
const input = $('input', item);
|
|
|
|
const regex = validExclusionRegex.toString();
|
|
|
|
input.value = value;
|
|
|
|
input.setAttribute('pattern', regex.substring(1, regex.length - 1));
|
|
|
|
if (insertAfter) {
|
|
|
|
insertAfter.insertAdjacentElement('afterend', item);
|
|
|
|
} else {
|
|
|
|
container.appendChild(item);
|
|
|
|
}
|
|
|
|
input.focus();
|
2018-01-25 01:42:02 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 18:00:27 +00:00
|
|
|
function populateList() {
|
|
|
|
// List should never be empty - need to add an empty input
|
|
|
|
const list = exclusions.list.length ? exclusions.list : [''];
|
|
|
|
const block = $('#excluded-wrap');
|
|
|
|
block.textContent = '';
|
|
|
|
const container = document.createDocumentFragment();
|
|
|
|
list.forEach(value => {
|
|
|
|
addExclusionEntry({container, value});
|
2018-01-25 01:42:02 +00:00
|
|
|
});
|
2018-04-19 18:00:27 +00:00
|
|
|
block.appendChild(container);
|
2018-01-25 01:42:02 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 18:00:27 +00:00
|
|
|
function validateEntry(input) {
|
|
|
|
const lists = Object.keys(get());
|
|
|
|
const url = input.value;
|
|
|
|
const index = $$('.exclusion-entry input:valid').indexOf(input);
|
2018-01-25 01:42:02 +00:00
|
|
|
// Generic URL globs; e.g. "https://test.com/*" & "*.test.com"
|
2018-04-19 18:00:27 +00:00
|
|
|
return !(lists.includes(url) && lists.indexOf(url) !== index) &&
|
|
|
|
validExclusionRegex.test(url);
|
2018-01-25 01:42:02 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 18:00:27 +00:00
|
|
|
function updateList() {
|
|
|
|
const list = get();
|
|
|
|
const keys = Object.keys(list);
|
|
|
|
if (exclusions.savedValue !== keys.join(',')) {
|
|
|
|
exclusions.saveValue = keys.join(',');
|
|
|
|
exclusions.list = list;
|
2018-01-25 01:42:02 +00:00
|
|
|
}
|
2018-04-19 18:00:27 +00:00
|
|
|
debounce(save, 100, {});
|
|
|
|
updateStats();
|
2018-01-25 01:42:02 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 18:00:27 +00:00
|
|
|
function deleteExclusions(entry) {
|
|
|
|
if ($('#excluded-wrap').children.length === 1) {
|
|
|
|
const input = $('.exclusion-input', entry);
|
|
|
|
input.value = '';
|
|
|
|
input.focus();
|
|
|
|
} else {
|
|
|
|
const nextFocus = entry.previousElementSibling || entry.nextElementSibling;
|
|
|
|
entry.parentNode.removeChild(entry);
|
|
|
|
if (nextFocus) {
|
|
|
|
$('input', nextFocus).focus();
|
|
|
|
}
|
2018-01-25 01:42:02 +00:00
|
|
|
}
|
2018-04-19 18:00:27 +00:00
|
|
|
updateList();
|
2018-01-25 01:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function excludeAction(event) {
|
2018-04-19 18:00:27 +00:00
|
|
|
event.preventDefault();
|
2018-01-25 01:42:02 +00:00
|
|
|
const target = event.target;
|
2018-04-19 18:00:27 +00:00
|
|
|
const entry = target.closest('.exclusion-entry');
|
|
|
|
if (target.classList.contains('exclusion-add')) {
|
|
|
|
addExclusionEntry({
|
|
|
|
container: $('#excluded-wrap'),
|
|
|
|
value: '',
|
|
|
|
insertAfter: entry
|
|
|
|
});
|
|
|
|
} else if (target.classList.contains('exclusion-delete')) {
|
|
|
|
deleteExclusions(entry);
|
2018-01-25 01:42:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 18:00:27 +00:00
|
|
|
function excludeValidate(event) {
|
|
|
|
const target = event.target;
|
|
|
|
target.setCustomValidity('');
|
|
|
|
target.title = '';
|
|
|
|
if (target.matches(':valid')) {
|
|
|
|
if (!validateEntry(target)) {
|
|
|
|
target.setCustomValidity(t('exclusionsvalidateEntry'));
|
|
|
|
target.title = t('exclusionsvalidateEntry');
|
|
|
|
} else {
|
|
|
|
updateList();
|
|
|
|
}
|
2018-01-25 01:42:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateStats() {
|
2018-04-19 18:00:27 +00:00
|
|
|
const total = Object.keys(exclusions.list).length;
|
|
|
|
$('#excluded-stats').textContent = total ? t('exclusionsStatus', [total]) : '';
|
2018-01-25 01:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function showExclusionHelp(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
showHelp(t('exclusionsHelpTitle'), t('exclusionsHelp').replace(/\n/g, '<br>'), 'info');
|
|
|
|
}
|
|
|
|
|
|
|
|
function onRuntimeMessage(msg) {
|
2018-04-19 18:00:27 +00:00
|
|
|
if (msg.method === 'exclusionsUpdate' && msg.style && msg.style.exclusions) {
|
|
|
|
update({list: Object.keys(msg.style.exclusions), isUpdating: true});
|
|
|
|
// update popup, if loaded
|
|
|
|
if (typeof popupExclusions !== 'undefined') {
|
|
|
|
popupExclusions.selectExclusions(msg.style.exclusions);
|
|
|
|
}
|
2018-01-25 01:42:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 18:00:27 +00:00
|
|
|
function update({list = exclusions.list, isUpdating}) {
|
|
|
|
if (!isUpdating) {
|
|
|
|
exclusions.list = list;
|
|
|
|
populateList();
|
|
|
|
}
|
2018-01-25 01:42:02 +00:00
|
|
|
updateStats();
|
|
|
|
}
|
|
|
|
|
2018-04-19 18:00:27 +00:00
|
|
|
function save({id, exclusionList = get()}) {
|
|
|
|
// get last saved version
|
|
|
|
API.getStyles({id: id || exclusions.id}).then(([style]) => {
|
|
|
|
style.exclusions = exclusionList;
|
|
|
|
style.reason = 'exclusionsUpdate';
|
|
|
|
API.saveStyle(style);
|
|
|
|
});
|
2018-01-25 01:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function init(style) {
|
2018-04-19 18:00:27 +00:00
|
|
|
const block = $('#exclusions');
|
2018-01-25 01:42:02 +00:00
|
|
|
const list = Object.keys(style.exclusions || {});
|
|
|
|
const size = list.length;
|
2018-04-19 18:00:27 +00:00
|
|
|
exclusions.id = style.id;
|
|
|
|
exclusions.savedValue = list.join(',');
|
2018-01-25 01:42:02 +00:00
|
|
|
exclusions.list = list;
|
2018-04-19 18:00:27 +00:00
|
|
|
if (size) {
|
|
|
|
block.setAttribute('open', true);
|
|
|
|
} else {
|
|
|
|
block.removeAttribute('open');
|
|
|
|
}
|
|
|
|
update({});
|
2018-01-25 01:42:02 +00:00
|
|
|
|
|
|
|
$('#excluded-wrap').onclick = excludeAction;
|
2018-04-19 18:00:27 +00:00
|
|
|
$('#excluded-wrap').oninput = event => debounce(excludeValidate, saveDelay, event);
|
2018-01-25 01:42:02 +00:00
|
|
|
$('#excluded-list-help').onclick = showExclusionHelp;
|
|
|
|
chrome.runtime.onMessage.addListener(onRuntimeMessage);
|
|
|
|
}
|
|
|
|
|
2018-04-19 18:00:27 +00:00
|
|
|
return {init, get, update, save, createRegExp};
|
2018-01-25 01:42:02 +00:00
|
|
|
})();
|