2020-03-23 12:25:50 +00:00
|
|
|
/* global messageBox UI handleEvent filtersSelector filterAndAppend
|
2018-11-07 06:09:29 +00:00
|
|
|
sorter $ $$ $create API onDOMready scrollElementIntoView t chromeLocal */
|
2020-03-23 12:25:50 +00:00
|
|
|
/* exported handleUpdateInstalled resetUpdates */
|
2017-08-15 11:40:36 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-01-02 02:10:38 +00:00
|
|
|
let updateTimer;
|
|
|
|
|
2017-08-15 11:40:36 +00:00
|
|
|
onDOMready().then(() => {
|
2019-01-02 02:10:38 +00:00
|
|
|
$('#check-all-updates-force').onclick = checkUpdateBulk;
|
2017-08-15 11:40:36 +00:00
|
|
|
$('#apply-all-updates').onclick = applyUpdateAll;
|
|
|
|
$('#update-history').onclick = showUpdateHistory;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
function applyUpdateAll() {
|
|
|
|
const btnApply = $('#apply-all-updates');
|
|
|
|
btnApply.disabled = true;
|
|
|
|
setTimeout(() => {
|
|
|
|
btnApply.classList.add('hidden');
|
|
|
|
btnApply.disabled = false;
|
|
|
|
renderUpdatesOnlyFilter({show: false});
|
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
$$('.can-update .update').forEach(button => {
|
|
|
|
scrollElementIntoView(button);
|
|
|
|
button.click();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-23 12:25:50 +00:00
|
|
|
function resetUpdates() {
|
|
|
|
$('#check-all-updates-force').classList.add('hidden');
|
|
|
|
$('#apply-all-updates').classList.add('hidden');
|
|
|
|
$('#update-history').classList.add('hidden');
|
|
|
|
const checkbox = $('#only-updates');
|
|
|
|
checkbox.checked = false;
|
|
|
|
checkbox.parentElement.classList.add('hidden');
|
|
|
|
}
|
|
|
|
|
2017-08-15 11:40:36 +00:00
|
|
|
|
2019-01-02 02:10:38 +00:00
|
|
|
function checkUpdateBulk() {
|
|
|
|
clearTimeout(updateTimer);
|
2017-08-15 11:40:36 +00:00
|
|
|
document.body.classList.add('update-in-progress');
|
2018-12-31 05:45:14 +00:00
|
|
|
// const btnCheck = $('#check-all-updates');
|
2018-01-01 17:02:49 +00:00
|
|
|
const btnCheckForce = $('#check-all-updates-force');
|
|
|
|
const btnApply = $('#apply-all-updates');
|
|
|
|
const noUpdates = $('#update-all-no-updates');
|
2019-01-02 02:10:38 +00:00
|
|
|
const styleIds = $$('.entry-filter-toggle:checked').map(el => el.closest('.entry').styleMeta.id);
|
2018-12-31 05:45:14 +00:00
|
|
|
// btnCheck.disabled = true;
|
2018-01-01 17:02:49 +00:00
|
|
|
btnCheckForce.classList.add('hidden');
|
|
|
|
btnApply.classList.add('hidden');
|
|
|
|
noUpdates.classList.add('hidden');
|
2017-08-15 11:40:36 +00:00
|
|
|
const ignoreDigest = this && this.id === 'check-all-updates-force';
|
|
|
|
$$('.updatable:not(.can-update)' + (ignoreDigest ? '' : ':not(.update-problem)'))
|
2018-01-01 17:02:49 +00:00
|
|
|
.map(checkUpdate);
|
2017-08-15 11:40:36 +00:00
|
|
|
|
|
|
|
let total = 0;
|
|
|
|
let checked = 0;
|
|
|
|
let skippedEdited = 0;
|
|
|
|
let updated = 0;
|
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
chrome.runtime.onConnect.addListener(function onConnect(port) {
|
|
|
|
if (port.name !== 'updater') return;
|
|
|
|
port.onMessage.addListener(observer);
|
|
|
|
chrome.runtime.onConnect.removeListener(onConnect);
|
|
|
|
});
|
|
|
|
|
2019-01-02 02:10:38 +00:00
|
|
|
API.updateCheckBulk({
|
2018-01-01 17:02:49 +00:00
|
|
|
save: false,
|
|
|
|
observe: true,
|
|
|
|
ignoreDigest,
|
2019-01-02 02:10:38 +00:00
|
|
|
styleIds,
|
2018-02-16 09:43:22 +00:00
|
|
|
});
|
2017-08-15 11:40:36 +00:00
|
|
|
|
2018-02-16 09:43:22 +00:00
|
|
|
function observer(info, port) {
|
2018-01-01 17:02:49 +00:00
|
|
|
if ('count' in info) {
|
|
|
|
total = info.count;
|
|
|
|
}
|
|
|
|
if (info.updated) {
|
|
|
|
if (++updated === 1) {
|
|
|
|
btnApply.disabled = true;
|
|
|
|
}
|
|
|
|
btnApply.dataset.value = updated;
|
|
|
|
}
|
2018-01-03 17:26:20 +00:00
|
|
|
if (info.updated || 'error' in info) {
|
2018-01-01 17:02:49 +00:00
|
|
|
checked++;
|
2018-01-03 17:26:20 +00:00
|
|
|
skippedEdited += !info.updated && [info.STATES.EDITED, info.STATES.MAYBE_EDITED].includes(info.error);
|
2018-01-01 17:02:49 +00:00
|
|
|
reportUpdateState(info);
|
2017-08-15 11:40:36 +00:00
|
|
|
}
|
|
|
|
const progress = $('#update-progress');
|
|
|
|
const maxWidth = progress.parentElement.clientWidth;
|
|
|
|
progress.style.width = Math.round(checked / total * maxWidth) + 'px';
|
|
|
|
|
2018-02-16 09:49:53 +00:00
|
|
|
if (!info.done) return;
|
2018-02-16 09:43:22 +00:00
|
|
|
|
|
|
|
port.onMessage.removeListener(observer);
|
2017-08-15 11:40:36 +00:00
|
|
|
document.body.classList.remove('update-in-progress');
|
2018-12-31 05:45:14 +00:00
|
|
|
// btnCheck.disabled = total === 0;
|
2018-01-01 17:02:49 +00:00
|
|
|
btnApply.disabled = false;
|
2017-08-15 11:40:36 +00:00
|
|
|
renderUpdatesOnlyFilter({check: updated + skippedEdited > 0});
|
|
|
|
if (!updated) {
|
2019-01-02 02:10:38 +00:00
|
|
|
if (skippedEdited > 0) {
|
|
|
|
noUpdates.dataset.title = t('updateAllCheckSucceededSomeEdited');
|
|
|
|
}
|
2018-01-01 17:02:49 +00:00
|
|
|
noUpdates.classList.remove('hidden');
|
|
|
|
btnCheckForce.classList.toggle('hidden', skippedEdited === 0);
|
2019-01-02 02:10:38 +00:00
|
|
|
updateTimer = setTimeout(() => {
|
|
|
|
noUpdates.classList.add('hidden');
|
|
|
|
noUpdates.dataset.title = '';
|
|
|
|
}, 1e4);
|
2017-08-15 11:40:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
function checkUpdate(entry, {single} = {}) {
|
2018-12-08 14:42:29 +00:00
|
|
|
$('.check-update', entry).dataset.title = t('checkingForUpdate');
|
2017-08-15 11:40:36 +00:00
|
|
|
if (single) {
|
2018-01-01 17:02:49 +00:00
|
|
|
API.updateCheck({
|
2017-08-15 11:40:36 +00:00
|
|
|
save: false,
|
2018-01-01 17:02:49 +00:00
|
|
|
id: entry.styleId,
|
2017-08-15 11:40:36 +00:00
|
|
|
ignoreDigest: entry.classList.contains('update-problem'),
|
2018-01-01 17:02:49 +00:00
|
|
|
}).then(reportUpdateState);
|
2017-08-15 11:40:36 +00:00
|
|
|
}
|
|
|
|
entry.classList.remove('checking-update', 'no-update', 'update-problem');
|
|
|
|
entry.classList.add('checking-update');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
function reportUpdateState({updated, style, error, STATES}) {
|
2018-02-26 19:54:35 +00:00
|
|
|
const isCheckAll = document.body.classList.contains('update-in-progress');
|
2018-11-25 23:35:54 +00:00
|
|
|
const entry = $(UI.ENTRY_ID_PREFIX + style.id);
|
2020-03-23 12:25:50 +00:00
|
|
|
if (!entry) return;
|
2018-01-01 04:45:04 +00:00
|
|
|
const newClasses = new Map([
|
|
|
|
/*
|
|
|
|
When a style is updated/installed, handleUpdateInstalled() clears "updatable"
|
|
|
|
and sets "update-done" class (optionally "install-done").
|
|
|
|
If you don't close the manager and the style is changed remotely,
|
|
|
|
checking for updates would find an update so we need to ensure the entry is "updatable"
|
|
|
|
*/
|
|
|
|
['updatable', true],
|
|
|
|
// falsy = remove
|
|
|
|
['checking-update', 0],
|
|
|
|
['update-done', 0],
|
|
|
|
['install-done', 0],
|
|
|
|
['no-update', 0],
|
|
|
|
['update-problem', 0],
|
|
|
|
]);
|
2018-01-01 17:02:49 +00:00
|
|
|
if (updated) {
|
|
|
|
newClasses.set('can-update', true);
|
|
|
|
entry.updatedCode = style;
|
2020-03-23 12:25:50 +00:00
|
|
|
const onlyUpdates = $('#only-updates');
|
|
|
|
onlyUpdates.parentElement.classList.remove('hidden');
|
|
|
|
onlyUpdates.checked = true;
|
|
|
|
onlyUpdates.change();
|
2018-01-01 17:02:49 +00:00
|
|
|
} else if (!entry.classList.contains('can-update')) {
|
|
|
|
const same = (
|
|
|
|
error === STATES.SAME_MD5 ||
|
|
|
|
error === STATES.SAME_CODE ||
|
|
|
|
error === STATES.SAME_VERSION
|
|
|
|
);
|
|
|
|
const edited = error === STATES.EDITED || error === STATES.MAYBE_EDITED;
|
|
|
|
entry.dataset.error = error;
|
|
|
|
if (!error) {
|
|
|
|
error = t('updateCheckFailServerUnreachable') + '\n' + style.updateUrl;
|
|
|
|
} else if (typeof error === 'number') {
|
|
|
|
error = t('updateCheckFailBadResponseCode', [error]) + '\n' + style.updateUrl;
|
|
|
|
} else if (error === STATES.EDITED) {
|
|
|
|
error = t('updateCheckSkippedLocallyEdited') + '\n' + t('updateCheckManualUpdateHint');
|
|
|
|
} else if (error === STATES.MAYBE_EDITED) {
|
|
|
|
error = t('updateCheckSkippedMaybeLocallyEdited') + '\n' + t('updateCheckManualUpdateHint');
|
2018-11-20 04:55:38 +00:00
|
|
|
} else if (typeof error === 'object' && error.message) {
|
|
|
|
// UserCSS meta errors provide an object
|
|
|
|
error = error.message;
|
2018-01-01 17:02:49 +00:00
|
|
|
}
|
|
|
|
const message = same ? t('updateCheckSucceededNoUpdate') : error;
|
|
|
|
newClasses.set('no-update', true);
|
|
|
|
newClasses.set('update-problem', !same);
|
2018-12-08 14:42:29 +00:00
|
|
|
$('.check-update', entry).dataset.title = UI.enabled ? message : '';
|
|
|
|
$('.update', entry).dataset.title = t(edited ? 'updateCheckManualUpdateForce' : 'installUpdate');
|
2018-08-27 17:17:36 +00:00
|
|
|
// digest may change silently when forcing an update of a locally edited style
|
|
|
|
// so we need to update it in entry's styleMeta in all open manager tabs
|
|
|
|
if (error === STATES.SAME_CODE) {
|
|
|
|
for (const view of chrome.extension.getViews({type: 'tab'})) {
|
|
|
|
if (view.location.pathname === location.pathname) {
|
2018-11-25 23:35:54 +00:00
|
|
|
const entry = view.$(UI.ENTRY_ID_PREFIX + style.id);
|
2018-08-27 17:17:36 +00:00
|
|
|
if (entry) entry.styleMeta.originalDigest = style.originalDigest;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-26 19:54:35 +00:00
|
|
|
if (!isCheckAll) {
|
2018-01-01 17:02:49 +00:00
|
|
|
renderUpdatesOnlyFilter({show: $('.can-update, .update-problem')});
|
2017-08-15 11:40:36 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-01 04:45:04 +00:00
|
|
|
|
|
|
|
// construct a new className:
|
|
|
|
// 1. add all truthy newClasses
|
|
|
|
// 2. remove falsy newClasses
|
|
|
|
// 3. keep existing classes otherwise
|
|
|
|
const classes = new Map([...entry.classList.values()].map(cls => [cls, true]));
|
2018-01-01 17:02:49 +00:00
|
|
|
for (const [cls, newState] of newClasses.entries()) {
|
|
|
|
classes.set(cls, newState);
|
|
|
|
}
|
|
|
|
const className = [...classes.entries()]
|
|
|
|
.map(([cls, state]) => state && cls)
|
|
|
|
.filter(Boolean)
|
|
|
|
.join(' ');
|
|
|
|
if (className !== entry.className) {
|
|
|
|
entry.className = className;
|
|
|
|
}
|
2018-01-01 04:45:04 +00:00
|
|
|
|
2018-02-26 19:54:35 +00:00
|
|
|
if (filtersSelector.hide && isCheckAll) {
|
2018-05-15 09:21:30 +00:00
|
|
|
filterAndAppend({entry}).then(sorter.updateStripes);
|
2018-08-15 06:36:29 +00:00
|
|
|
} else if (updated && !isCheckAll) {
|
|
|
|
renderUpdatesOnlyFilter();
|
2017-08-15 11:40:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderUpdatesOnlyFilter({show, check} = {}) {
|
|
|
|
const numUpdatable = $$('.can-update').length;
|
|
|
|
const mightUpdate = numUpdatable > 0 || $('.update-problem');
|
2020-03-23 12:25:50 +00:00
|
|
|
const checkbox = $('#only-updates');
|
2017-08-15 11:40:36 +00:00
|
|
|
show = show !== undefined ? show : mightUpdate;
|
|
|
|
check = check !== undefined ? show && check : checkbox.checked && mightUpdate;
|
|
|
|
|
2020-03-23 12:25:50 +00:00
|
|
|
checkbox.checked = check;
|
|
|
|
checkbox.parentElement.classList.toggle('hidden', !show);
|
2017-08-15 11:40:36 +00:00
|
|
|
checkbox.dispatchEvent(new Event('change'));
|
|
|
|
|
|
|
|
const btnApply = $('#apply-all-updates');
|
2018-08-15 06:36:29 +00:00
|
|
|
btnApply.classList.toggle('hidden', !numUpdatable);
|
|
|
|
btnApply.dataset.value = numUpdatable;
|
2017-08-15 11:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-09 15:25:44 +00:00
|
|
|
function showUpdateHistory(event) {
|
|
|
|
event.preventDefault();
|
2017-12-09 20:26:28 +00:00
|
|
|
const log = $create('.update-history-log');
|
|
|
|
let logText, scroller, toggler;
|
|
|
|
let deleted = false;
|
2018-01-01 17:02:49 +00:00
|
|
|
Promise.all([
|
|
|
|
chromeLocal.getValue('updateLog'),
|
|
|
|
API.getUpdaterStates(),
|
|
|
|
]).then(([lines = [], states]) => {
|
2017-12-09 20:26:28 +00:00
|
|
|
logText = lines.join('\n');
|
2017-08-15 11:40:36 +00:00
|
|
|
messageBox({
|
|
|
|
title: t('updateCheckHistory'),
|
2017-12-09 20:26:28 +00:00
|
|
|
contents: log,
|
2017-12-12 18:33:41 +00:00
|
|
|
blockScroll: true,
|
2017-12-09 20:26:28 +00:00
|
|
|
buttons: [
|
|
|
|
t('confirmOK'),
|
|
|
|
logText && {textContent: t('confirmDelete'), onclick: deleteHistory},
|
|
|
|
],
|
|
|
|
onshow: logText && (() => {
|
|
|
|
scroller = $('#message-box-contents');
|
2017-12-12 18:33:41 +00:00
|
|
|
scroller.tabIndex = 0;
|
|
|
|
setTimeout(() => scroller.focus());
|
2017-12-09 20:26:28 +00:00
|
|
|
scrollToBottom();
|
2017-12-12 18:33:41 +00:00
|
|
|
|
2017-12-09 20:26:28 +00:00
|
|
|
$('#message-box-buttons button').insertAdjacentElement('afterend',
|
|
|
|
// TODO: add a global class for our labels
|
|
|
|
// TODO: add a <template> or a common function to create such controls
|
|
|
|
$create('label', {style: 'position: relative; padding-left: 16px;'}, [
|
|
|
|
toggler =
|
|
|
|
$create('input', {type: 'checkbox', checked: true, onchange: toggleSkipped}),
|
|
|
|
$create('SVG:svg.svg-icon.checked',
|
|
|
|
$create('SVG:use', {'xlink:href': '#svg-icon-checked'})),
|
|
|
|
t('manageOnlyUpdates'),
|
|
|
|
]));
|
2017-12-12 18:33:41 +00:00
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
toggler.rxRemoveNOP = new RegExp(
|
|
|
|
'^[^#]*(' +
|
|
|
|
Object.keys(states)
|
|
|
|
.filter(k => k.startsWith('SAME_'))
|
|
|
|
.map(k => states[k])
|
|
|
|
.join('|') +
|
|
|
|
').*\r?\n', 'gm');
|
2017-12-09 20:26:28 +00:00
|
|
|
toggler.onchange();
|
|
|
|
}),
|
2017-08-15 11:40:36 +00:00
|
|
|
});
|
|
|
|
});
|
2017-12-09 20:26:28 +00:00
|
|
|
function scrollToBottom() {
|
|
|
|
scroller.scrollTop = 1e9;
|
|
|
|
}
|
|
|
|
function calcScrollRatio() {
|
|
|
|
return (scroller.scrollTop + scroller.clientHeight) / scroller.scrollHeight;
|
|
|
|
}
|
|
|
|
function toggleSkipped() {
|
|
|
|
if (deleted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const scrollRatio = calcScrollRatio();
|
2018-01-01 17:02:49 +00:00
|
|
|
log.textContent = !this.checked ? logText : logText.replace(this.rxRemoveNOP, '');
|
2017-12-09 20:26:28 +00:00
|
|
|
if (Math.abs(scrollRatio - calcScrollRatio()) > .1) {
|
|
|
|
scroller.scrollTop = scrollRatio * scroller.scrollHeight - scroller.clientHeight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function deleteHistory() {
|
|
|
|
if (deleted) {
|
2018-01-01 17:02:49 +00:00
|
|
|
chromeLocal.setValue('updateLog', logText.split('\n'));
|
2017-12-09 20:26:28 +00:00
|
|
|
setTimeout(scrollToBottom);
|
|
|
|
} else {
|
2018-01-01 17:02:49 +00:00
|
|
|
chromeLocal.remove('updateLog');
|
2017-12-09 20:26:28 +00:00
|
|
|
log.textContent = '';
|
|
|
|
}
|
|
|
|
deleted = !deleted;
|
|
|
|
toggler.onchange();
|
|
|
|
this.textContent = t(deleted ? 'undo' : 'confirmDelete');
|
|
|
|
}
|
2017-08-15 11:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-02 04:07:12 +00:00
|
|
|
function handleUpdateInstalled(entry, reason) {
|
|
|
|
const isNew = reason === 'install';
|
|
|
|
const note = t(isNew ? 'installButtonInstalled' : 'updateCompleted');
|
|
|
|
entry.classList.add('update-done', ...(isNew ? ['install-done'] : []));
|
2017-08-15 11:40:36 +00:00
|
|
|
entry.classList.remove('can-update', 'updatable');
|
2018-12-08 14:42:29 +00:00
|
|
|
$('.updated', entry).dataset.title = note;
|
2017-08-15 11:40:36 +00:00
|
|
|
renderUpdatesOnlyFilter();
|
|
|
|
}
|