2017-06-10 11:08:03 +00:00
|
|
|
/* global messageBox, getStyleWithNoCode, retranslateCSS */
|
2017-03-26 02:30:59 +00:00
|
|
|
'use strict';
|
2015-01-30 17:31:20 +00:00
|
|
|
|
2017-04-08 11:03:54 +00:00
|
|
|
let installed;
|
2017-04-16 11:24:49 +00:00
|
|
|
const filtersSelector = {
|
|
|
|
hide: '',
|
|
|
|
unhide: '',
|
|
|
|
};
|
2017-04-08 11:03:54 +00:00
|
|
|
|
2017-04-05 13:14:59 +00:00
|
|
|
const newUI = {
|
|
|
|
enabled: prefs.get('manage.newUI'),
|
|
|
|
favicons: prefs.get('manage.newUI.favicons'),
|
2017-04-18 18:49:04 +00:00
|
|
|
faviconsGray: prefs.get('manage.newUI.faviconsGray'),
|
2017-04-05 13:14:59 +00:00
|
|
|
targets: prefs.get('manage.newUI.targets'),
|
2017-04-08 11:03:54 +00:00
|
|
|
renderClass() {
|
|
|
|
document.documentElement.classList.toggle('newUI', newUI.enabled);
|
|
|
|
},
|
2017-04-05 13:14:59 +00:00
|
|
|
};
|
2017-04-08 11:03:54 +00:00
|
|
|
newUI.renderClass();
|
2017-04-05 13:14:59 +00:00
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
const TARGET_TYPES = ['domains', 'urls', 'urlPrefixes', 'regexps'];
|
2017-04-13 08:03:45 +00:00
|
|
|
const GET_FAVICON_URL = 'https://www.google.com/s2/favicons?domain=';
|
2017-04-09 06:43:51 +00:00
|
|
|
const OWN_ICON = chrome.runtime.getManifest().icons['16'];
|
2017-04-05 13:14:59 +00:00
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
const handleEvent = {};
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-08 11:03:54 +00:00
|
|
|
Promise.all([
|
|
|
|
getStylesSafe(),
|
|
|
|
onDOMready().then(initGlobalEvents),
|
|
|
|
]).then(([styles]) => {
|
|
|
|
showStyles(styles);
|
|
|
|
});
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-06-10 11:08:03 +00:00
|
|
|
if (FIREFOX) {
|
|
|
|
// TODO: remove when this bug is fixed in FF
|
|
|
|
retranslateCSS({
|
|
|
|
'.disabled h2::after':
|
|
|
|
'__MSG_genericDisabledLabel__',
|
|
|
|
'#update-all-no-updates[data-skipped-edited="true"]::after':
|
|
|
|
' __MSG_updateAllCheckSucceededSomeEdited__',
|
|
|
|
});
|
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
chrome.runtime.onMessage.addListener(onRuntimeMessage);
|
|
|
|
|
|
|
|
function onRuntimeMessage(msg) {
|
2017-03-21 01:32:38 +00:00
|
|
|
switch (msg.method) {
|
|
|
|
case 'styleUpdated':
|
|
|
|
case 'styleAdded':
|
|
|
|
handleUpdate(msg.style, msg);
|
|
|
|
break;
|
|
|
|
case 'styleDeleted':
|
|
|
|
handleDelete(msg.id);
|
|
|
|
break;
|
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2015-01-30 17:31:20 +00:00
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
function initGlobalEvents() {
|
2017-04-08 11:03:54 +00:00
|
|
|
installed = $('#installed');
|
2017-04-05 13:14:59 +00:00
|
|
|
installed.onclick = handleEvent.entryClicked;
|
2017-03-21 01:32:38 +00:00
|
|
|
$('#check-all-updates').onclick = checkUpdateAll;
|
2017-04-25 11:17:37 +00:00
|
|
|
$('#check-all-updates-force').onclick = checkUpdateAll;
|
2017-03-21 01:32:38 +00:00
|
|
|
$('#apply-all-updates').onclick = applyUpdateAll;
|
2017-04-26 20:55:54 +00:00
|
|
|
$('#update-history').onclick = showUpdateHistory;
|
2017-03-21 01:32:38 +00:00
|
|
|
$('#search').oninput = searchStyles;
|
2017-03-24 02:51:44 +00:00
|
|
|
$('#manage-options-button').onclick = () => chrome.runtime.openOptionsPage();
|
2017-04-09 06:43:51 +00:00
|
|
|
$('#manage-shortcuts-button').onclick = () => openURL({url: URLS.configureCommands});
|
2017-04-03 04:13:10 +00:00
|
|
|
$$('#header a[href^="http"]').forEach(a => (a.onclick = handleEvent.external));
|
2017-03-21 01:32:38 +00:00
|
|
|
|
|
|
|
// focus search field on / key
|
|
|
|
document.onkeypress = event => {
|
2017-04-18 18:56:32 +00:00
|
|
|
if ((event.keyCode || event.which) == 47
|
2017-03-21 01:32:38 +00:00
|
|
|
&& !event.altKey && !event.shiftKey && !event.ctrlKey && !event.metaKey
|
|
|
|
&& !event.target.matches('[type="text"], [type="search"]')) {
|
|
|
|
event.preventDefault();
|
|
|
|
$('#search').focus();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// remember scroll position on normal history navigation
|
2017-04-10 07:35:15 +00:00
|
|
|
window.onbeforeunload = rememberScrollPosition;
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-13 08:03:45 +00:00
|
|
|
$$('[data-toggle-on-click]').forEach(el => {
|
2017-04-18 18:38:22 +00:00
|
|
|
// dataset on SVG doesn't work in Chrome 49-??, works in 57+
|
|
|
|
const target = $(el.getAttribute('data-toggle-on-click'));
|
|
|
|
el.onclick = () => target.classList.toggle('hidden');
|
2017-04-13 08:03:45 +00:00
|
|
|
});
|
|
|
|
|
2017-04-18 18:49:04 +00:00
|
|
|
// triggered automatically by setupLivePrefs() below
|
2017-04-13 07:54:56 +00:00
|
|
|
enforceInputRange($('#manage.newUI.targets'));
|
2017-04-05 13:14:59 +00:00
|
|
|
|
2017-04-18 18:49:04 +00:00
|
|
|
// N.B. triggers existing onchange listeners
|
2017-04-21 16:39:34 +00:00
|
|
|
setupLivePrefs();
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-16 11:24:49 +00:00
|
|
|
$$('[data-filter]').forEach(el => {
|
|
|
|
el.onchange = handleEvent.filterOnChange;
|
2017-04-26 10:56:32 +00:00
|
|
|
if (el.closest('.hidden')) {
|
|
|
|
el.checked = false;
|
|
|
|
}
|
2017-04-16 11:24:49 +00:00
|
|
|
});
|
|
|
|
handleEvent.filterOnChange({forceRefilter: true});
|
|
|
|
|
2017-04-13 08:03:45 +00:00
|
|
|
$$('[id^="manage.newUI"]')
|
|
|
|
.forEach(el => (el.oninput = (el.onchange = switchUI)));
|
2017-04-05 13:14:59 +00:00
|
|
|
|
|
|
|
switchUI({styleOnly: true});
|
2015-01-30 17:31:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
|
|
|
|
function showStyles(styles = []) {
|
|
|
|
const sorted = styles
|
|
|
|
.map(style => ({name: style.name.toLocaleLowerCase(), style}))
|
2017-03-26 02:30:59 +00:00
|
|
|
.sort((a, b) => (a.name < b.name ? -1 : a.name == b.name ? 0 : 1));
|
2017-04-22 12:09:48 +00:00
|
|
|
let index = 0;
|
2017-04-27 19:12:32 +00:00
|
|
|
const scrollY = (history.state || {}).scrollY;
|
|
|
|
const shouldRenderAll = scrollY > window.innerHeight;
|
2017-03-21 01:32:38 +00:00
|
|
|
const renderBin = document.createDocumentFragment();
|
2017-04-27 19:12:32 +00:00
|
|
|
if (scrollY) {
|
2017-04-22 12:09:48 +00:00
|
|
|
renderStyles();
|
|
|
|
} else {
|
|
|
|
requestAnimationFrame(renderStyles);
|
|
|
|
}
|
2017-04-10 08:26:30 +00:00
|
|
|
|
2017-04-22 12:09:48 +00:00
|
|
|
function renderStyles() {
|
2017-03-21 01:32:38 +00:00
|
|
|
const t0 = performance.now();
|
2017-04-19 20:54:05 +00:00
|
|
|
let rendered = 0;
|
|
|
|
while (index < sorted.length
|
2017-04-22 12:09:48 +00:00
|
|
|
&& (shouldRenderAll || ++rendered < 10 || performance.now() - t0 < 10)) {
|
2017-03-26 08:43:45 +00:00
|
|
|
renderBin.appendChild(createStyleElement(sorted[index++]));
|
2017-03-25 02:35:54 +00:00
|
|
|
}
|
2017-04-16 11:24:49 +00:00
|
|
|
filterAndAppend({container: renderBin});
|
2017-03-21 01:32:38 +00:00
|
|
|
if (index < sorted.length) {
|
2017-04-22 12:09:48 +00:00
|
|
|
requestAnimationFrame(renderStyles);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ('scrollY' in (history.state || {})) {
|
|
|
|
setTimeout(window.scrollTo, 0, 0, history.state.scrollY);
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
2017-04-10 08:26:30 +00:00
|
|
|
if (newUI.enabled && newUI.favicons) {
|
|
|
|
debounce(handleEvent.loadFavicons, 16);
|
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
2015-01-30 17:31:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-03-26 08:43:45 +00:00
|
|
|
function createStyleElement({style, name}) {
|
2017-04-10 08:26:30 +00:00
|
|
|
// query the sub-elements just once, then reuse the references
|
|
|
|
if ((createStyleElement.parts || {}).newUI !== newUI.enabled) {
|
2017-04-22 12:09:48 +00:00
|
|
|
const entry = template[`style${newUI.enabled ? 'Compact' : ''}`];
|
2017-04-10 08:26:30 +00:00
|
|
|
createStyleElement.parts = {
|
|
|
|
newUI: newUI.enabled,
|
|
|
|
entry,
|
|
|
|
entryClassBase: entry.className,
|
2017-04-14 18:30:55 +00:00
|
|
|
checker: $('.checker', entry) || {},
|
2017-04-10 08:26:30 +00:00
|
|
|
nameLink: $('.style-name-link', entry),
|
|
|
|
editLink: $('.style-edit-link', entry) || {},
|
2017-06-18 12:46:53 +00:00
|
|
|
editHrefBase: 'edit.html?id=',
|
2017-04-10 08:26:30 +00:00
|
|
|
homepage: $('.homepage', entry),
|
2017-04-22 12:09:48 +00:00
|
|
|
homepageIcon: template[`homepageIcon${newUI.enabled ? 'Small' : 'Big'}`],
|
2017-04-10 08:26:30 +00:00
|
|
|
appliesTo: $('.applies-to', entry),
|
|
|
|
targets: $('.targets', entry),
|
|
|
|
expander: $('.expander', entry),
|
|
|
|
decorations: {
|
|
|
|
urlPrefixesAfter: '*',
|
|
|
|
regexpsBefore: '/',
|
|
|
|
regexpsAfter: '/',
|
|
|
|
},
|
|
|
|
};
|
2017-03-25 02:35:54 +00:00
|
|
|
}
|
2017-04-10 08:26:30 +00:00
|
|
|
const parts = createStyleElement.parts;
|
2017-04-14 18:30:55 +00:00
|
|
|
parts.checker.checked = style.enabled;
|
2017-04-10 08:26:30 +00:00
|
|
|
parts.nameLink.textContent = style.name;
|
|
|
|
parts.nameLink.href = parts.editLink.href = parts.editHrefBase + style.id;
|
|
|
|
parts.homepage.href = parts.homepage.title = style.url || '';
|
|
|
|
|
2017-04-14 18:30:55 +00:00
|
|
|
const entry = parts.entry.cloneNode(true);
|
|
|
|
entry.id = 'style-' + style.id;
|
|
|
|
entry.styleId = style.id;
|
|
|
|
entry.styleNameLowerCase = name || style.name.toLocaleLowerCase();
|
2017-04-26 10:56:32 +00:00
|
|
|
entry.styleMeta = getStyleWithNoCode(style);
|
2017-04-14 18:30:55 +00:00
|
|
|
entry.className = parts.entryClassBase + ' ' +
|
|
|
|
(style.enabled ? 'enabled' : 'disabled') +
|
|
|
|
(style.updateUrl ? ' updatable' : '');
|
|
|
|
|
2017-04-22 12:09:48 +00:00
|
|
|
if (style.url) {
|
|
|
|
$('.homepage', entry).appendChild(parts.homepageIcon.cloneNode(true));
|
|
|
|
}
|
|
|
|
if (style.updateUrl && newUI.enabled) {
|
|
|
|
$('.actions', entry).appendChild(template.updaterIcons.cloneNode(true));
|
|
|
|
}
|
|
|
|
|
2017-04-14 18:30:55 +00:00
|
|
|
// name being supplied signifies we're invoked by showStyles()
|
|
|
|
// which debounces its main loop thus loading the postponed favicons
|
|
|
|
createStyleTargetsElement({entry, style, postponeFavicons: name});
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function createStyleTargetsElement({entry, style, postponeFavicons}) {
|
|
|
|
const parts = createStyleElement.parts;
|
2017-04-10 08:26:30 +00:00
|
|
|
const targets = parts.targets.cloneNode(true);
|
|
|
|
let container = targets;
|
2017-04-05 13:14:59 +00:00
|
|
|
let numTargets = 0;
|
2017-04-09 04:53:16 +00:00
|
|
|
let numIcons = 0;
|
2017-04-10 08:26:30 +00:00
|
|
|
const displayed = new Set();
|
2017-04-05 13:14:59 +00:00
|
|
|
for (const type of TARGET_TYPES) {
|
2017-03-26 02:30:59 +00:00
|
|
|
for (const section of style.sections) {
|
2017-04-05 13:14:59 +00:00
|
|
|
for (const targetValue of section[type] || []) {
|
|
|
|
if (displayed.has(targetValue)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
displayed.add(targetValue);
|
|
|
|
const element = template.appliesToTarget.cloneNode(true);
|
2017-04-09 04:53:16 +00:00
|
|
|
if (!newUI.enabled) {
|
|
|
|
if (numTargets == 10) {
|
2017-04-10 08:26:30 +00:00
|
|
|
container = container.appendChild(template.extraAppliesTo.cloneNode(true));
|
2017-04-09 04:53:16 +00:00
|
|
|
} else if (numTargets > 1) {
|
|
|
|
container.appendChild(template.appliesToSeparator.cloneNode(true));
|
|
|
|
}
|
|
|
|
} else if (newUI.favicons) {
|
2017-04-05 13:14:59 +00:00
|
|
|
let favicon = '';
|
|
|
|
if (type == 'domains') {
|
2017-04-13 08:03:45 +00:00
|
|
|
favicon = GET_FAVICON_URL + targetValue;
|
2017-04-05 13:14:59 +00:00
|
|
|
} else if (targetValue.startsWith('chrome-extension:')) {
|
|
|
|
favicon = OWN_ICON;
|
|
|
|
} else if (type != 'regexps') {
|
2017-04-13 08:03:45 +00:00
|
|
|
favicon = targetValue.includes('://') && targetValue.match(/^.*?:\/\/([^/]+)/);
|
|
|
|
favicon = favicon ? GET_FAVICON_URL + favicon[1] : '';
|
2017-04-05 13:14:59 +00:00
|
|
|
}
|
|
|
|
if (favicon) {
|
|
|
|
element.appendChild(document.createElement('img')).dataset.src = favicon;
|
2017-04-09 04:53:16 +00:00
|
|
|
numIcons++;
|
2017-04-05 13:14:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
element.appendChild(
|
|
|
|
document.createTextNode(
|
2017-04-10 08:26:30 +00:00
|
|
|
(parts.decorations[type + 'Before'] || '') +
|
2017-04-05 13:14:59 +00:00
|
|
|
targetValue +
|
2017-04-10 08:26:30 +00:00
|
|
|
(parts.decorations[type + 'After'] || '')));
|
2017-04-05 13:14:59 +00:00
|
|
|
container.appendChild(element);
|
2017-04-09 04:53:16 +00:00
|
|
|
numTargets++;
|
2017-03-25 02:35:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-05 13:14:59 +00:00
|
|
|
if (newUI.enabled) {
|
2017-04-14 18:30:55 +00:00
|
|
|
if (numTargets > newUI.targets) {
|
|
|
|
$('.applies-to', entry).classList.add('has-more');
|
|
|
|
}
|
|
|
|
if (numIcons && !postponeFavicons) {
|
2017-04-09 04:53:16 +00:00
|
|
|
debounce(handleEvent.loadFavicons);
|
|
|
|
}
|
2017-04-05 13:14:59 +00:00
|
|
|
}
|
2017-04-14 18:30:55 +00:00
|
|
|
const entryTargets = $('.targets', entry);
|
2017-04-10 08:26:30 +00:00
|
|
|
if (numTargets) {
|
2017-04-14 18:30:55 +00:00
|
|
|
entryTargets.parentElement.replaceChild(targets, entryTargets);
|
2017-04-10 08:26:30 +00:00
|
|
|
} else {
|
2017-04-14 18:30:55 +00:00
|
|
|
entryTargets.appendChild(template.appliesToEverything.cloneNode(true));
|
2017-04-10 08:26:30 +00:00
|
|
|
}
|
2017-04-14 18:30:55 +00:00
|
|
|
entry.classList.toggle('global', !numTargets);
|
2015-01-30 17:31:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
Object.assign(handleEvent, {
|
|
|
|
|
2017-04-05 13:14:59 +00:00
|
|
|
ENTRY_ROUTES: {
|
|
|
|
'.checker, .enable, .disable': 'toggle',
|
|
|
|
'.style-name-link': 'edit',
|
|
|
|
'.homepage': 'external',
|
|
|
|
'.check-update': 'check',
|
|
|
|
'.update': 'update',
|
|
|
|
'.delete': 'delete',
|
|
|
|
'.applies-to .expander': 'expandTargets',
|
|
|
|
},
|
|
|
|
|
|
|
|
entryClicked(event) {
|
|
|
|
const target = event.target;
|
|
|
|
const entry = target.closest('.entry');
|
|
|
|
for (const selector in handleEvent.ENTRY_ROUTES) {
|
|
|
|
for (let el = target; el && el != entry; el = el.parentElement) {
|
|
|
|
if (el.matches(selector)) {
|
|
|
|
const handler = handleEvent.ENTRY_ROUTES[selector];
|
|
|
|
return handleEvent[handler].call(el, event, entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
edit(event) {
|
2017-03-21 01:32:38 +00:00
|
|
|
if (event.altKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2017-03-26 02:30:59 +00:00
|
|
|
const left = event.button == 0;
|
|
|
|
const middle = event.button == 1;
|
|
|
|
const shift = event.shiftKey;
|
|
|
|
const ctrl = event.ctrlKey;
|
2017-03-21 01:32:38 +00:00
|
|
|
const openWindow = left && shift && !ctrl;
|
|
|
|
const openBackgroundTab = (middle && !shift) || (left && ctrl && !shift);
|
|
|
|
const openForegroundTab = (middle && shift) || (left && ctrl && shift);
|
|
|
|
const url = event.target.closest('[href]').href;
|
|
|
|
if (openWindow || openBackgroundTab || openForegroundTab) {
|
|
|
|
if (openWindow) {
|
|
|
|
chrome.windows.create(Object.assign(prefs.get('windowPosition'), {url}));
|
|
|
|
} else {
|
|
|
|
openURL({url, active: openForegroundTab});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rememberScrollPosition();
|
|
|
|
getActiveTab().then(tab => {
|
|
|
|
sessionStorageHash('manageStylesHistory').set(tab.id, url);
|
|
|
|
location.href = url;
|
|
|
|
});
|
|
|
|
}
|
2017-04-03 04:13:10 +00:00
|
|
|
},
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-05 13:14:59 +00:00
|
|
|
toggle(event, entry) {
|
2017-04-11 10:51:40 +00:00
|
|
|
saveStyleSafe({
|
|
|
|
id: entry.styleId,
|
|
|
|
enabled: this.matches('.enable') || this.checked,
|
|
|
|
});
|
2017-04-03 04:13:10 +00:00
|
|
|
},
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-05 13:14:59 +00:00
|
|
|
check(event, entry) {
|
|
|
|
checkUpdate(entry);
|
2017-04-03 04:13:10 +00:00
|
|
|
},
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-05 13:14:59 +00:00
|
|
|
update(event, entry) {
|
2017-03-21 01:32:38 +00:00
|
|
|
// update everything but name
|
2017-04-11 10:51:40 +00:00
|
|
|
saveStyleSafe(Object.assign(entry.updatedCode, {
|
2017-04-05 13:14:59 +00:00
|
|
|
id: entry.styleId,
|
2017-03-25 02:35:54 +00:00
|
|
|
name: null,
|
|
|
|
reason: 'update',
|
|
|
|
}));
|
2017-04-03 04:13:10 +00:00
|
|
|
},
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-05 13:14:59 +00:00
|
|
|
delete(event, entry) {
|
|
|
|
const id = entry.styleId;
|
2017-04-11 10:51:40 +00:00
|
|
|
const {name} = BG.cachedStyles.byId.get(id) || {};
|
2017-04-05 13:14:59 +00:00
|
|
|
animateElement(entry, {className: 'highlight'});
|
2017-03-25 19:50:37 +00:00
|
|
|
messageBox({
|
|
|
|
title: t('deleteStyleConfirm'),
|
|
|
|
contents: name,
|
|
|
|
className: 'danger center',
|
|
|
|
buttons: [t('confirmDelete'), t('confirmCancel')],
|
|
|
|
})
|
|
|
|
.then(({button, enter, esc}) => {
|
|
|
|
if (button == 0 || enter) {
|
2017-04-11 10:51:40 +00:00
|
|
|
deleteStyleSafe({id});
|
2017-03-25 19:50:37 +00:00
|
|
|
}
|
|
|
|
});
|
2017-04-03 04:13:10 +00:00
|
|
|
},
|
2017-03-31 22:49:51 +00:00
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
external(event) {
|
2017-03-31 22:49:51 +00:00
|
|
|
openURL({url: event.target.closest('a').href});
|
|
|
|
event.preventDefault();
|
2017-04-03 04:13:10 +00:00
|
|
|
},
|
2017-04-05 13:14:59 +00:00
|
|
|
|
|
|
|
expandTargets() {
|
|
|
|
this.closest('.applies-to').classList.toggle('expanded');
|
|
|
|
},
|
|
|
|
|
2017-04-16 11:24:49 +00:00
|
|
|
loadFavicons(container = document.body) {
|
2017-05-02 14:13:42 +00:00
|
|
|
for (const img of $$('img', container)) {
|
2017-04-13 08:03:45 +00:00
|
|
|
if (img.dataset.src) {
|
|
|
|
img.src = img.dataset.src;
|
|
|
|
delete img.dataset.src;
|
2017-04-05 13:14:59 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-16 11:24:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
filterOnChange({target: el, forceRefilter}) {
|
|
|
|
const getValue = el => (el.type == 'checkbox' ? el.checked : el.value.trim());
|
|
|
|
if (!forceRefilter) {
|
|
|
|
const value = getValue(el);
|
|
|
|
if (value == el.lastValue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
el.lastValue = value;
|
|
|
|
}
|
|
|
|
const enabledFilters = $$('#header [data-filter]').filter(el => getValue(el));
|
2017-04-24 13:29:48 +00:00
|
|
|
const buildFilter = hide =>
|
2017-04-26 15:35:00 +00:00
|
|
|
(hide ? '' : '.entry.hidden') +
|
2017-04-24 13:29:48 +00:00
|
|
|
[...enabledFilters.map(el =>
|
|
|
|
el.dataset[hide ? 'filterHide' : 'filter']
|
|
|
|
.split(/,\s*/)
|
2017-04-26 15:35:00 +00:00
|
|
|
.map(s => (hide ? '.entry:not(.hidden)' : '') + s)
|
|
|
|
.join(','))
|
|
|
|
].join(hide ? ',' : '');
|
2017-04-16 11:24:49 +00:00
|
|
|
Object.assign(filtersSelector, {
|
2017-04-24 13:29:48 +00:00
|
|
|
hide: buildFilter(true),
|
|
|
|
unhide: buildFilter(false),
|
2017-04-16 11:24:49 +00:00
|
|
|
});
|
|
|
|
reapplyFilter();
|
|
|
|
},
|
2017-04-03 04:13:10 +00:00
|
|
|
});
|
2015-01-30 17:31:20 +00:00
|
|
|
|
|
|
|
|
2017-04-26 10:56:32 +00:00
|
|
|
function handleUpdate(style, {reason, method} = {}) {
|
|
|
|
let entry;
|
|
|
|
let oldEntry = $('#style-' + style.id);
|
|
|
|
if (oldEntry && method == 'styleUpdated') {
|
2017-04-26 15:35:00 +00:00
|
|
|
handleToggledOrCodeOnly();
|
2017-04-26 10:56:32 +00:00
|
|
|
}
|
|
|
|
entry = entry || createStyleElement({style});
|
2017-04-16 11:24:49 +00:00
|
|
|
if (oldEntry) {
|
|
|
|
if (oldEntry.styleNameLowerCase == entry.styleNameLowerCase) {
|
|
|
|
installed.replaceChild(entry, oldEntry);
|
2017-03-26 08:43:45 +00:00
|
|
|
} else {
|
2017-04-16 11:24:49 +00:00
|
|
|
oldEntry.remove();
|
2017-03-26 08:43:45 +00:00
|
|
|
}
|
2017-04-26 10:56:32 +00:00
|
|
|
}
|
|
|
|
if (reason == 'update' && entry.matches('.updatable')) {
|
|
|
|
handleUpdateInstalled();
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
2017-04-16 11:24:49 +00:00
|
|
|
filterAndAppend({entry});
|
2017-04-26 10:56:32 +00:00
|
|
|
if (!entry.matches('.hidden') && reason != 'import') {
|
2017-04-16 11:24:49 +00:00
|
|
|
animateElement(entry, {className: 'highlight'});
|
|
|
|
scrollElementIntoView(entry);
|
2017-04-05 13:14:59 +00:00
|
|
|
}
|
2017-04-26 10:56:32 +00:00
|
|
|
|
2017-04-26 15:35:00 +00:00
|
|
|
function handleToggledOrCodeOnly() {
|
2017-04-26 10:56:32 +00:00
|
|
|
const newStyleMeta = getStyleWithNoCode(style);
|
|
|
|
const diff = objectDiff(oldEntry.styleMeta, newStyleMeta);
|
|
|
|
if (diff.length == 0) {
|
|
|
|
// only code was modified
|
|
|
|
entry = oldEntry;
|
|
|
|
oldEntry = null;
|
|
|
|
}
|
|
|
|
if (diff.length == 1 && diff[0].key == 'enabled') {
|
|
|
|
oldEntry.classList.toggle('enabled', style.enabled);
|
|
|
|
oldEntry.classList.toggle('disabled', !style.enabled);
|
|
|
|
$$('.checker', oldEntry).forEach(el => (el.checked = style.enabled));
|
|
|
|
oldEntry.styleMeta = newStyleMeta;
|
|
|
|
entry = oldEntry;
|
|
|
|
oldEntry = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleUpdateInstalled() {
|
|
|
|
entry.classList.add('update-done');
|
|
|
|
entry.classList.remove('can-update', 'updatable');
|
|
|
|
$('.update-note', entry).textContent = t('updateCompleted');
|
|
|
|
renderUpdatesOnlyFilter();
|
|
|
|
}
|
2015-01-30 17:31:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2015-01-30 17:31:20 +00:00
|
|
|
function handleDelete(id) {
|
2017-04-16 11:24:49 +00:00
|
|
|
const node = $('#style-' + id);
|
2017-03-21 01:32:38 +00:00
|
|
|
if (node) {
|
|
|
|
node.remove();
|
2017-04-16 11:24:49 +00:00
|
|
|
if (node.matches('.can-update')) {
|
|
|
|
const btnApply = $('#apply-all-updates');
|
|
|
|
btnApply.dataset.value = Number(btnApply.dataset.value) - 1;
|
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
2015-01-30 17:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-05 13:14:59 +00:00
|
|
|
function switchUI({styleOnly} = {}) {
|
2017-04-18 18:49:04 +00:00
|
|
|
const current = {};
|
|
|
|
const changed = {};
|
|
|
|
let someChanged = false;
|
|
|
|
// ensure the global option is processed first
|
|
|
|
for (const el of [$('#manage.newUI'), ...$$('[id^="manage.newUI."]')]) {
|
|
|
|
const id = el.id.replace(/^manage\.newUI\.?/, '') || 'enabled';
|
|
|
|
const value = el.type == 'checkbox' ? el.checked : Number(el.value);
|
|
|
|
const valueChanged = value !== newUI[id] && (id == 'enabled' || current.enabled);
|
|
|
|
current[id] = value;
|
|
|
|
changed[id] = valueChanged;
|
|
|
|
someChanged |= valueChanged;
|
|
|
|
}
|
2017-04-05 13:14:59 +00:00
|
|
|
|
2017-04-18 18:49:04 +00:00
|
|
|
if (!styleOnly && !someChanged) {
|
2017-04-05 13:14:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-18 18:49:04 +00:00
|
|
|
Object.assign(newUI, current);
|
2017-04-08 11:03:54 +00:00
|
|
|
newUI.renderClass();
|
2017-04-18 18:49:04 +00:00
|
|
|
installed.classList.toggle('has-favicons', newUI.favicons);
|
2017-04-05 13:14:59 +00:00
|
|
|
$('#style-overrides').textContent = `
|
|
|
|
.newUI .targets {
|
|
|
|
max-height: ${newUI.targets * 18}px;
|
|
|
|
}
|
2017-04-18 18:49:04 +00:00
|
|
|
` + (newUI.faviconsGray ? `
|
|
|
|
.newUI .target img {
|
|
|
|
-webkit-filter: grayscale(1);
|
|
|
|
filter: grayscale(1);
|
|
|
|
opacity: .25;
|
|
|
|
}
|
|
|
|
` : `
|
|
|
|
.newUI .target img {
|
|
|
|
-webkit-filter: none;
|
|
|
|
filter: none;
|
|
|
|
opacity: 1;
|
|
|
|
}
|
|
|
|
`);
|
2017-04-05 13:14:59 +00:00
|
|
|
|
2017-04-14 18:30:55 +00:00
|
|
|
if (styleOnly) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-18 18:49:04 +00:00
|
|
|
const missingFavicons = newUI.enabled && newUI.favicons && !$('.applies-to img');
|
|
|
|
if (changed.enabled || (missingFavicons && !createStyleElement.parts)) {
|
2017-04-05 13:14:59 +00:00
|
|
|
installed.innerHTML = '';
|
|
|
|
getStylesSafe().then(showStyles);
|
2017-04-14 18:30:55 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-18 18:49:04 +00:00
|
|
|
if (changed.targets) {
|
2017-04-10 08:26:30 +00:00
|
|
|
for (const targets of $$('.entry .targets')) {
|
|
|
|
const hasMore = targets.children.length > newUI.targets;
|
|
|
|
targets.parentElement.classList.toggle('has-more', hasMore);
|
|
|
|
}
|
2017-04-14 18:30:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (missingFavicons) {
|
|
|
|
getStylesSafe().then(styles => {
|
|
|
|
for (const style of styles) {
|
|
|
|
const entry = $('#style-' + style.id);
|
|
|
|
if (entry) {
|
|
|
|
createStyleTargetsElement({entry, style, postponeFavicons: true});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debounce(handleEvent.loadFavicons);
|
|
|
|
});
|
|
|
|
return;
|
2017-04-05 13:14:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-15 04:21:08 +00:00
|
|
|
function applyUpdateAll() {
|
2017-03-21 01:32:38 +00:00
|
|
|
const btnApply = $('#apply-all-updates');
|
|
|
|
btnApply.disabled = true;
|
|
|
|
setTimeout(() => {
|
2017-04-14 20:25:54 +00:00
|
|
|
btnApply.classList.add('hidden');
|
2017-03-21 01:32:38 +00:00
|
|
|
btnApply.disabled = false;
|
2017-04-18 11:59:59 +00:00
|
|
|
renderUpdatesOnlyFilter({show: false});
|
2017-03-21 01:32:38 +00:00
|
|
|
}, 1000);
|
|
|
|
|
2017-03-25 05:54:58 +00:00
|
|
|
$$('.can-update .update').forEach(button => {
|
2017-04-14 20:25:54 +00:00
|
|
|
scrollElementIntoView(button);
|
2017-03-25 05:54:58 +00:00
|
|
|
button.click();
|
|
|
|
});
|
2015-03-15 04:21:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2015-01-30 17:31:20 +00:00
|
|
|
function checkUpdateAll() {
|
2017-04-26 10:56:32 +00:00
|
|
|
document.body.classList.add('update-in-progress');
|
2017-04-21 12:33:28 +00:00
|
|
|
$('#check-all-updates').disabled = true;
|
2017-04-25 11:17:37 +00:00
|
|
|
$('#check-all-updates-force').classList.add('hidden');
|
2017-04-21 12:33:28 +00:00
|
|
|
$('#apply-all-updates').classList.add('hidden');
|
|
|
|
$('#update-all-no-updates').classList.add('hidden');
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-26 10:56:32 +00:00
|
|
|
const ignoreDigest = this && this.id == 'check-all-updates-force';
|
|
|
|
$$('.updatable:not(.can-update)' + (ignoreDigest ? '' : ':not(.update-problem)'))
|
|
|
|
.map(el => checkUpdate(el, {single: false}));
|
|
|
|
|
2017-04-21 12:33:28 +00:00
|
|
|
let total = 0;
|
2017-04-14 20:25:54 +00:00
|
|
|
let checked = 0;
|
2017-04-24 13:29:48 +00:00
|
|
|
let skippedEdited = 0;
|
2017-04-21 12:33:28 +00:00
|
|
|
let updated = 0;
|
|
|
|
|
2017-04-26 10:56:32 +00:00
|
|
|
BG.updater.checkAllStyles({observer, save: false, ignoreDigest}).then(done);
|
2017-04-21 12:33:28 +00:00
|
|
|
|
2017-04-24 13:29:48 +00:00
|
|
|
function observer(state, value, details) {
|
2017-04-21 12:33:28 +00:00
|
|
|
switch (state) {
|
|
|
|
case BG.updater.COUNT:
|
|
|
|
total = value;
|
|
|
|
break;
|
|
|
|
case BG.updater.UPDATED:
|
|
|
|
if (++updated == 1) {
|
|
|
|
$('#apply-all-updates').disabled = true;
|
|
|
|
$('#apply-all-updates').classList.remove('hidden');
|
|
|
|
}
|
|
|
|
$('#apply-all-updates').dataset.value = updated;
|
|
|
|
// fallthrough
|
|
|
|
case BG.updater.SKIPPED:
|
|
|
|
checked++;
|
2017-04-24 13:29:48 +00:00
|
|
|
if (details == BG.updater.EDITED || details == BG.updater.MAYBE_EDITED) {
|
|
|
|
skippedEdited++;
|
|
|
|
}
|
2017-04-21 12:33:28 +00:00
|
|
|
reportUpdateState(state, value, details);
|
|
|
|
break;
|
2017-04-14 20:25:54 +00:00
|
|
|
}
|
2017-04-21 12:33:28 +00:00
|
|
|
const progress = $('#update-progress');
|
|
|
|
const maxWidth = progress.parentElement.clientWidth;
|
|
|
|
progress.style.width = Math.round(checked / total * maxWidth) + 'px';
|
|
|
|
}
|
2017-04-26 10:56:32 +00:00
|
|
|
|
|
|
|
function done() {
|
|
|
|
document.body.classList.remove('update-in-progress');
|
|
|
|
$('#check-all-updates').disabled = total == 0;
|
|
|
|
$('#apply-all-updates').disabled = false;
|
|
|
|
renderUpdatesOnlyFilter({check: updated + skippedEdited > 0});
|
|
|
|
if (!updated) {
|
|
|
|
$('#update-all-no-updates').dataset.skippedEdited = skippedEdited > 0;
|
|
|
|
$('#update-all-no-updates').classList.remove('hidden');
|
|
|
|
$('#check-all-updates-force').classList.toggle('hidden', skippedEdited == 0);
|
|
|
|
}
|
|
|
|
}
|
2015-01-30 17:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-24 13:29:48 +00:00
|
|
|
function checkUpdate(entry, {single = true} = {}) {
|
|
|
|
$('.update-note', entry).textContent = t('checkingForUpdate');
|
|
|
|
$('.check-update', entry).title = '';
|
2017-04-21 12:33:28 +00:00
|
|
|
if (single) {
|
2017-04-24 13:29:48 +00:00
|
|
|
BG.updater.checkStyle({
|
|
|
|
save: false,
|
|
|
|
ignoreDigest: entry.classList.contains('update-problem'),
|
|
|
|
style: BG.cachedStyles.byId.get(entry.styleId),
|
|
|
|
observer: reportUpdateState,
|
|
|
|
});
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
2017-04-24 13:29:48 +00:00
|
|
|
entry.classList.remove('checking-update', 'no-update', 'update-problem');
|
|
|
|
entry.classList.add('checking-update');
|
2017-04-21 12:33:28 +00:00
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
|
|
|
|
|
2017-04-21 12:33:28 +00:00
|
|
|
function reportUpdateState(state, style, details) {
|
|
|
|
const entry = $('#style-' + style.id);
|
|
|
|
entry.classList.remove('checking-update');
|
|
|
|
switch (state) {
|
|
|
|
case BG.updater.UPDATED:
|
|
|
|
entry.classList.add('can-update');
|
|
|
|
entry.updatedCode = style;
|
|
|
|
$('.update-note', entry).textContent = '';
|
2017-04-09 16:04:00 +00:00
|
|
|
$('#onlyUpdates').classList.remove('hidden');
|
2017-04-21 12:33:28 +00:00
|
|
|
break;
|
|
|
|
case BG.updater.SKIPPED: {
|
2017-04-24 13:29:48 +00:00
|
|
|
if (entry.classList.contains('can-update')) {
|
|
|
|
break;
|
|
|
|
}
|
2017-04-25 11:17:37 +00:00
|
|
|
const same = details == BG.updater.SAME_MD5 || details == BG.updater.SAME_CODE;
|
|
|
|
const edited = details == BG.updater.EDITED || details == BG.updater.MAYBE_EDITED;
|
|
|
|
entry.dataset.details = details;
|
2017-04-21 12:33:28 +00:00
|
|
|
if (!details) {
|
|
|
|
details = t('updateCheckFailServerUnreachable');
|
|
|
|
} else if (typeof details == 'number') {
|
|
|
|
details = t('updateCheckFailBadResponseCode', [details]);
|
2017-04-24 13:29:48 +00:00
|
|
|
} else if (details == BG.updater.EDITED) {
|
2017-04-23 12:19:18 +00:00
|
|
|
details = t('updateCheckSkippedLocallyEdited') + '\n' + t('updateCheckManualUpdateHint');
|
2017-04-24 13:29:48 +00:00
|
|
|
} else if (details == BG.updater.MAYBE_EDITED) {
|
2017-04-23 12:19:18 +00:00
|
|
|
details = t('updateCheckSkippedMaybeLocallyEdited') + '\n' + t('updateCheckManualUpdateHint');
|
2017-04-05 13:14:59 +00:00
|
|
|
}
|
2017-04-21 12:33:28 +00:00
|
|
|
const message = same ? t('updateCheckSucceededNoUpdate') : details;
|
|
|
|
entry.classList.add('no-update');
|
|
|
|
entry.classList.toggle('update-problem', !same);
|
|
|
|
$('.update-note', entry).textContent = message;
|
|
|
|
$('.check-update', entry).title = newUI.enabled ? message : '';
|
2017-04-25 11:17:37 +00:00
|
|
|
$('.update', entry).title = t(edited ? 'updateCheckManualUpdateForce' : 'installUpdate');
|
2017-04-26 10:56:32 +00:00
|
|
|
if (!document.body.classList.contains('update-in-progress')) {
|
2017-04-21 12:33:28 +00:00
|
|
|
// this is a single update job so we can decide whether to hide the filter
|
2017-04-24 13:29:48 +00:00
|
|
|
renderUpdatesOnlyFilter({show: $('.can-update, .update-problem')});
|
2017-04-09 16:04:00 +00:00
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
2017-04-21 12:33:28 +00:00
|
|
|
}
|
|
|
|
if (filtersSelector.hide) {
|
|
|
|
filterAndAppend({entry});
|
2017-03-25 02:35:54 +00:00
|
|
|
}
|
2015-01-30 17:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-09 16:04:00 +00:00
|
|
|
function renderUpdatesOnlyFilter({show, check} = {}) {
|
|
|
|
const numUpdatable = $$('.can-update').length;
|
2017-04-24 13:29:48 +00:00
|
|
|
const mightUpdate = numUpdatable > 0 || $('.update-problem');
|
2017-04-09 16:04:00 +00:00
|
|
|
const checkbox = $('#onlyUpdates input');
|
2017-04-24 13:29:48 +00:00
|
|
|
show = show !== undefined ? show : mightUpdate;
|
|
|
|
check = check !== undefined ? show && check : checkbox.checked && mightUpdate;
|
2017-04-09 16:04:00 +00:00
|
|
|
|
|
|
|
$('#onlyUpdates').classList.toggle('hidden', !show);
|
|
|
|
checkbox.checked = check;
|
|
|
|
checkbox.dispatchEvent(new Event('change'));
|
|
|
|
|
|
|
|
const btnApply = $('#apply-all-updates');
|
|
|
|
if (!btnApply.matches('.hidden')) {
|
2017-04-24 13:29:48 +00:00
|
|
|
if (numUpdatable > 0) {
|
2017-04-14 20:25:54 +00:00
|
|
|
btnApply.dataset.value = numUpdatable;
|
2017-04-09 16:04:00 +00:00
|
|
|
} else {
|
|
|
|
btnApply.classList.add('hidden');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-26 20:55:54 +00:00
|
|
|
function showUpdateHistory() {
|
|
|
|
BG.chromeLocal.getValue('updateLog').then((lines = []) => {
|
|
|
|
messageBox({
|
|
|
|
title: t('updateCheckHistory'),
|
|
|
|
contents: $element({
|
|
|
|
className: 'update-history-log',
|
|
|
|
textContent: lines.join('\n'),
|
|
|
|
}),
|
|
|
|
buttons: [t('confirmOK')],
|
|
|
|
onshow: () => ($('#message-box-contents').scrollTop = 1e9),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
function searchStyles({immediately, container}) {
|
2017-04-16 11:24:49 +00:00
|
|
|
const searchElement = $('#search');
|
|
|
|
const query = searchElement.value.toLocaleLowerCase();
|
|
|
|
const queryPrev = searchElement.lastValue || '';
|
|
|
|
if (query == queryPrev && !immediately && !container) {
|
2017-03-21 01:32:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!immediately) {
|
2017-06-27 12:17:52 +00:00
|
|
|
debounce(searchStyles, 150, {immediately: true});
|
2017-03-21 01:32:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-16 11:24:49 +00:00
|
|
|
searchElement.lastValue = query;
|
|
|
|
|
|
|
|
const searchInVisible = queryPrev && query.includes(queryPrev);
|
|
|
|
const entries = container && container.children || container ||
|
|
|
|
(searchInVisible ? $$('.entry:not(.hidden)') : installed.children);
|
|
|
|
let needsRefilter = false;
|
|
|
|
for (const entry of entries) {
|
|
|
|
let isMatching = !query;
|
|
|
|
if (!isMatching) {
|
|
|
|
const style = BG.cachedStyles.byId.get(entry.styleId) || {};
|
|
|
|
isMatching = Boolean(style && (
|
|
|
|
isMatchingText(style.name) ||
|
|
|
|
style.url && isMatchingText(style.url) ||
|
|
|
|
isMatchingStyle(style)));
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
2017-04-16 11:24:49 +00:00
|
|
|
if (entry.classList.contains('not-matching') != !isMatching) {
|
|
|
|
entry.classList.toggle('not-matching', !isMatching);
|
|
|
|
needsRefilter = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (needsRefilter && !container) {
|
|
|
|
handleEvent.filterOnChange({forceRefilter: true});
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
2017-04-16 11:24:49 +00:00
|
|
|
return;
|
2017-03-21 01:32:38 +00:00
|
|
|
|
|
|
|
function isMatchingStyle(style) {
|
2017-03-26 02:30:59 +00:00
|
|
|
for (const section of style.sections) {
|
|
|
|
for (const prop in section) {
|
2017-03-21 01:32:38 +00:00
|
|
|
const value = section[prop];
|
|
|
|
switch (typeof value) {
|
|
|
|
case 'string':
|
|
|
|
if (isMatchingText(value)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'object':
|
2017-03-26 02:30:59 +00:00
|
|
|
for (const str of value) {
|
2017-03-21 01:32:38 +00:00
|
|
|
if (isMatchingText(str)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function isMatchingText(text) {
|
|
|
|
return text.toLocaleLowerCase().indexOf(query) >= 0;
|
|
|
|
}
|
2015-01-30 17:31:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-16 11:24:49 +00:00
|
|
|
function filterAndAppend({entry, container}) {
|
|
|
|
if (!container) {
|
2017-04-26 10:56:32 +00:00
|
|
|
container = [entry];
|
2017-04-16 11:24:49 +00:00
|
|
|
// reverse the visibility, otherwise reapplyFilter will see no need to work
|
|
|
|
if (!filtersSelector.hide || !entry.matches(filtersSelector.hide)) {
|
|
|
|
entry.classList.add('hidden');
|
|
|
|
}
|
2017-06-04 09:22:51 +00:00
|
|
|
} else if ($('#search').value.trim()) {
|
2017-04-16 11:24:49 +00:00
|
|
|
searchStyles({immediately: true, container});
|
|
|
|
}
|
|
|
|
reapplyFilter(container);
|
2015-03-04 07:55:35 +00:00
|
|
|
}
|
2017-03-26 08:43:45 +00:00
|
|
|
|
|
|
|
|
2017-04-16 11:24:49 +00:00
|
|
|
function reapplyFilter(container = installed) {
|
|
|
|
// A: show
|
2017-06-04 09:22:51 +00:00
|
|
|
let toHide = [];
|
|
|
|
let toUnhide = [];
|
|
|
|
if (filtersSelector.hide) {
|
|
|
|
filterContainer({hide: false});
|
|
|
|
} else {
|
|
|
|
toUnhide = container;
|
|
|
|
}
|
2017-04-16 11:24:49 +00:00
|
|
|
// showStyles() is building the page and no filters are active
|
|
|
|
if (toUnhide instanceof DocumentFragment) {
|
|
|
|
installed.appendChild(toUnhide);
|
|
|
|
return;
|
2017-04-26 15:35:00 +00:00
|
|
|
} else if (toUnhide.length && $('#search').value.trim()) {
|
|
|
|
searchStyles({immediately: true, container: toUnhide});
|
2017-06-04 09:22:51 +00:00
|
|
|
filterContainer({hide: false});
|
2017-03-26 08:43:45 +00:00
|
|
|
}
|
2017-04-16 11:24:49 +00:00
|
|
|
// filtering needed or a single-element job from handleUpdate()
|
|
|
|
const entries = installed.children;
|
|
|
|
const numEntries = entries.length;
|
|
|
|
let numVisible = numEntries - $$('.entry.hidden').length;
|
|
|
|
for (const entry of toUnhide.children || toUnhide) {
|
|
|
|
const next = findInsertionPoint(entry);
|
|
|
|
if (entry.nextElementSibling !== next) {
|
|
|
|
installed.insertBefore(entry, next);
|
|
|
|
}
|
|
|
|
if (entry.classList.contains('hidden')) {
|
|
|
|
entry.classList.remove('hidden');
|
|
|
|
numVisible++;
|
|
|
|
}
|
2017-03-26 08:43:45 +00:00
|
|
|
}
|
2017-04-16 11:24:49 +00:00
|
|
|
// B: hide
|
2017-06-04 09:22:51 +00:00
|
|
|
if (filtersSelector.hide) {
|
|
|
|
filterContainer({hide: true});
|
|
|
|
}
|
2017-04-16 11:24:49 +00:00
|
|
|
if (!toHide.length) {
|
|
|
|
return;
|
2017-03-26 08:43:45 +00:00
|
|
|
}
|
2017-04-16 11:24:49 +00:00
|
|
|
for (const entry of toHide) {
|
|
|
|
entry.classList.add('hidden');
|
|
|
|
}
|
|
|
|
// showStyles() is building the page with filters active so we need to:
|
|
|
|
// 1. add all hidden entries to the end
|
|
|
|
// 2. add the visible entries before the first hidden entry
|
|
|
|
if (container instanceof DocumentFragment) {
|
|
|
|
for (const entry of toHide) {
|
|
|
|
installed.appendChild(entry);
|
2017-03-26 08:43:45 +00:00
|
|
|
}
|
2017-04-26 15:35:00 +00:00
|
|
|
installed.insertBefore(container, $('.entry.hidden'));
|
2017-04-16 11:24:49 +00:00
|
|
|
return;
|
2017-03-26 08:43:45 +00:00
|
|
|
}
|
2017-04-16 11:24:49 +00:00
|
|
|
// normal filtering of the page or a single-element job from handleUpdate()
|
|
|
|
// we need to keep the visible entries together at the start
|
|
|
|
// first pass only moves one hidden entry in hidden groups with odd number of items
|
|
|
|
shuffle(false);
|
|
|
|
setTimeout(shuffle, 0, true);
|
|
|
|
// single-element job from handleEvent(): add the last wraith
|
|
|
|
if (toHide.length == 1 && toHide[0].parentElement != installed) {
|
|
|
|
installed.appendChild(toHide[0]);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
2017-04-26 10:56:32 +00:00
|
|
|
function filterContainer({hide}) {
|
|
|
|
const selector = filtersSelector[hide ? 'hide' : 'unhide'];
|
|
|
|
if (container.filter) {
|
2017-04-26 15:35:00 +00:00
|
|
|
if (hide) {
|
|
|
|
// already filtered in previous invocation
|
2017-06-04 09:22:51 +00:00
|
|
|
return;
|
2017-04-26 15:35:00 +00:00
|
|
|
}
|
|
|
|
for (const el of container) {
|
|
|
|
(el.matches(selector) ? toUnhide : toHide).push(el);
|
|
|
|
}
|
2017-06-04 09:22:51 +00:00
|
|
|
return;
|
|
|
|
} else if (hide) {
|
|
|
|
toHide = $$(selector, container);
|
2017-04-26 10:56:32 +00:00
|
|
|
} else {
|
2017-06-04 09:22:51 +00:00
|
|
|
toUnhide = $$(selector, container);
|
2017-04-26 10:56:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-16 11:24:49 +00:00
|
|
|
function shuffle(fullPass) {
|
2017-04-26 10:56:32 +00:00
|
|
|
if (fullPass && !document.body.classList.contains('update-in-progress')) {
|
2017-04-18 14:14:29 +00:00
|
|
|
$('#check-all-updates').disabled = !$('.updatable:not(.can-update)');
|
|
|
|
}
|
2017-04-16 11:24:49 +00:00
|
|
|
// 1. skip the visible group on top
|
|
|
|
let firstHidden = $('#installed > .hidden');
|
|
|
|
let entry = firstHidden;
|
|
|
|
let i = [...entries].indexOf(entry);
|
|
|
|
let horizon = entries[numVisible];
|
|
|
|
const skipGroup = state => {
|
|
|
|
const start = i;
|
|
|
|
const first = entry;
|
|
|
|
while (entry && entry.classList.contains('hidden') == state) {
|
|
|
|
entry = entry.nextElementSibling;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return {first, start, len: i - start};
|
|
|
|
};
|
|
|
|
let prevGroup = i ? {first: entries[0], start: 0, len: i} : skipGroup(true);
|
|
|
|
// eslint-disable-next-line no-unmodified-loop-condition
|
|
|
|
while (entry) {
|
|
|
|
// 2a. find the next hidden group's start and end
|
|
|
|
// 2b. find the next visible group's start and end
|
|
|
|
const isHidden = entry.classList.contains('hidden');
|
|
|
|
const group = skipGroup(isHidden);
|
|
|
|
const hidden = isHidden ? group : prevGroup;
|
|
|
|
const visible = isHidden ? prevGroup : group;
|
|
|
|
// 3. move the shortest group; repeat 2-3
|
|
|
|
if (hidden.len < visible.len && (fullPass || hidden.len % 2)) {
|
|
|
|
// 3a. move hidden under the horizon
|
|
|
|
for (let j = 0; j < (fullPass ? hidden.len : 1); j++) {
|
|
|
|
const entry = entries[hidden.start];
|
|
|
|
installed.insertBefore(entry, horizon);
|
|
|
|
horizon = entry;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
prevGroup = isHidden ? skipGroup(false) : group;
|
|
|
|
firstHidden = entry;
|
|
|
|
} else if (isHidden || !fullPass) {
|
|
|
|
prevGroup = group;
|
|
|
|
} else {
|
|
|
|
// 3b. move visible above the horizon
|
|
|
|
for (let j = 0; j < visible.len; j++) {
|
|
|
|
const entry = entries[visible.start + j];
|
|
|
|
installed.insertBefore(entry, firstHidden);
|
|
|
|
}
|
|
|
|
prevGroup = {
|
|
|
|
first: firstHidden,
|
|
|
|
start: hidden.start + visible.len,
|
|
|
|
len: hidden.len + skipGroup(true).len,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2017-03-26 08:43:45 +00:00
|
|
|
}
|
2017-04-16 11:24:49 +00:00
|
|
|
|
|
|
|
function findInsertionPoint(entry) {
|
|
|
|
const nameLLC = entry.styleNameLowerCase;
|
|
|
|
let a = 0;
|
|
|
|
let b = Math.min(numEntries, numVisible) - 1;
|
|
|
|
if (b < 0) {
|
|
|
|
return entries[numVisible];
|
|
|
|
}
|
|
|
|
if (entries[0].styleNameLowerCase > nameLLC) {
|
|
|
|
return entries[0];
|
|
|
|
}
|
|
|
|
if (entries[b].styleNameLowerCase <= nameLLC) {
|
|
|
|
return entries[numVisible];
|
|
|
|
}
|
|
|
|
// bisect
|
|
|
|
while (a < b - 1) {
|
|
|
|
const c = (a + b) / 2 | 0;
|
|
|
|
if (nameLLC < entries[c].styleNameLowerCase) {
|
|
|
|
b = c;
|
|
|
|
} else {
|
|
|
|
a = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (entries[a].styleNameLowerCase > nameLLC) {
|
|
|
|
return entries[a];
|
|
|
|
}
|
|
|
|
while (a <= b && entries[a].styleNameLowerCase < nameLLC) {
|
|
|
|
a++;
|
|
|
|
}
|
|
|
|
return entries[entries[a].styleNameLowerCase <= nameLLC ? a + 1 : a];
|
2017-03-26 08:43:45 +00:00
|
|
|
}
|
2017-04-16 11:24:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function rememberScrollPosition() {
|
|
|
|
history.replaceState({scrollY: window.scrollY}, document.title);
|
2017-03-26 08:43:45 +00:00
|
|
|
}
|
2017-04-26 10:56:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
function objectDiff(first, second, path = '') {
|
|
|
|
const diff = [];
|
|
|
|
for (const key in first) {
|
|
|
|
const a = first[key];
|
|
|
|
const b = second[key];
|
|
|
|
if (a === b) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (b === undefined) {
|
|
|
|
diff.push({path, key, values: [a], type: 'removed'});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (a && typeof a.filter == 'function' && b && typeof b.filter == 'function') {
|
|
|
|
if (a.length != b.length
|
|
|
|
|| a.some((el, i) => !el || typeof el != 'object' ? el != b[i]
|
|
|
|
: objectDiff(el, b[i], path + key + '[' + i + '].').length)
|
|
|
|
) {
|
|
|
|
diff.push({path, key, values: [a, b], type: 'changed'});
|
|
|
|
}
|
|
|
|
} else if (typeof a == 'object' && typeof b == 'object') {
|
|
|
|
diff.push(...objectDiff(a, b, path + key + '.'));
|
|
|
|
} else {
|
|
|
|
diff.push({path, key, values: [a, b], type: 'changed'});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const key in second) {
|
|
|
|
if (!(key in first)) {
|
|
|
|
diff.push({path, key, values: [second[key]], type: 'added'});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return diff;
|
|
|
|
}
|