2017-03-26 02:30:59 +00:00
|
|
|
/* global messageBox */
|
|
|
|
'use strict';
|
2015-01-30 17:31:20 +00:00
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
const installed = $('#installed');
|
|
|
|
const TARGET_LABEL = t('appliesDisplay', '').trim();
|
|
|
|
const TARGET_TYPES = ['domains', 'urls', 'urlPrefixes', 'regexps'];
|
|
|
|
const TARGET_LIMIT = 10;
|
2017-04-03 04:13:10 +00:00
|
|
|
const handleEvent = {};
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-03-30 23:18:41 +00:00
|
|
|
getStylesSafe()
|
2017-03-21 01:32:38 +00:00
|
|
|
.then(showStyles)
|
|
|
|
.then(initGlobalEvents);
|
|
|
|
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
chrome.runtime.onMessage.addListener(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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-01-30 17:31:20 +00:00
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
function initGlobalEvents() {
|
|
|
|
$('#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();
|
|
|
|
$('#manage-shortcuts-button').onclick = configureCommands.open;
|
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-03-26 02:30:59 +00:00
|
|
|
document.addEventListener('visibilitychange', () => {
|
2017-03-21 01:32:38 +00:00
|
|
|
if (document.visibilityState != 'visible') {
|
|
|
|
rememberScrollPosition();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
setupLivePrefs([
|
|
|
|
'manage.onlyEnabled',
|
|
|
|
'manage.onlyEdited',
|
|
|
|
]);
|
|
|
|
|
|
|
|
[
|
|
|
|
['enabled-only', $('#manage.onlyEnabled')],
|
|
|
|
['edited-only', $('#manage.onlyEdited')],
|
|
|
|
]
|
|
|
|
.forEach(([className, checkbox]) => {
|
|
|
|
checkbox.onchange = () => installed.classList.toggle(className, checkbox.checked);
|
|
|
|
checkbox.onchange();
|
|
|
|
});
|
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-03-21 01:32:38 +00:00
|
|
|
const shouldRenderAll = history.state && history.state.scrollY > innerHeight;
|
|
|
|
const renderBin = document.createDocumentFragment();
|
2017-03-23 17:49:23 +00:00
|
|
|
tDocLoader.stop();
|
2017-03-21 01:32:38 +00:00
|
|
|
renderStyles(0);
|
|
|
|
// TODO: remember how many styles fit one page to display just that portion first next time
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
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-03-21 01:32:38 +00:00
|
|
|
const entry = template.style.cloneNode(true);
|
|
|
|
entry.classList.add(style.enabled ? 'enabled' : 'disabled');
|
|
|
|
entry.setAttribute('style-id', style.id);
|
2017-04-01 08:41:33 +00:00
|
|
|
entry.id = 'style-' + style.id;
|
2017-03-21 01:32:38 +00:00
|
|
|
entry.styleId = style.id;
|
2017-03-26 08:43:45 +00:00
|
|
|
entry.styleNameLowerCase = name || style.name.toLocaleLowerCase();
|
2017-03-21 01:32:38 +00:00
|
|
|
if (style.updateUrl) {
|
|
|
|
entry.setAttribute('style-update-url', style.updateUrl);
|
|
|
|
}
|
|
|
|
if (style.md5Url) {
|
|
|
|
entry.setAttribute('style-md5-url', style.md5Url);
|
|
|
|
}
|
|
|
|
if (style.originalMd5) {
|
|
|
|
entry.setAttribute('style-original-md5', style.originalMd5);
|
|
|
|
}
|
|
|
|
|
|
|
|
const styleName = $('.style-name', entry);
|
|
|
|
const styleNameEditLink = $('a', styleName);
|
|
|
|
styleNameEditLink.appendChild(document.createTextNode(style.name));
|
|
|
|
styleNameEditLink.href = styleNameEditLink.getAttribute('href') + style.id;
|
2017-04-03 04:13:10 +00:00
|
|
|
styleNameEditLink.onclick = handleEvent.edit;
|
2017-03-21 01:32:38 +00:00
|
|
|
if (style.url) {
|
|
|
|
const homepage = template.styleHomepage.cloneNode(true);
|
|
|
|
homepage.href = style.url;
|
2017-04-03 04:13:10 +00:00
|
|
|
homepage.onclick = handleEvent.external;
|
2017-03-21 01:32:38 +00:00
|
|
|
styleName.appendChild(document.createTextNode(' '));
|
2017-03-25 02:35:54 +00:00
|
|
|
styleName.appendChild(homepage);
|
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
|
|
|
|
const targets = new Map(TARGET_TYPES.map(t => [t, new Set()]));
|
|
|
|
const decorations = {
|
|
|
|
urlPrefixesAfter: '*',
|
|
|
|
regexpsBefore: '/',
|
|
|
|
regexpsAfter: '/',
|
|
|
|
};
|
2017-03-26 02:30:59 +00:00
|
|
|
for (const [name, target] of targets.entries()) {
|
|
|
|
for (const section of style.sections) {
|
|
|
|
for (const targetValue of section[name] || []) {
|
2017-03-21 01:32:38 +00:00
|
|
|
target.add(
|
|
|
|
(decorations[name + 'Before'] || '') +
|
|
|
|
targetValue.trim() +
|
|
|
|
(decorations[name + 'After'] || ''));
|
2017-03-25 02:35:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
const appliesTo = $('.applies-to', entry);
|
|
|
|
appliesTo.firstElementChild.textContent = TARGET_LABEL;
|
|
|
|
const targetsList = Array.prototype.concat.apply([],
|
|
|
|
[...targets.values()].map(set => [...set.values()]));
|
|
|
|
if (!targetsList.length) {
|
|
|
|
appliesTo.appendChild(template.appliesToEverything.cloneNode(true));
|
|
|
|
entry.classList.add('global');
|
|
|
|
} else {
|
|
|
|
let index = 0;
|
|
|
|
let container = appliesTo;
|
2017-03-26 02:30:59 +00:00
|
|
|
for (const target of targetsList) {
|
2017-03-21 01:32:38 +00:00
|
|
|
if (index > 0) {
|
|
|
|
container.appendChild(template.appliesToSeparator.cloneNode(true));
|
2017-03-25 02:35:54 +00:00
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
if (++index == TARGET_LIMIT) {
|
|
|
|
container = appliesTo.appendChild(template.extraAppliesTo.cloneNode(true));
|
2017-03-25 02:35:54 +00:00
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
const item = template.appliesToTarget.cloneNode(true);
|
|
|
|
item.textContent = target;
|
|
|
|
container.appendChild(item);
|
|
|
|
}
|
2017-03-25 02:35:54 +00:00
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-03-25 02:35:54 +00:00
|
|
|
const editLink = $('.style-edit-link', entry);
|
|
|
|
editLink.href = editLink.getAttribute('href') + style.id;
|
2017-04-03 04:13:10 +00:00
|
|
|
editLink.onclick = handleEvent.edit;
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
$('.enable', entry).onclick = handleEvent.toggle;
|
|
|
|
$('.disable', entry).onclick = handleEvent.toggle;
|
|
|
|
$('.check-update', entry).onclick = handleEvent.check;
|
|
|
|
$('.update', entry).onclick = handleEvent.update;
|
|
|
|
$('.delete', entry).onclick = handleEvent.delete;
|
2017-03-21 01:32:38 +00:00
|
|
|
return entry;
|
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, {
|
|
|
|
|
|
|
|
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-03 04:13:10 +00:00
|
|
|
toggle(event) {
|
2017-03-21 01:32:38 +00:00
|
|
|
enableStyle(getClickedStyleId(event), this.matches('.enable'))
|
|
|
|
.then(handleUpdate);
|
2017-04-03 04:13:10 +00:00
|
|
|
},
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
check(event) {
|
2017-03-21 01:32:38 +00:00
|
|
|
checkUpdate(getClickedStyleElement(event));
|
2017-04-03 04:13:10 +00:00
|
|
|
},
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
update(event) {
|
2017-03-26 02:30:59 +00:00
|
|
|
const styleElement = getClickedStyleElement(event);
|
2017-03-21 01:32:38 +00:00
|
|
|
// update everything but name
|
2017-03-26 02:30:59 +00:00
|
|
|
saveStyle(Object.assign(styleElement.updatedCode, {
|
|
|
|
id: styleElement.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-03 04:13:10 +00:00
|
|
|
delete(event) {
|
2017-03-25 19:50:37 +00:00
|
|
|
const styleElement = getClickedStyleElement(event);
|
|
|
|
const id = styleElement.styleId;
|
2017-03-30 23:18:41 +00:00
|
|
|
const {name} = cachedStyles.byId.get(id) || {};
|
2017-03-25 19:50:37 +00:00
|
|
|
animateElement(styleElement, {className: 'highlight'});
|
|
|
|
messageBox({
|
|
|
|
title: t('deleteStyleConfirm'),
|
|
|
|
contents: name,
|
|
|
|
className: 'danger center',
|
|
|
|
buttons: [t('confirmDelete'), t('confirmCancel')],
|
|
|
|
})
|
|
|
|
.then(({button, enter, esc}) => {
|
|
|
|
if (button == 0 || enter) {
|
|
|
|
deleteStyle(id);
|
|
|
|
}
|
|
|
|
});
|
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
|
|
|
},
|
|
|
|
});
|
2015-01-30 17:31:20 +00:00
|
|
|
|
|
|
|
|
2017-03-21 01:32:38 +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');
|
|
|
|
$('.update-note', element).innerHTML = t('updateCompleted');
|
|
|
|
}
|
|
|
|
}
|
2017-03-26 08:43:45 +00:00
|
|
|
installed.insertBefore(element, findNextElement(style));
|
|
|
|
animateElement(element, {className: 'highlight'});
|
2017-03-24 02:51:44 +00:00
|
|
|
scrollElementIntoView(element);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
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-03-25 05:54:58 +00:00
|
|
|
Promise.all($$('[style-update-url]').map(checkUpdate))
|
2017-03-21 01:32:38 +00:00
|
|
|
.then(updatables => {
|
|
|
|
btnCheck.disabled = false;
|
|
|
|
if (updatables.includes(true)) {
|
|
|
|
btnApply.classList.remove('hidden');
|
|
|
|
} 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');
|
|
|
|
element.classList.remove('checking-update', 'no-update', 'can-update');
|
|
|
|
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) {
|
|
|
|
Object.assign(this, {
|
|
|
|
element,
|
2017-04-01 08:41:33 +00:00
|
|
|
id: element.styleId,
|
2017-03-21 01:32:38 +00:00
|
|
|
url: element.getAttribute('style-update-url'),
|
|
|
|
md5Url: element.getAttribute('style-md5-url'),
|
|
|
|
md5: element.getAttribute('style-original-md5'),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-21 01:32:38 +00:00
|
|
|
this.onFailure);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)),
|
|
|
|
this.onFailure);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleJson(forceUpdate, json) {
|
|
|
|
return getStylesSafe({id: this.id}).then(([style]) => {
|
|
|
|
const needsUpdate = forceUpdate || !styleSectionsEqual(style, json);
|
|
|
|
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 = '';
|
|
|
|
} else {
|
|
|
|
this.element.classList.add('no-update');
|
|
|
|
$('.update-note', this.element).innerHTML = message || t('updateCheckSucceededNoUpdate');
|
|
|
|
}
|
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-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-03-30 23:18:41 +00:00
|
|
|
const style = 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() {
|
|
|
|
history.replaceState({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];
|
|
|
|
}
|