2017-03-26 02:30:59 +00:00
|
|
|
/* global messageBox */
|
|
|
|
'use strict';
|
2015-01-30 17:31:20 +00:00
|
|
|
|
2017-04-08 11:03:54 +00:00
|
|
|
let installed;
|
|
|
|
|
2017-04-05 13:14:59 +00:00
|
|
|
const newUI = {
|
|
|
|
enabled: prefs.get('manage.newUI'),
|
|
|
|
favicons: prefs.get('manage.newUI.favicons'),
|
|
|
|
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-05 13:14:59 +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-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;
|
|
|
|
$('#apply-all-updates').onclick = applyUpdateAll;
|
|
|
|
$('#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 => {
|
|
|
|
if (event.keyCode == 47
|
|
|
|
&& !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-05 13:14:59 +00:00
|
|
|
for (const [className, checkbox] of [
|
|
|
|
['enabled-only', $('#manage.onlyEnabled')],
|
|
|
|
['edited-only', $('#manage.onlyEdited')],
|
2017-04-09 16:04:00 +00:00
|
|
|
['updates-only', $('#onlyUpdates input')],
|
2017-04-05 13:14:59 +00:00
|
|
|
]) {
|
|
|
|
// will be triggered by setupLivePrefs immediately
|
|
|
|
checkbox.onchange = () => installed.classList.toggle(className, checkbox.checked);
|
|
|
|
}
|
|
|
|
|
|
|
|
enforceInputRange($('#manage.newUI.favicons'));
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
setupLivePrefs([
|
|
|
|
'manage.onlyEnabled',
|
|
|
|
'manage.onlyEdited',
|
2017-04-05 13:14:59 +00:00
|
|
|
'manage.newUI',
|
|
|
|
'manage.newUI.favicons',
|
|
|
|
'manage.newUI.targets',
|
2017-03-21 01:32:38 +00:00
|
|
|
]);
|
|
|
|
|
2017-04-05 13:14:59 +00:00
|
|
|
$$('[id^="manage.newUI"]')
|
|
|
|
.forEach(el => (el.oninput = (el.onchange = switchUI)));
|
|
|
|
|
|
|
|
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-03 04:24:50 +00:00
|
|
|
const shouldRenderAll = (history.state || {}).scrollY > window.innerHeight;
|
2017-03-21 01:32:38 +00:00
|
|
|
const renderBin = document.createDocumentFragment();
|
|
|
|
renderStyles(0);
|
2017-04-10 08:26:30 +00:00
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
function renderStyles(index) {
|
|
|
|
const t0 = performance.now();
|
2017-03-26 02:30:59 +00:00
|
|
|
while (index < sorted.length) {
|
2017-03-26 08:43:45 +00:00
|
|
|
renderBin.appendChild(createStyleElement(sorted[index++]));
|
2017-03-26 02:30:59 +00:00
|
|
|
if (!shouldRenderAll && performance.now() - t0 > 10) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-25 02:35:54 +00:00
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
if ($('#search').value) {
|
|
|
|
// re-apply filtering on history Back
|
2017-03-26 02:30:59 +00:00
|
|
|
searchStyles({immediately: true, container: renderBin});
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
|
|
|
installed.appendChild(renderBin);
|
|
|
|
if (index < sorted.length) {
|
|
|
|
setTimeout(renderStyles, 0, index);
|
2017-04-03 04:13:10 +00:00
|
|
|
} else if (shouldRenderAll && 'scrollY' in (history.state || {})) {
|
2017-03-21 01:32:38 +00:00
|
|
|
setTimeout(() => scrollTo(0, history.state.scrollY));
|
|
|
|
}
|
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) {
|
|
|
|
const entry = template[`style${newUI.enabled ? 'Compact' : ''}`].cloneNode(true);
|
|
|
|
createStyleElement.parts = {
|
|
|
|
newUI: newUI.enabled,
|
|
|
|
entry,
|
|
|
|
entryClassBase: entry.className,
|
|
|
|
checker: $('.checker', entry),
|
|
|
|
nameLink: $('.style-name-link', entry),
|
|
|
|
editLink: $('.style-edit-link', entry) || {},
|
|
|
|
editHrefBase: $('.style-name-link, .style-edit-link', entry).getAttribute('href'),
|
|
|
|
homepage: $('.homepage', entry),
|
|
|
|
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;
|
|
|
|
Object.assign(parts.entry, {
|
|
|
|
className: parts.entryClassBase + ' ' +
|
|
|
|
(style.enabled ? 'enabled' : 'disabled') +
|
|
|
|
(style.updateUrl ? ' updatable' : ''),
|
|
|
|
id: 'style-' + style.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
parts.nameLink.textContent = style.name;
|
|
|
|
parts.nameLink.href = parts.editLink.href = parts.editHrefBase + style.id;
|
|
|
|
parts.homepage.href = parts.homepage.title = style.url || '';
|
|
|
|
|
|
|
|
// .targets may be a large list so we clone it separately
|
|
|
|
// and paste into the cloned entry in the end
|
|
|
|
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') {
|
|
|
|
favicon = GET_FAVICON_URL + targetValue;
|
|
|
|
} else if (targetValue.startsWith('chrome-extension:')) {
|
|
|
|
favicon = OWN_ICON;
|
|
|
|
} else if (type != 'regexps') {
|
2017-04-09 04:53:16 +00:00
|
|
|
favicon = targetValue.includes('://') && targetValue.match(/^.*?:\/\/([^/]+)/);
|
2017-04-05 13:14:59 +00:00
|
|
|
favicon = favicon ? GET_FAVICON_URL + favicon[1] : '';
|
|
|
|
}
|
|
|
|
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-03-21 01:32:38 +00:00
|
|
|
|
2017-04-05 13:14:59 +00:00
|
|
|
if (newUI.enabled) {
|
2017-04-10 08:26:30 +00:00
|
|
|
parts.checker.checked = style.enabled;
|
|
|
|
parts.appliesTo.classList.toggle('has-more', numTargets > newUI.targets);
|
|
|
|
// name is supplied by showStyles so we let it decide when to load the icons
|
|
|
|
if (numIcons && !name) {
|
2017-04-09 04:53:16 +00:00
|
|
|
debounce(handleEvent.loadFavicons);
|
|
|
|
}
|
2017-04-05 13:14:59 +00:00
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-10 08:26:30 +00:00
|
|
|
const newEntry = parts.entry.cloneNode(true);
|
2017-04-11 10:51:40 +00:00
|
|
|
newEntry.styleId = style.id;
|
|
|
|
newEntry.styleNameLowerCase = name || style.name.toLocaleLowerCase();
|
2017-04-10 08:26:30 +00:00
|
|
|
const newTargets = $('.targets', newEntry);
|
|
|
|
if (numTargets) {
|
|
|
|
newTargets.parentElement.replaceChild(targets, newTargets);
|
|
|
|
} else {
|
|
|
|
newTargets.appendChild(template.appliesToEverything.cloneNode(true));
|
|
|
|
newEntry.classList.add('global');
|
|
|
|
}
|
|
|
|
return newEntry;
|
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');
|
|
|
|
},
|
|
|
|
|
|
|
|
loadFavicons(container = installed) {
|
|
|
|
for (const img of container.getElementsByTagName('img')) {
|
|
|
|
if (img.dataset.src) {
|
|
|
|
img.src = img.dataset.src;
|
|
|
|
delete img.dataset.src;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-03 04:13:10 +00:00
|
|
|
});
|
2015-01-30 17:31:20 +00:00
|
|
|
|
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
function handleUpdate(style, {reason} = {}) {
|
2017-03-26 08:43:45 +00:00
|
|
|
const element = createStyleElement({style});
|
2017-04-01 08:41:33 +00:00
|
|
|
const oldElement = $('#style-' + style.id, installed);
|
2017-03-26 08:43:45 +00:00
|
|
|
if (oldElement) {
|
|
|
|
if (oldElement.styleNameLowerCase == element.styleNameLowerCase) {
|
|
|
|
installed.replaceChild(element, oldElement);
|
|
|
|
} else {
|
|
|
|
oldElement.remove();
|
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
if (reason == 'update') {
|
|
|
|
element.classList.add('update-done');
|
2017-04-09 11:28:10 +00:00
|
|
|
element.classList.remove('can-update', 'updatable');
|
2017-03-21 01:32:38 +00:00
|
|
|
$('.update-note', element).innerHTML = t('updateCompleted');
|
2017-04-09 16:04:00 +00:00
|
|
|
renderUpdatesOnlyFilter();
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-26 08:43:45 +00:00
|
|
|
installed.insertBefore(element, findNextElement(style));
|
2017-04-11 05:17:37 +00:00
|
|
|
if (reason != 'import') {
|
2017-04-05 13:14:59 +00:00
|
|
|
animateElement(element, {className: 'highlight'});
|
2017-04-11 10:51:40 +00:00
|
|
|
scrollElementIntoView(element);
|
2017-04-05 13:14:59 +00:00
|
|
|
}
|
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-01 08:41:33 +00:00
|
|
|
const node = $('#style-' + id, installed);
|
2017-03-21 01:32:38 +00:00
|
|
|
if (node) {
|
|
|
|
node.remove();
|
|
|
|
}
|
2015-01-30 17:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-05 13:14:59 +00:00
|
|
|
function switchUI({styleOnly} = {}) {
|
|
|
|
const enabled = $('#manage.newUI').checked;
|
|
|
|
const favicons = $('#manage.newUI.favicons').checked;
|
|
|
|
const targets = Number($('#manage.newUI.targets').value);
|
|
|
|
|
|
|
|
const stateToggled = newUI.enabled != enabled;
|
|
|
|
const targetsChanged = enabled && targets != newUI.targets;
|
|
|
|
const faviconsChanged = enabled && favicons != newUI.favicons;
|
|
|
|
const missingFavicons = enabled && favicons && !$('.applies-to img');
|
|
|
|
|
|
|
|
if (!styleOnly && !stateToggled && !targetsChanged && !faviconsChanged) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.assign(newUI, {enabled, favicons, targets});
|
|
|
|
|
2017-04-08 11:03:54 +00:00
|
|
|
newUI.renderClass();
|
2017-04-05 13:14:59 +00:00
|
|
|
installed.classList.toggle('has-favicons', favicons);
|
|
|
|
$('#style-overrides').textContent = `
|
|
|
|
.newUI .targets {
|
|
|
|
max-height: ${newUI.targets * 18}px;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
if (!styleOnly && (stateToggled || missingFavicons)) {
|
|
|
|
installed.innerHTML = '';
|
|
|
|
getStylesSafe().then(showStyles);
|
2017-04-10 08:26:30 +00:00
|
|
|
} else if (targetsChanged) {
|
|
|
|
for (const targets of $$('.entry .targets')) {
|
|
|
|
const hasMore = targets.children.length > newUI.targets;
|
|
|
|
targets.parentElement.classList.toggle('has-more', hasMore);
|
|
|
|
}
|
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(() => {
|
|
|
|
btnApply.style.display = 'none';
|
|
|
|
btnApply.disabled = false;
|
|
|
|
}, 1000);
|
|
|
|
|
2017-03-25 05:54:58 +00:00
|
|
|
$$('.can-update .update').forEach(button => {
|
|
|
|
// align to the bottom of the visible area if wasn't visible
|
|
|
|
button.scrollIntoView(false);
|
|
|
|
button.click();
|
|
|
|
});
|
2017-04-09 16:04:00 +00:00
|
|
|
|
|
|
|
renderUpdatesOnlyFilter({show: false});
|
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-03-21 01:32:38 +00:00
|
|
|
const btnCheck = $('#check-all-updates');
|
|
|
|
const btnApply = $('#apply-all-updates');
|
|
|
|
const noUpdates = $('#update-all-no-updates');
|
|
|
|
|
|
|
|
btnCheck.disabled = true;
|
|
|
|
btnApply.classList.add('hidden');
|
|
|
|
noUpdates.classList.add('hidden');
|
|
|
|
|
2017-04-09 16:04:00 +00:00
|
|
|
Promise.all($$('.updatable:not(.can-update)').map(checkUpdate))
|
2017-03-21 01:32:38 +00:00
|
|
|
.then(updatables => {
|
|
|
|
btnCheck.disabled = false;
|
2017-04-05 13:14:59 +00:00
|
|
|
const numUpdatable = updatables.filter(u => u).length;
|
|
|
|
if (numUpdatable) {
|
2017-03-21 01:32:38 +00:00
|
|
|
btnApply.classList.remove('hidden');
|
2017-04-05 13:14:59 +00:00
|
|
|
btnApply.originalLabel = btnApply.originalLabel || btnApply.textContent;
|
|
|
|
btnApply.textContent = btnApply.originalLabel + ` (${numUpdatable})`;
|
2017-04-09 16:04:00 +00:00
|
|
|
renderUpdatesOnlyFilter({check: true});
|
2017-03-21 01:32:38 +00:00
|
|
|
} else {
|
|
|
|
noUpdates.classList.remove('hidden');
|
|
|
|
setTimeout(() => {
|
|
|
|
noUpdates.classList.add('hidden');
|
|
|
|
}, 10e3);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// notify the automatic updater to reset the next automatic update accordingly
|
|
|
|
chrome.runtime.sendMessage({
|
|
|
|
method: 'resetInterval'
|
|
|
|
});
|
2015-01-30 17:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
function checkUpdate(element) {
|
|
|
|
$('.update-note', element).innerHTML = t('checkingForUpdate');
|
2017-04-05 13:14:59 +00:00
|
|
|
$('.check-update', element).title = '';
|
2017-04-09 16:04:00 +00:00
|
|
|
element.classList.remove('checking-update', 'no-update', 'update-problem');
|
2017-03-21 01:32:38 +00:00
|
|
|
element.classList.add('checking-update');
|
2017-04-03 04:13:10 +00:00
|
|
|
return new Updater(element).run(); // eslint-disable-line no-use-before-define
|
2015-01-30 17:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
class Updater {
|
|
|
|
constructor(element) {
|
2017-04-11 10:51:40 +00:00
|
|
|
const style = BG.cachedStyles.byId.get(element.styleId);
|
2017-03-21 01:32:38 +00:00
|
|
|
Object.assign(this, {
|
|
|
|
element,
|
2017-04-05 13:14:59 +00:00
|
|
|
id: style.id,
|
|
|
|
url: style.updateUrl,
|
|
|
|
md5Url: style.md5Url,
|
|
|
|
md5: style.originalMd5,
|
2017-03-21 01:32:38 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
run() {
|
|
|
|
return this.md5Url && this.md5
|
|
|
|
? this.checkMd5()
|
|
|
|
: this.checkFullCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
checkMd5() {
|
2017-03-26 02:30:59 +00:00
|
|
|
return Updater.download(this.md5Url).then(
|
|
|
|
md5 => (md5.length == 32
|
2017-03-21 01:32:38 +00:00
|
|
|
? this.decideOnMd5(md5 != this.md5)
|
2017-03-26 02:30:59 +00:00
|
|
|
: this.onFailure(-1)),
|
2017-04-05 13:14:59 +00:00
|
|
|
status => this.onFailure(status));
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
decideOnMd5(md5changed) {
|
|
|
|
if (md5changed) {
|
|
|
|
return this.checkFullCode({forceUpdate: true});
|
|
|
|
}
|
|
|
|
this.display();
|
|
|
|
}
|
|
|
|
|
|
|
|
checkFullCode({forceUpdate = false} = {}) {
|
2017-03-26 02:30:59 +00:00
|
|
|
return Updater.download(this.url).then(
|
2017-03-21 01:32:38 +00:00
|
|
|
text => this.handleJson(forceUpdate, JSON.parse(text)),
|
2017-04-05 13:14:59 +00:00
|
|
|
status => this.onFailure(status));
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleJson(forceUpdate, json) {
|
|
|
|
return getStylesSafe({id: this.id}).then(([style]) => {
|
2017-04-11 10:51:40 +00:00
|
|
|
const needsUpdate = forceUpdate || !BG.styleSectionsEqual(style, json);
|
2017-03-21 01:32:38 +00:00
|
|
|
this.display({json: needsUpdate && json});
|
|
|
|
return needsUpdate;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onFailure(status) {
|
|
|
|
this.display({
|
|
|
|
message: status == 0
|
|
|
|
? t('updateCheckFailServerUnreachable')
|
|
|
|
: t('updateCheckFailBadResponseCode', [status]),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
display({json, message} = {}) {
|
|
|
|
// json on success
|
|
|
|
// message on failure
|
|
|
|
// none on update not needed
|
|
|
|
this.element.classList.remove('checking-update');
|
|
|
|
if (json) {
|
|
|
|
this.element.classList.add('can-update');
|
|
|
|
this.element.updatedCode = json;
|
|
|
|
$('.update-note', this.element).innerHTML = '';
|
2017-04-09 16:04:00 +00:00
|
|
|
$('#onlyUpdates').classList.remove('hidden');
|
2017-03-21 01:32:38 +00:00
|
|
|
} else {
|
|
|
|
this.element.classList.add('no-update');
|
2017-04-05 13:14:59 +00:00
|
|
|
this.element.classList.toggle('update-problem', Boolean(message));
|
2017-03-21 01:32:38 +00:00
|
|
|
$('.update-note', this.element).innerHTML = message || t('updateCheckSucceededNoUpdate');
|
2017-04-05 13:14:59 +00:00
|
|
|
if (newUI.enabled) {
|
|
|
|
$('.check-update', this.element).title = message;
|
|
|
|
}
|
2017-04-09 16:04:00 +00:00
|
|
|
// don't hide if check-all is running
|
|
|
|
if (!$('#check-all-updates').disabled) {
|
|
|
|
$('#onlyUpdates').classList.toggle('hidden', !$('.can-update'));
|
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
2017-03-25 02:35:54 +00:00
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
static download(url) {
|
2017-03-21 01:32:38 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const xhr = new XMLHttpRequest();
|
2017-03-26 02:30:59 +00:00
|
|
|
xhr.onloadend = () => (xhr.status == 200
|
2017-03-21 01:32:38 +00:00
|
|
|
? resolve(xhr.responseText)
|
2017-03-26 02:30:59 +00:00
|
|
|
: reject(xhr.status));
|
2017-03-21 01:32:38 +00:00
|
|
|
if (url.length > 2000) {
|
|
|
|
const [mainUrl, query] = url.split('?');
|
|
|
|
xhr.open('POST', mainUrl, true);
|
|
|
|
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
|
|
|
|
xhr.send(query);
|
|
|
|
} else {
|
|
|
|
xhr.open('GET', url);
|
|
|
|
xhr.send();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
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;
|
|
|
|
const canUpdate = numUpdatable > 0;
|
|
|
|
const checkbox = $('#onlyUpdates input');
|
|
|
|
show = show !== undefined ? show : canUpdate;
|
|
|
|
check = check !== undefined ? show && check : checkbox.checked && canUpdate;
|
|
|
|
|
|
|
|
$('#onlyUpdates').classList.toggle('hidden', !show);
|
|
|
|
checkbox.checked = check;
|
|
|
|
checkbox.dispatchEvent(new Event('change'));
|
|
|
|
|
|
|
|
const btnApply = $('#apply-all-updates');
|
|
|
|
if (!btnApply.matches('.hidden')) {
|
|
|
|
if (canUpdate) {
|
|
|
|
btnApply.textContent = btnApply.originalLabel + ` (${numUpdatable})`;
|
|
|
|
} else {
|
|
|
|
btnApply.classList.add('hidden');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
function searchStyles({immediately, container}) {
|
2017-03-21 01:32:38 +00:00
|
|
|
const query = $('#search').value.toLocaleLowerCase();
|
2017-03-26 08:57:44 +00:00
|
|
|
if (query == (searchStyles.lastQuery || '') && !immediately && !container) {
|
2017-03-21 01:32:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
searchStyles.lastQuery = query;
|
|
|
|
if (!immediately) {
|
|
|
|
clearTimeout(searchStyles.timeout);
|
2017-03-26 08:57:44 +00:00
|
|
|
searchStyles.timeout = setTimeout(searchStyles, 150, {immediately: true});
|
2017-03-21 01:32:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
for (const element of (container || installed).children) {
|
2017-04-11 10:51:40 +00:00
|
|
|
const style = BG.cachedStyles.byId.get(element.styleId) || {};
|
2017-03-21 01:32:38 +00:00
|
|
|
if (style) {
|
2017-03-26 08:57:44 +00:00
|
|
|
const isMatching = !query
|
|
|
|
|| isMatchingText(style.name)
|
|
|
|
|| style.url && isMatchingText(style.url)
|
|
|
|
|| isMatchingStyle(style);
|
2017-03-21 01:32:38 +00:00
|
|
|
element.style.display = isMatching ? '' : 'none';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
function rememberScrollPosition() {
|
2017-04-03 04:24:50 +00:00
|
|
|
history.replaceState({scrollY: window.scrollY}, document.title);
|
2015-03-04 07:55:35 +00:00
|
|
|
}
|
2017-03-26 08:43:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
function findNextElement(style) {
|
|
|
|
const nameLLC = style.name.toLocaleLowerCase();
|
|
|
|
const elements = installed.children;
|
|
|
|
let a = 0;
|
|
|
|
let b = elements.length - 1;
|
|
|
|
if (b < 0) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (elements[0].styleNameLowerCase > nameLLC) {
|
|
|
|
return elements[0];
|
|
|
|
}
|
|
|
|
if (elements[b].styleNameLowerCase <= nameLLC) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
// bisect
|
|
|
|
while (a < b - 1) {
|
|
|
|
const c = (a + b) / 2 | 0;
|
|
|
|
if (nameLLC < elements[c].styleNameLowerCase) {
|
|
|
|
b = c;
|
|
|
|
} else {
|
|
|
|
a = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (elements[a].styleNameLowerCase > nameLLC) {
|
|
|
|
return elements[a];
|
|
|
|
}
|
|
|
|
while (a <= b && elements[a].name < nameLLC) {
|
|
|
|
a++;
|
|
|
|
}
|
|
|
|
return elements[elements[a].styleNameLowerCase <= nameLLC ? a + 1 : a];
|
|
|
|
}
|