2021-01-01 14:27:58 +00:00
|
|
|
/* global $ $$ $create messageBoxProxy scrollElementIntoView */// dom.js
|
|
|
|
/* global $entry */// render.js
|
|
|
|
/* global API */// msg.js
|
|
|
|
/* global filterAndAppend filtersSelector */// filters.js
|
|
|
|
/* global newUI */// manage.js
|
|
|
|
/* global sorter */
|
|
|
|
/* global t */// localization.js
|
2017-08-15 11:40:36 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
$('#check-all-updates').onclick = checkUpdateAll;
|
|
|
|
$('#check-all-updates-force').onclick = checkUpdateAll;
|
|
|
|
$('#apply-all-updates').onclick = applyUpdateAll;
|
|
|
|
$('#update-history').onclick = showUpdateHistory;
|
2017-08-15 11:40:36 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkUpdateAll() {
|
|
|
|
document.body.classList.add('update-in-progress');
|
2018-01-01 17:02:49 +00:00
|
|
|
const btnCheck = $('#check-all-updates');
|
|
|
|
const btnCheckForce = $('#check-all-updates-force');
|
|
|
|
const btnApply = $('#apply-all-updates');
|
|
|
|
const noUpdates = $('#update-all-no-updates');
|
|
|
|
btnCheck.disabled = true;
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
API.updater.checkAllStyles({
|
2018-01-01 17:02:49 +00:00
|
|
|
save: false,
|
|
|
|
observe: true,
|
|
|
|
ignoreDigest,
|
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-01-01 17:02:49 +00:00
|
|
|
btnCheck.disabled = total === 0;
|
|
|
|
btnApply.disabled = false;
|
2017-08-15 11:40:36 +00:00
|
|
|
renderUpdatesOnlyFilter({check: updated + skippedEdited > 0});
|
|
|
|
if (!updated) {
|
2018-01-01 17:02:49 +00:00
|
|
|
noUpdates.dataset.skippedEdited = skippedEdited > 0;
|
|
|
|
noUpdates.classList.remove('hidden');
|
|
|
|
btnCheckForce.classList.toggle('hidden', skippedEdited === 0);
|
2017-08-15 11:40:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
function checkUpdate(entry, {single} = {}) {
|
2017-08-15 11:40:36 +00:00
|
|
|
$('.update-note', entry).textContent = t('checkingForUpdate');
|
|
|
|
$('.check-update', entry).title = '';
|
|
|
|
if (single) {
|
2021-01-01 14:27:58 +00:00
|
|
|
API.updater.checkStyle({
|
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');
|
2021-01-01 14:27:58 +00:00
|
|
|
const entry = $entry(style);
|
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;
|
|
|
|
$('.update-note', entry).textContent = '';
|
|
|
|
$('#only-updates').classList.remove('hidden');
|
|
|
|
} 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;
|
|
|
|
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;
|
2022-01-19 11:42:10 +00:00
|
|
|
} else if (Array.isArray(error)) {
|
|
|
|
// UserCSS build error
|
|
|
|
error = error.map(e => `${e.message || e}${
|
|
|
|
e.context ? '\n' + e.context.replace(/^/gm, '\t') : '' // indenting source text
|
|
|
|
}`).join('\n');
|
2018-01-01 17:02:49 +00:00
|
|
|
}
|
2022-01-19 11:42:10 +00:00
|
|
|
entry.dataset.error = error;
|
2018-01-01 17:02:49 +00:00
|
|
|
const message = same ? t('updateCheckSucceededNoUpdate') : error;
|
|
|
|
newClasses.set('no-update', true);
|
|
|
|
newClasses.set('update-problem', !same);
|
|
|
|
$('.update-note', entry).textContent = message;
|
|
|
|
$('.check-update', entry).title = newUI.enabled ? message : '';
|
|
|
|
$('.update', entry).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) {
|
2021-01-01 14:27:58 +00:00
|
|
|
const entry = $entry(style, view.document);
|
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]));
|
2021-01-01 14:27:58 +00:00
|
|
|
for (const [cls, newState] of newClasses) {
|
2018-01-01 17:02:49 +00:00
|
|
|
classes.set(cls, newState);
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
const className = [...classes]
|
2018-01-01 17:02:49 +00:00
|
|
|
.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');
|
2017-12-06 22:39:15 +00:00
|
|
|
const checkbox = $('#only-updates input');
|
2017-08-15 11:40:36 +00:00
|
|
|
show = show !== undefined ? show : mightUpdate;
|
|
|
|
check = check !== undefined ? show && check : checkbox.checked && mightUpdate;
|
|
|
|
|
2017-12-06 22:39:15 +00:00
|
|
|
$('#only-updates').classList.toggle('hidden', !show);
|
|
|
|
checkbox.checked = check && 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
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
async function showUpdateHistory(event) {
|
2017-12-09 15:25:44 +00:00
|
|
|
event.preventDefault();
|
2017-12-09 20:26:28 +00:00
|
|
|
const log = $create('.update-history-log');
|
2021-01-01 14:27:58 +00:00
|
|
|
let scroller, toggler;
|
2017-12-09 20:26:28 +00:00
|
|
|
let deleted = false;
|
2021-01-01 14:27:58 +00:00
|
|
|
await require(['/js/storage-util']); /* global chromeLocal */
|
2021-02-27 17:42:49 +00:00
|
|
|
const [lines = [], states] = await Promise.all([
|
2018-01-01 17:02:49 +00:00
|
|
|
chromeLocal.getValue('updateLog'),
|
2021-01-01 14:27:58 +00:00
|
|
|
API.updater.getStates(),
|
|
|
|
]);
|
|
|
|
const logText = lines.join('\n');
|
|
|
|
messageBoxProxy.show({
|
|
|
|
title: t('updateCheckHistory'),
|
2021-08-06 16:50:36 +00:00
|
|
|
className: 'center-dialog',
|
2021-01-01 14:27:58 +00:00
|
|
|
contents: log,
|
|
|
|
blockScroll: true,
|
|
|
|
buttons: [
|
|
|
|
t('confirmOK'),
|
|
|
|
logText && {textContent: t('confirmDelete'), onclick: deleteHistory},
|
|
|
|
],
|
|
|
|
onshow: logText && (() => {
|
|
|
|
scroller = $('#message-box-contents');
|
|
|
|
scroller.tabIndex = 0;
|
|
|
|
setTimeout(() => scroller.focus());
|
|
|
|
scrollToBottom();
|
2017-12-12 18:33:41 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
$('#message-box-buttons button').insertAdjacentElement('afterend',
|
|
|
|
// TODO: add a <template> or a common function to create such controls
|
2022-02-19 08:18:14 +00:00
|
|
|
$create('label.checkbox-wrapper', [
|
2021-01-01 14:27:58 +00:00
|
|
|
toggler =
|
2017-12-09 20:26:28 +00:00
|
|
|
$create('input', {type: 'checkbox', checked: true, onchange: toggleSkipped}),
|
2021-01-01 14:27:58 +00:00
|
|
|
$create('SVG:svg.svg-icon.checked',
|
|
|
|
$create('SVG:use', {'xlink:href': '#svg-icon-checked'})),
|
|
|
|
t('manageOnlyUpdates'),
|
|
|
|
]));
|
2017-12-12 18:33:41 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
toggler.rxRemoveNOP = new RegExp(
|
|
|
|
'^[^#]*(' +
|
|
|
|
Object.keys(states)
|
|
|
|
.filter(k => k.startsWith('SAME_'))
|
|
|
|
.map(k => states[k])
|
|
|
|
.join('|') +
|
|
|
|
').*\r?\n', 'gm');
|
|
|
|
toggler.onchange();
|
|
|
|
}),
|
2017-08-15 11:40:36 +00:00
|
|
|
});
|
2021-01-01 14:27:58 +00:00
|
|
|
|
2017-12-09 20:26:28 +00:00
|
|
|
function scrollToBottom() {
|
|
|
|
scroller.scrollTop = 1e9;
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
|
2017-12-09 20:26:28 +00:00
|
|
|
function calcScrollRatio() {
|
|
|
|
return (scroller.scrollTop + scroller.clientHeight) / scroller.scrollHeight;
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
|
2017-12-09 20:26:28 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
|
2017-12-09 20:26:28 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/* exported handleUpdateInstalled */
|
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');
|
2017-12-02 04:07:12 +00:00
|
|
|
$('.update-note', entry).textContent = note;
|
|
|
|
$('.updated', entry).title = note;
|
2017-08-15 11:40:36 +00:00
|
|
|
renderUpdatesOnlyFilter();
|
|
|
|
}
|