Modify input method

This commit is contained in:
Rob Garrison 2018-04-19 13:00:27 -05:00
parent 9f75b69cd8
commit 2b4a1a5635
11 changed files with 364 additions and 344 deletions

View File

@ -205,18 +205,6 @@
"configOnChangeTooltip": { "configOnChangeTooltip": {
"message": "Autosave and apply changes automatically" "message": "Autosave and apply changes automatically"
}, },
"genericAdd": {
"message": "Add",
"description": "Used in various places to select/perform an add action."
},
"genericDelete": {
"message": "Delete",
"description": "Used in various places to select/perform a delete action."
},
"genericEdit": {
"message": "Edit",
"description": "Used in various places to select/perform an edit action."
},
"genericError": { "genericError": {
"message": "Error", "message": "Error",
"description": "Used in various places to indicate some error occurred." "description": "Used in various places to indicate some error occurred."
@ -380,42 +368,29 @@
"description": "Label for a full url with a subdirectory to be used as the beginning portion of a URL to match to exclude a style" "description": "Label for a full url with a subdirectory to be used as the beginning portion of a URL to match to exclude a style"
}, },
"exclusionsAddTitle": { "exclusionsAddTitle": {
"message": "Add excluded page", "message": "Add exclusion",
"description": "Title of popup to add an excluded page (URL)" "description": "Title of popup to add an excluded site or page (URL)"
},
"exclusionsEditTitle": {
"message": "Edit excluded page(s)",
"description": "Title of popup to edit an excluded page (URL)"
},
"exclusionsEmpty": {
"message": "No exclusions",
"description": "Label shown when there are no global exclusions"
},
"exclusionsDeleteConfirmation": {
"message": "Are you sure you want to delete $number$ entries?",
"description": "Delete confirmation dialog message",
"placeholders": {
"number": {
"content": "$1"
}
}
}, },
"exclusionsHeader": { "exclusionsHeader": {
"message": "Excluded Pages", "message": "Excluded",
"description": "Title of user configurable lists of site urls to exclude per style" "description": "Title of user configurable lists of site urls to exclude per style"
}, },
"exclusionsHelp": { "exclusionsHelp": {
"message": "Add one or more exclusions for each style. An exclusion is a string that will match a web location (URL). If a match is found, the given style (and all internal sections) will not be applied to that page. A list of exclusions is set separately from the userstyle so that it will not be effected when updating or editing the style itself.\n\nThis is useful because you can exclude websites that would be effected by a global style.\n\nThe exclusion string may contain wildcards (\"*\") to match any portion of the URL, e.g. \"forum.*.com\" will exclude the forum sub-domains of all dot-com top level domains.\n\nRegular expressions are not supported.", "message": "Exclusion entries are only checked when a style is set to be applied to a page, and if an exclusion is found, the given style (and all internal sections) will not be applied to that page.\n\nThe list of exclusions is set separately from the userstyle so that it will not be effected when updating or editing the style itself. This is useful because you can exclude pages that would be otherwise be effected by a global style.\n\nAdd one or more exclusion entries for each style. An exclusion entry string contains a pattern that will match a web location (URL). This string may contain wildcards (\"*\") to match any portion of a URL, e.g. \"forum.*.com\" will exclude the forum sub-domains of all top level dot-com domains. Regular expressions are allowed, except `.` and `*` are altered, and are saved as a string so character classes must be doubly escaped (e.g. `\\w`).\n\nExcluded pages are automatically updated while typing; invalid entries will be removed on page reload!",
"description": "Help text for user set style exclusions" "description": "Help text for user set style exclusions"
}, },
"exclusionsHelpTitle": { "exclusionsHelpTitle": {
"message": "Set Style Exclusions", "message": "Set Style Exclusions",
"description": "Header text for help modal" "description": "Header text for help modal"
}, },
"exclusionsInvalidUrl": { "exclusionsvalidateEntry": {
"message": "Enter a unique and valid URL", "message": "Enter a unique and valid URL",
"description": "Text for an alert notifying the user that an entered URL is not unique or invalid" "description": "Text for an alert notifying the user that an entered URL is not unique or invalid"
}, },
"exclusionsPopupTitle": {
"message": "Exclude the site or page",
"description": "Title of exclusion popup dialog within the extension popup"
},
"exclusionsPopupTip": { "exclusionsPopupTip": {
"message": "Right-click to edit exclusions on this page", "message": "Right-click to edit exclusions on this page",
"description": "Title on the checkbox in the popup to let the user know how to edit exclusions on the current page" "description": "Title on the checkbox in the popup to let the user know how to edit exclusions on the current page"
@ -425,7 +400,7 @@
"description": "Prefix label added to the applies to column in the style manager" "description": "Prefix label added to the applies to column in the style manager"
}, },
"exclusionsStatus": { "exclusionsStatus": {
"message": "$number$ sites", "message": "$number$ pages",
"description": "Label added next to the Excluded Pages header when 'number' is not zero", "description": "Label added next to the Excluded Pages header when 'number' is not zero",
"placeholders": { "placeholders": {
"number": { "number": {

View File

@ -19,6 +19,7 @@ var cachedStyles = {
byId: new Map(), // all styles indexed by id byId: new Map(), // all styles indexed by id
filters: new Map(), // filterStyles() parameters mapped to the returned results, 10k max filters: new Map(), // filterStyles() parameters mapped to the returned results, 10k max
regexps: new Map(), // compiled style regexps regexps: new Map(), // compiled style regexps
exclusions: new Map(), // compiled exclusion regexps
urlDomains: new Map(), // getDomain() results for 100 last checked urls urlDomains: new Map(), // getDomain() results for 100 last checked urls
needTransitionPatch: new Map(), // FF bug workaround needTransitionPatch: new Map(), // FF bug workaround
mutex: { mutex: {
@ -250,6 +251,7 @@ function filterStyles({
strictRegexp, strictRegexp,
blankHash, blankHash,
cacheKey, cacheKey,
omitCode,
}); });
} }
if (!omitCode) return styles; if (!omitCode) return styles;
@ -275,6 +277,7 @@ function filterStylesInternal({
strictRegexp, strictRegexp,
blankHash, blankHash,
cacheKey, cacheKey,
omitCode,
}) { }) {
if (matchUrl && !cachedStyles.urlDomains.has(matchUrl)) { if (matchUrl && !cachedStyles.urlDomains.has(matchUrl)) {
cachedStyles.urlDomains.set(matchUrl, getDomains(matchUrl)); cachedStyles.urlDomains.set(matchUrl, getDomains(matchUrl));
@ -309,6 +312,7 @@ function filterStylesInternal({
stopOnFirst: !asHash, stopOnFirst: !asHash,
skipUrlCheck: true, skipUrlCheck: true,
matchUrlBase, matchUrlBase,
omitCode,
}); });
if (asHash) { if (asHash) {
if (sections.length) { if (sections.length) {
@ -423,10 +427,9 @@ function saveStyle(style) {
style.id = style.id || event.target.result; style.id = style.id || event.target.result;
invalidateCache(existed ? {updated: style} : {added: style}); invalidateCache(existed ? {updated: style} : {added: style});
if (notify) { if (notify) {
notifyAllTabs({ const method = reason === 'exclusionsUpdate' ? reason :
method: existed ? 'styleUpdated' : 'styleAdded', existed ? 'styleUpdated' : 'styleAdded';
style, codeIsUpdated, reason, notifyAllTabs({method, style, codeIsUpdated, reason});
});
} }
return style; return style;
} }
@ -444,11 +447,27 @@ function deleteStyle({id, notify = true}) {
}); });
} }
function checkExclusions(matchUrl, exclusions = {}) {
const values = Object.values(exclusions); function compileExclusionRegexps(exclusions) {
return values.length && exclusions.forEach(exclusion => {
values.reduce((acc, exclude) => acc || tryRegExp(exclude).test(matchUrl), false); if (!cachedStyles.exclusions.get(exclusion)) {
cachedStyles.exclusions.set(exclusion, tryRegExp(exclusion) || false);
} }
});
}
function isPageExcluded(matchUrl, exclusions = {}) {
const values = Object.values(exclusions);
if (!values.length) {
return false;
}
compileExclusionRegexps(values);
return values.some(exclude => {
const rx = cachedStyles.exclusions.get(exclude);
return rx && rx.test(matchUrl);
});
}
function getApplicableSections({ function getApplicableSections({
style, style,
@ -458,12 +477,13 @@ function getApplicableSections({
stopOnFirst, stopOnFirst,
skipUrlCheck, skipUrlCheck,
matchUrlBase = matchUrl.includes('#') && matchUrl.split('#', 1)[0], matchUrlBase = matchUrl.includes('#') && matchUrl.split('#', 1)[0],
omitCode,
// as per spec the fragment portion is ignored in @-moz-document: // as per spec the fragment portion is ignored in @-moz-document:
// https://www.w3.org/TR/2012/WD-css3-conditional-20120911/#url-of-doc // https://www.w3.org/TR/2012/WD-css3-conditional-20120911/#url-of-doc
// 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) || checkExclusions(matchUrl, style.exclusions)) { if (!skipUrlCheck && !URLS.supported(matchUrl) || omitCode !== false && isPageExcluded(matchUrl, style.exclusions)) {
return []; return [];
} }
const sections = []; const sections = [];
@ -642,6 +662,7 @@ function updateFiltersCache(style) {
matchUrlBase, matchUrlBase,
strictRegexp, strictRegexp,
skipUrlCheck: true, skipUrlCheck: true,
omitCode: false
}); });
if (sections.length) { if (sections.length) {
styles[id] = sections; styles[id] = sections;
@ -830,8 +851,8 @@ function detectSloppyRegexps({matchUrl, ids}) {
} }
} }
if (!hasRegExp) continue; if (!hasRegExp) continue;
const applied = getApplicableSections({style, matchUrl}); const applied = getApplicableSections({style, matchUrl, omitCode: false});
const wannabe = getApplicableSections({style, matchUrl, strictRegexp: false}); const wannabe = getApplicableSections({style, matchUrl, omitCode: false, strictRegexp: false});
results.push({ results.push({
id, id,
applied, applied,

View File

@ -172,6 +172,16 @@
</div> </div>
</template> </template>
<template data-id="exclusionEntry">
<div class="exclusion-entry">
<input class="exclusion-input" placeholder="*://*.google.com/*foo*">
<button class="exclusion-add" i18n-title="exclusionsAddTitle">+</button>
<button class="exclusion-delete" i18n-title="confirmDelete">
<svg class="svg-icon"><use xlink:href="#svg-icon-delete"/></svg>
</button>
</div>
</template>
<template data-id="find"> <template data-id="find">
<div data-type="content"> <div data-type="content">
<div data-type="input-wrapper"> <div data-type="input-wrapper">
@ -400,19 +410,15 @@
</div> </div>
</div> </div>
</details> </details>
<details id="exclusions" data-pref="editor.exclusions.expanded"> <details id="exclusions">
<summary> <summary>
<h2 i18n-text="exclusionsHeader">: <span id="excluded-stats"></span></h2> <h2 i18n-text="exclusionsHeader"></h2>
</summary>
<select id="excluded-list" class="style-contributor" size="4" multiple="true"></select>
<div id="excluded-wrap">
<button id="excluded-list-add" i18n-text="genericAdd"></button>
<button id="excluded-list-edit" i18n-text="genericEdit"></button>
<button id="excluded-list-delete" i18n-text="genericDelete"></button>
<a id="excluded-list-help" href="#" class="svg-inline-wrapper"> <a id="excluded-list-help" href="#" class="svg-inline-wrapper">
<svg class="svg-icon info"><use xlink:href="#svg-icon-help"/></svg> <svg class="svg-icon info"><use xlink:href="#svg-icon-help"/></svg>
</a> </a>
</div> <span id="excluded-stats"></span>
</summary>
<div id="excluded-wrap"></div>
</details> </details>
<details id="lint" class="hidden" data-pref="editor.lint.expanded"> <details id="lint" class="hidden" data-pref="editor.lint.expanded">
<summary> <summary>
@ -480,6 +486,10 @@
<path fill-rule="evenodd" d="M0 3v2h8v-2h-8z"/> <path fill-rule="evenodd" d="M0 3v2h8v-2h-8z"/>
</symbol> </symbol>
<symbol id="svg-icon-delete" viewBox="0 0 14 16">
<path fill-rule="evenodd" d="M11 2H9c0-.55-.45-1-1-1H5c-.55 0-1 .45-1 1H2c-.55 0-1 .45-1 1v1c0 .55.45 1 1 1v9c0 .55.45 1 1 1h7c.55 0 1-.45 1-1V5c.55 0 1-.45 1-1V3c0-.55-.45-1-1-1zm-1 12H3V5h1v8h1V5h1v8h1V5h1v8h1V5h1v9zm1-10H2V3h9v1z"/>
</symbol>
</svg> </svg>
</body> </body>

View File

@ -84,27 +84,22 @@ label {
margin-top: 0; margin-top: 0;
} }
#exclusions select { #exclusions .exclusion-input {
width: 100%; flex: auto;
height: auto;
overflow: hidden;
margin-bottom: 10px;
padding: 0 6px;
} }
#exclusions option { #exclusions button {
overflow: hidden; margin-left: 2px;
} }
#excluded-pages:empty { #exclusions .exclusion-input:invalid {
height: 1.4em; border-color: red;
border: none;
} }
#excludedError { #exclusions .exclusion-delete .svg-icon {
position: absolute; pointer-events: none;
top: 7px; vertical-align: text-top;
color: red; height: 15px;
} }
#excluded-stats:not(:empty) { #excluded-stats:not(:empty) {
@ -443,7 +438,7 @@ body[data-match-highlight="selection"] .CodeMirror-selection-highlight-scrollbar
margin: 0; margin: 0;
padding: 0; padding: 0;
} }
.applies-to li { .applies-to li, .exclusion-entry {
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;
list-style-type: none; list-style-type: none;

View File

@ -40,8 +40,6 @@ function createSourceEditor(style) {
style.enabled = value; style.enabled = value;
}; };
exclusions.onchange(dirty);
cm.on('changes', () => { cm.on('changes', () => {
dirty.modify('sourceGeneration', savedGeneration, cm.changeGeneration()); dirty.modify('sourceGeneration', savedGeneration, cm.changeGeneration());
updateLintReportIfEnabled(cm); updateLintReportIfEnabled(cm);
@ -201,15 +199,18 @@ function createSourceEditor(style) {
function save() { function save() {
if (!dirty.isDirty()) return; if (!dirty.isDirty()) return;
const code = cm.getValue(); const code = cm.getValue();
style.exclusions = exclusions.get(); const exclusionList = exclusions.get();
exclusions.save(style, dirty); exclusions.save({
id: style.id,
exclusionList
});
return ( return (
API.saveUsercssUnsafe({ API.saveUsercssUnsafe({
id: style.id, id: style.id,
reason: 'editSave', reason: 'editSave',
enabled: style.enabled, enabled: style.enabled,
sourceCode: code, sourceCode: code,
exclusions: style.exclusions exclusions: exclusionList
})) }))
.then(({style, errors}) => { .then(({style, errors}) => {
replaceStyle(style); replaceStyle(style);

View File

@ -1,190 +1,132 @@
/* /*
global messageBox resolveWith global messageBox resolveWith
gloabl editor showHelp onChange gloabl editor showHelp getSectionsHashes
global popupExclusions
*/ */
'use strict'; 'use strict';
const exclusions = (() => { const exclusions = (() => {
// `\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;
// get exclusions from a select element // get exclusions from a select element
function get(options = {}) { function get() {
const lists = {}; exclusions.list = {};
const excluded = options.exclusions || getMultiOptions(options); $$('#excluded-wrap input').forEach(input => {
excluded.forEach(list => { const url = input.value;
lists[list] = createRegExp(list); if (url && validExclusionRegex.test(url)) {
exclusions.list[url] = createRegExp(url);
}
}); });
return lists; return exclusions.list;
} }
function createRegExp(url) { function createRegExp(url) {
// returning a regex string; Object.assign is used on style & doesn't save RegExp // Include boundaries to prevent `e.c` from matching `google.com`
return url.replace(/[-/\\^$+?.()|[\]{}]/g, '\\$&').replace(/[*]/g, '.+?'); 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 getMultiOptions({select, selectedOnly, elements} = {}) { function addExclusionEntry({container, value, insertAfter}) {
return [...(select || exclusions.select).children].reduce((acc, opt) => { const item = template.exclusionEntry.cloneNode(true);
if (selectedOnly && opt.selected) { const input = $('input', item);
acc.push(elements ? opt : opt.value); const regex = validExclusionRegex.toString();
} else if (!selectedOnly) { input.value = value;
acc.push(elements ? opt : opt.value); input.setAttribute('pattern', regex.substring(1, regex.length - 1));
if (insertAfter) {
insertAfter.insertAdjacentElement('afterend', item);
} else {
container.appendChild(item);
} }
return acc; input.focus();
}, []);
} }
function populateSelect(options = []) { function populateList() {
exclusions.select.textContent = ''; // List should never be empty - need to add an empty input
const option = $create('option'); const list = exclusions.list.length ? exclusions.list : [''];
options.forEach(value => { const block = $('#excluded-wrap');
const opt = option.cloneNode(); block.textContent = '';
opt.value = value; const container = document.createDocumentFragment();
opt.textContent = value; list.forEach(value => {
opt.title = value; addExclusionEntry({container, value});
exclusions.select.appendChild(opt);
}); });
exclusions.lastValue = exclusions.select.textContent; block.appendChild(container);
} }
function openInputDialog({title, callback, value = ''}) { function validateEntry(input) {
messageBox({ const lists = Object.keys(get());
title, const url = input.value;
className: 'center', const index = $$('.exclusion-entry input:valid').indexOf(input);
contents: [
$create('div', {id: 'excludedError', textContent: '\xa0\xa0'}),
$create('input', {type: 'text', id: 'excluded-input', value})
],
buttons: [t('confirmOK'), t('confirmCancel')]
});
setTimeout(() => {
const btn = $('#message-box-buttons button', messageBox.element);
// not using onkeyup here because pressing enter to activate add/edit
// button fires onkeyup here when user releases the key
$('#excluded-input').onkeydown = event => {
if (event.which === 13) {
event.preventDefault();
callback.apply(btn);
}
};
btn.onclick = callback;
}, 1);
}
function validateURL(url) {
const lists = getMultiOptions();
// Generic URL globs; e.g. "https://test.com/*" & "*.test.com" // Generic URL globs; e.g. "https://test.com/*" & "*.test.com"
return !lists.includes(url) && /^(?:https?:\/\/)?([\w*]+\.)+[\w*./-]+/.test(url); return !(lists.includes(url) && lists.indexOf(url) !== index) &&
validExclusionRegex.test(url);
} }
function addExclusion() { function updateList() {
openInputDialog({ const list = get();
title: t('exclusionsAddTitle'), const keys = Object.keys(list);
callback: function () { if (exclusions.savedValue !== keys.join(',')) {
const value = $('#excluded-input').value; exclusions.saveValue = keys.join(',');
if (value && validateURL(value)) { exclusions.list = list;
exclusions.select.appendChild($create('option', {value, innerText: value}));
done();
messageBox.listeners.button.apply(this);
} else {
const errorBox = $('#excludedError', messageBox.element);
errorBox.textContent = t('exclusionsInvalidUrl');
setTimeout(() => {
errorBox.textContent = '';
}, 5000);
}
}
});
}
function editExclusion() {
const value = exclusions.select.value;
if (value) {
openInputDialog({
title: t('exclusionsAddTitle'),
value,
callback: function () {
const newValue = $('#excluded-input').value;
// only edit the first selected option
const option = getMultiOptions({selectedOnly: true, elements: true})[0];
if (newValue && validateURL(newValue) && option) {
option.value = newValue;
option.textContent = newValue;
option.title = newValue;
if (newValue !== value) {
// make it dirty!
exclusions.select.savedValue = '';
}
done();
messageBox.listeners.button.apply(this);
} else {
const errorBox = $('#excludedError', messageBox.element);
errorBox.textContent = t('exclusionsInvalidUrl');
setTimeout(() => {
errorBox.textContent = '';
}, 5000);
}
}
});
}
}
function deleteExclusions() {
const entries = getMultiOptions({selectedOnly: true, elements: true});
if (entries.length) {
messageBox
.confirm(t('exclusionsDeleteConfirmation', [entries.length]))
.then(ok => {
if (ok) {
entries.forEach(el => exclusions.select.removeChild(el));
done();
}
});
}
}
function excludeAction(event) {
const target = event.target;
if (target.id && target.id.startsWith('excluded-list-')) {
// class "excluded-list-(add/edit/delete)" -> ['excluded', 'list', 'add']
const type = target.id.split('-').pop();
switch (type) {
case 'add':
addExclusion();
break;
case 'edit':
editExclusion();
break;
case 'delete':
deleteExclusions();
break;
}
}
}
function done() {
if (editor) {
// make usercss dirty
exclusions.select.onchange();
} else {
// make regular userstyle dirty
onChange({target: exclusions.select});
} }
debounce(save, 100, {});
updateStats(); updateStats();
} }
function updateStats() { function deleteExclusions(entry) {
if (exclusions.select) { if ($('#excluded-wrap').children.length === 1) {
const excludedTotal = exclusions.select.children.length; const input = $('.exclusion-input', entry);
const state = excludedTotal === 0; input.value = '';
exclusions.select.setAttribute('size', excludedTotal || 1); input.focus();
$('#excluded-stats').textContent = state ? '' : t('exclusionsStatus', [excludedTotal]); } else {
toggleButtons(state); const nextFocus = entry.previousElementSibling || entry.nextElementSibling;
entry.parentNode.removeChild(entry);
if (nextFocus) {
$('input', nextFocus).focus();
}
}
updateList();
}
function excludeAction(event) {
event.preventDefault();
const target = event.target;
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);
} }
} }
function toggleButtons(state = false) { function excludeValidate(event) {
const noSelection = exclusions.select.value === ''; const target = event.target;
$('#excluded-list-edit').disabled = noSelection || state; target.setCustomValidity('');
$('#excluded-list-delete').disabled = noSelection || state; target.title = '';
if (target.matches(':valid')) {
if (!validateEntry(target)) {
target.setCustomValidity(t('exclusionsvalidateEntry'));
target.title = t('exclusionsvalidateEntry');
} else {
updateList();
}
}
}
function updateStats() {
const total = Object.keys(exclusions.list).length;
$('#excluded-stats').textContent = total ? t('exclusionsStatus', [total]) : '';
} }
function showExclusionHelp(event) { function showExclusionHelp(event) {
@ -193,49 +135,51 @@ const exclusions = (() => {
} }
function onRuntimeMessage(msg) { function onRuntimeMessage(msg) {
if (msg.method === 'styleUpdated' && msg.style && msg.style.exclusions && exclusions.select) { if (msg.method === 'exclusionsUpdate' && msg.style && msg.style.exclusions) {
update(Object.keys(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);
}
} }
} }
function update(list = exclusions.list) { function update({list = exclusions.list, isUpdating}) {
populateSelect(list); if (!isUpdating) {
exclusions.list = list;
populateList();
}
updateStats(); updateStats();
} }
function onchange(dirty) { function save({id, exclusionList = get()}) {
exclusions.select.onchange = function () { // get last saved version
dirty.modify('exclusions', exclusions.lastValue, exclusions.select.textContent); API.getStyles({id: id || exclusions.id}).then(([style]) => {
}; style.exclusions = exclusionList;
}
function save(style, dirty) {
style.reason = 'exclusionsUpdate'; style.reason = 'exclusionsUpdate';
API.saveStyle(style); API.saveStyle(style);
if (dirty) { });
dirty.clear('exclusions');
}
} }
function init(style) { function init(style) {
const block = $('#exclusions');
const list = Object.keys(style.exclusions || {}); const list = Object.keys(style.exclusions || {});
const size = list.length; const size = list.length;
exclusions.select = $('#excluded-list'); exclusions.id = style.id;
exclusions.select.savedValue = String(size); exclusions.savedValue = list.join(',');
exclusions.list = list; exclusions.list = list;
update(); if (size) {
block.setAttribute('open', true);
} else {
block.removeAttribute('open');
}
update({});
$('#excluded-wrap').onclick = excludeAction; $('#excluded-wrap').onclick = excludeAction;
$('#excluded-wrap').oninput = event => debounce(excludeValidate, saveDelay, event);
$('#excluded-list-help').onclick = showExclusionHelp; $('#excluded-list-help').onclick = showExclusionHelp;
// Disable Edit & Delete buttons if nothing selected
exclusions.select.onclick = () => toggleButtons();
document.head.appendChild($create('style', `
#excluded-list:empty:after {
content: "${t('exclusionsEmpty')}";
}
`));
chrome.runtime.onMessage.addListener(onRuntimeMessage); chrome.runtime.onMessage.addListener(onRuntimeMessage);
} }
return {init, get, update, onchange, save, createRegExp, getMultiOptions}; return {init, get, update, save, createRegExp};
})(); })();

View File

@ -40,7 +40,6 @@ var prefs = new function Prefs() {
'editor.options': {}, // CodeMirror.defaults.* 'editor.options': {}, // CodeMirror.defaults.*
'editor.options.expanded': true, // UI element state: expanded/collapsed 'editor.options.expanded': true, // UI element state: expanded/collapsed
'editor.exclusions.expanded': false, // UI element state: expanded/collapsed
'editor.lint.expanded': true, // UI element state: expanded/collapsed 'editor.lint.expanded': true, // UI element state: expanded/collapsed
'editor.lineWrapping': true, // word wrap 'editor.lineWrapping': true, // word wrap
'editor.smartIndent': true, // 'smart' indent 'editor.smartIndent': true, // 'smart' indent

View File

@ -165,7 +165,7 @@
<script src="js/script-loader.js" async></script> <script src="js/script-loader.js" async></script>
<script src="js/storage-util.js" async></script> <script src="js/storage-util.js" async></script>
<script src="js/exclusions.js" async></script> <script src="js/exclusions.js" async></script>
<script src="popup/popup-exclusions.js" async></script> <script src="popup/popup-exclusions.js"></script>
</head> </head>
<body id="stylus-popup"> <body id="stylus-popup">
@ -181,6 +181,18 @@
</div> </div>
</div> </div>
<div id="exclude">
<div>
<strong>Style's Name</strong>
<span i18n-text="exclusionsPopupTitle"></span>
<select id="popup-exclusions" size="0" multiple></select>
<div>
<button i18n-text="confirmOK" data-cmd="ok"></button>
<button i18n-text="confirmCancel" data-cmd="cancel"></button>
</div>
</div>
</div>
<div id="installed"> <div id="installed">
<aside id="hotkey-info" i18n-title="popupHotkeysTooltip"></aside> <aside id="hotkey-info" i18n-title="popupHotkeysTooltip"></aside>
</div> </div>

View File

@ -6,8 +6,6 @@ global exclusions
const popupExclusions = (() => { const popupExclusions = (() => {
const popupWidth = '400px';
// return matches on url ending to prevent duplicates in the exclusion list // return matches on url ending to prevent duplicates in the exclusion list
// e.g. http://test.com and http://test.com/* are equivalent // e.g. http://test.com and http://test.com/* are equivalent
// this function would return ['', '/*'] // this function would return ['', '/*']
@ -25,7 +23,8 @@ const popupExclusions = (() => {
function processURL(url) { function processURL(url) {
const results = []; const results = [];
const protocol = url.match(/\w+:\/\//); const protocol = url.match(/\w+:\/\//);
const parts = url.replace(/(\w+:\/\/|[#?].*$)/g, '').split('/'); // remove ending '/', protocol, hash & search strings
const parts = url.replace(/\/$/, '').replace(/(\w+:\/\/|[#?].*$)/g, '').split('/');
const domain = parts[0].split('.'); const domain = parts[0].split('.');
/* /*
Domain: a.b.com Domain: a.b.com
@ -45,28 +44,42 @@ const popupExclusions = (() => {
return results.reverse(); return results.reverse();
} }
function shortenURL(text) {
const len = text.length;
let prefix = '\u2026';
// account for URL that end with a '/'
let index = (text.endsWith('/') ? text.substring(0, len - 1) : text).lastIndexOf('/');
if (index < 0 || len - index < 2) {
index = 0;
prefix = '';
}
return prefix + text.substring(index, len);
}
function createOption(option) { function createOption(option) {
// ["Domain/Prefix", "{url}"] // ["Domain/Prefix", "{url}"]
return $create('option', { return $create('option', {
value: option[1], value: option[1],
title: option[1], title: option[1],
textContent: `${option[0]}: ${option[1]}` textContent: `${option[0]}: ${shortenURL(option[1])}`
}); });
} }
function createPopupContent(url) { function getMultiOptions({select, selectedOnly} = {}) {
return [...select.children].reduce((acc, opt) => {
if (selectedOnly && opt.selected || !selectedOnly) {
acc.push(opt.value);
}
return acc;
}, []);
}
function updatePopupContent(url) {
const options = processURL(url); const options = processURL(url);
return [ const renderBin = document.createDocumentFragment();
$create('h2', {textContent: t('exclusionsEditTitle')}), options.map(option => renderBin.appendChild(createOption(option)));
$create('select', { $('#popup-exclusions').textContent = '';
id: 'popup-exclusions', $('#popup-exclusions').appendChild(renderBin);
size: options.length,
multiple: 'true',
value: ''
}, [
...options.map(option => createOption(option))
])
];
} }
function getIframeURLs(style) { function getIframeURLs(style) {
@ -75,62 +88,89 @@ const popupExclusions = (() => {
chrome.webNavigation.getAllFrames({ chrome.webNavigation.getAllFrames({
tabId: tab.id tabId: tab.id
}, frames => { }, frames => {
const urls = frames.reduce((acc, frame) => processURL(frame.url), []); const urls = frames.reduce((acc, frame) => [...acc, ...processURL(frame.url)], []);
updateSelections(style, urls); updateSelections(style, urls);
}); });
} }
}); });
} }
function updateSelections(style, newOptions = []) { function selectExclusions(exclusions) {
const select = $('select', messageBox.element); const select = $('#exclude select');
const exclusions = Object.keys(style.exclusions || {}); const excludes = Object.keys(exclusions || {});
if (newOptions.length) {
const currentOptions = [...select.children].map(opt => opt.value);
newOptions.forEach(opt => {
if (!currentOptions.includes(opt[1])) {
select.appendChild(createOption(opt));
}
});
select.size = select.children.length;
}
[...select.children].forEach(option => { [...select.children].forEach(option => {
if (exclusionExists(exclusions, option.value).length) { if (exclusionExists(excludes, option.value).length) {
option.selected = true; option.selected = true;
} }
}); });
} }
function openPopupDialog(style, tabURL) { function updateSelections(style, newOptions = []) {
const msgBox = messageBox({ const wrap = $('#exclude');
title: style.name, const select = $('select', wrap);
className: 'center content-left', if (newOptions.length) {
contents: createPopupContent(tabURL), const currentOptions = [...select.children].map(opt => opt.value);
buttons: [t('confirmOK'), t('confirmCancel')], newOptions.forEach(opt => {
onshow: box => { if (!currentOptions.includes(opt[1])) {
const contents = box.firstElementChild; select.appendChild(createOption(opt));
contents.style = `max-width: calc(${popupWidth} - 20px); max-height: none;`; // newOptions may have duplicates (e.g. multiple iframes from same source)
document.body.style.minWidth = popupWidth; currentOptions.push(opt[1]);
document.body.style.minHeight = popupWidth;
updateSelections(style);
getIframeURLs(style);
$('#message-box-buttons button', messageBox.element).onclick = function () {
handlePopupSave(style, this);
};
} }
})
.then(() => {
document.body.style.minWidth = '';
document.body.style.minHeight = '';
}); });
return msgBox; select.size = select.children.length;
// hide select, then calculate & adjust height
select.style.height = '0';
document.body.style.height = `${select.scrollHeight + wrap.offsetHeight}px`;
select.style.height = '';
}
selectExclusions(style.exclusions);
} }
function handlePopupSave(style, button) { function isExcluded(matchUrl, exclusions = {}) {
const values = Object.values(exclusions);
return values.length && values.some(exclude => tryRegExp(exclude).test(matchUrl));
}
function openPopupDialog(entry, tabURL) {
const style = entry.styleMeta;
updateSelections(style, updatePopupContent(tabURL));
getIframeURLs(style);
const box = $('#exclude');
box.dataset.display = true;
box.style.cssText = '';
$('strong', box).textContent = style.name;
$('[data-cmd="ok"]', box).focus();
$('[data-cmd="ok"]', box).onclick = () => confirm(true);
$('[data-cmd="cancel"]', box).onclick = () => confirm(false);
window.onkeydown = event => {
const keyCode = event.keyCode || event.which;
if (!event.shiftKey && !event.ctrlKey && !event.altKey && !event.metaKey
&& (keyCode === 13 || keyCode === 27)) {
event.preventDefault();
confirm(keyCode === 13);
}
};
function confirm(ok) {
window.onkeydown = null;
animateElement(box, {
className: 'lights-on',
onComplete: () => (box.dataset.display = false),
});
if (ok) {
handlePopupSave(style);
entry.styleMeta = style;
entry.classList.toggle('excluded', isExcluded(tabURL, style.exclusions));
document.body.style.height = '';
}
}
return Promise.resolve();
}
function handlePopupSave(style) {
const current = Object.keys(style.exclusions); const current = Object.keys(style.exclusions);
const select = $('#popup-exclusions', messageBox.element); const select = $('#popup-exclusions', messageBox.element);
const all = exclusions.getMultiOptions({select}); const all = getMultiOptions({select});
const selected = exclusions.getMultiOptions({select, selectedOnly: true}); const selected = getMultiOptions({select, selectedOnly: true});
// Add exclusions // Add exclusions
selected.forEach(value => { selected.forEach(value => {
let exists = exclusionExists(current, value); let exists = exclusionExists(current, value);
@ -151,10 +191,12 @@ const popupExclusions = (() => {
delete style.exclusions[value + ending]; delete style.exclusions[value + ending];
}); });
}); });
exclusions.save(style); exclusions.save({
messageBox.listeners.button.apply(button); id: style.id,
exclusionList: style.exclusions
});
} }
return {openPopupDialog}; return {openPopupDialog, selectExclusions, isExcluded};
})(); })();

View File

@ -117,11 +117,15 @@ body > div:not(#installed):not(#message-box):not(.colorpicker-popup) {
margin-right: .5em; margin-right: .5em;
} }
#popup-exclusions-title {
margin: 2px;
}
#popup-exclusions { #popup-exclusions {
height: auto; height: auto;
overflow: hidden; overflow-y: auto;
max-width: 95%; max-width: 98%;
padding: 0 6px; padding: 0 2px;
} }
#popup-exclusions option { #popup-exclusions option {
@ -245,6 +249,10 @@ html[style] .entry {
padding-right: 5px; padding-right: 5px;
} }
.entry.excluded .style-name {
color: #d22;
}
.entry:nth-child(even) { .entry:nth-child(even) {
background-color: rgba(0, 0, 0, 0.05); background-color: rgba(0, 0, 0, 0.05);
} }
@ -490,7 +498,7 @@ body.blocked .actions > .main-controls {
/* confirm */ /* confirm */
#confirm { #confirm, #exclude {
align-items: center; align-items: center;
justify-content: center; justify-content: center;
z-index: 2147483647; z-index: 2147483647;
@ -507,21 +515,26 @@ body.blocked .actions > .main-controls {
animation-fill-mode: both; animation-fill-mode: both;
} }
#confirm.lights-on { #confirm.lights-on,
#exclude.lights-on {
animation: lights-on .25s ease-in-out; animation: lights-on .25s ease-in-out;
animation-fill-mode: both; animation-fill-mode: both;
} }
#confirm.lights-on, #confirm.lights-on,
#confirm.lights-on > div { #confirm.lights-on > div,
#exclude.lights-on,
#exclude.lights-on > div {
display: none; display: none;
} }
#confirm[data-display=true] { #confirm[data-display=true],
#exclude[data-display=true] {
display: flex; display: flex;
} }
#confirm > div { #confirm > div,
#exclude > div {
width: 80%; width: 80%;
max-height: 80%; max-height: 80%;
min-height: 6em; min-height: 6em;
@ -536,16 +549,23 @@ body.blocked .actions > .main-controls {
padding-bottom: .5em; padding-bottom: .5em;
} }
#confirm > div > div { #exclude select {
margin: .5em 0;
}
#confirm > div > div,
#exclude > div > div {
text-align: center; text-align: center;
} }
.non-windows #confirm > div > div { .non-windows #confirm > div > div,
.non-windows #exclude > div > div {
direction: rtl; direction: rtl;
text-align: right; text-align: right;
} }
#confirm > button { #confirm > button,
#exclude > button {
/* add a gap between buttons both for horizontal /* add a gap between buttons both for horizontal
or vertical (when the label is wide) layout */ or vertical (when the label is wide) layout */
margin: 0 .25em .25em 0; margin: 0 .25em .25em 0;

View File

@ -253,12 +253,15 @@ function createStyleElement({
container = installed, container = installed,
}) { }) {
const entry = template.style.cloneNode(true); const entry = template.style.cloneNode(true);
const excluded = popupExclusions.isExcluded(tabURL, style.exclusions);
entry.setAttribute('style-id', style.id); entry.setAttribute('style-id', style.id);
Object.assign(entry, { Object.assign(entry, {
id: ENTRY_ID_PREFIX_RAW + style.id, id: ENTRY_ID_PREFIX_RAW + style.id,
styleId: style.id, styleId: style.id,
styleIsUsercss: Boolean(style.usercssData), styleIsUsercss: Boolean(style.usercssData),
className: entry.className + ' ' + (style.enabled ? 'enabled' : 'disabled'), className: entry.className + ' ' +
(style.enabled ? 'enabled' : 'disabled') +
(excluded ? ' excluded' : ''),
onmousedown: handleEvent.maybeEdit, onmousedown: handleEvent.maybeEdit,
styleMeta: style styleMeta: style
}); });
@ -466,11 +469,9 @@ Object.assign(handleEvent, {
const entry = event.target.closest('.entry'); const entry = event.target.closest('.entry');
if (!chkbox.eventHandled) { if (!chkbox.eventHandled) {
chkbox.eventHandled = true; chkbox.eventHandled = true;
const style = entry.styleMeta;
popupExclusions popupExclusions
.openPopupDialog(style, tabURL) .openPopupDialog(entry, tabURL)
.then(() => { .then(() => {
entry.styleMeta = style;
chkbox.eventHandled = null; chkbox.eventHandled = null;
}); });
} }