2021-01-01 14:27:58 +00:00
|
|
|
/* global API msg */// msg.js
|
2022-01-27 12:21:02 +00:00
|
|
|
/* global CHROME UA debounce */// toolbox.js
|
2021-01-01 14:27:58 +00:00
|
|
|
/* global Events handleBulkChange handleVisibilityChange */// events.js
|
|
|
|
/* global fitSelectBoxesIn switchUI showStyles */// render.js
|
|
|
|
/* global prefs */
|
|
|
|
/* global router */
|
|
|
|
/* global sorter */
|
|
|
|
/* global t */// localization.js
|
2020-11-18 11:17:15 +00:00
|
|
|
/* global
|
|
|
|
$
|
|
|
|
$$
|
|
|
|
$create
|
|
|
|
animateElement
|
|
|
|
setupLivePrefs
|
2021-01-01 14:27:58 +00:00
|
|
|
waitForSelector
|
2021-01-05 16:54:49 +00:00
|
|
|
waitForSheet
|
2021-01-01 14:27:58 +00:00
|
|
|
*/// dom.js
|
2017-03-26 02:30:59 +00:00
|
|
|
'use strict';
|
2015-01-30 17:31:20 +00:00
|
|
|
|
2020-11-18 11:17:15 +00:00
|
|
|
/** @type {HTMLElement} */
|
2017-04-08 11:03:54 +00:00
|
|
|
let installed;
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
const changeQueue = [];
|
|
|
|
changeQueue.THROTTLE = 100; // ms
|
|
|
|
changeQueue.time = 0;
|
2020-06-01 00:14:42 +00:00
|
|
|
|
2020-10-02 15:10:52 +00:00
|
|
|
// define pref-mapped ids separately
|
2017-04-05 13:14:59 +00:00
|
|
|
const newUI = {
|
2020-10-02 15:10:52 +00:00
|
|
|
enabled: null, // the global option should come first
|
|
|
|
favicons: null,
|
|
|
|
faviconsGray: null,
|
|
|
|
targets: null,
|
2017-04-05 13:14:59 +00:00
|
|
|
};
|
2020-10-02 15:10:52 +00:00
|
|
|
// ...add utility functions
|
|
|
|
Object.assign(newUI, {
|
|
|
|
ids: Object.keys(newUI),
|
2021-01-15 10:16:14 +00:00
|
|
|
prefKeyForId: id => `manage.newUI.${id}`.replace(/\.enabled$/, ''),
|
2020-11-22 11:09:59 +00:00
|
|
|
renderClass: () => {
|
2022-02-14 19:19:20 +00:00
|
|
|
$.rootCL.toggle('newUI', newUI.enabled);
|
|
|
|
$.rootCL.toggle('oldUI', !newUI.enabled);
|
2020-11-22 11:09:59 +00:00
|
|
|
},
|
2020-10-02 15:10:52 +00:00
|
|
|
});
|
|
|
|
// ...read the actual values
|
|
|
|
for (const id of newUI.ids) {
|
|
|
|
newUI[id] = prefs.get(newUI.prefKeyForId(id));
|
|
|
|
}
|
2017-04-08 11:03:54 +00:00
|
|
|
newUI.renderClass();
|
2017-04-05 13:14:59 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
(async function init() {
|
2020-11-18 11:17:15 +00:00
|
|
|
const query = router.getSearch('search');
|
|
|
|
const [styles, ids, el] = await Promise.all([
|
2021-01-01 14:27:58 +00:00
|
|
|
API.styles.getAll(),
|
|
|
|
query && API.styles.searchDB({query, mode: router.getSearch('searchMode')}),
|
|
|
|
// needed to avoid flicker due to an extra frame and layout shift
|
|
|
|
waitForSelector('#installed'),
|
|
|
|
prefs.ready,
|
2020-11-18 11:17:15 +00:00
|
|
|
]);
|
2020-10-15 10:55:27 +00:00
|
|
|
installed = el;
|
2021-01-01 14:27:58 +00:00
|
|
|
installed.on('click', Events.entryClicked);
|
|
|
|
installed.on('mouseover', Events.lazyAddEntryTitle, {passive: true});
|
|
|
|
installed.on('mouseout', Events.lazyAddEntryTitle, {passive: true});
|
2020-08-24 16:27:23 +00:00
|
|
|
$('#manage-options-button').onclick = () => router.updateHash('#stylus-options');
|
2022-01-14 12:44:48 +00:00
|
|
|
$('#injection-order-button').onclick = () => router.updateHash('#injection-order');
|
2020-08-24 16:27:23 +00:00
|
|
|
$('#sync-styles').onclick = () => router.updateHash('#stylus-options');
|
2021-01-01 14:27:58 +00:00
|
|
|
$$('#header a[href^="http"]').forEach(a => (a.onclick = Events.external));
|
2022-01-28 23:46:12 +00:00
|
|
|
window.on('pageshow', handleVisibilityChange);
|
|
|
|
window.on('pagehide', handleVisibilityChange);
|
2017-04-21 16:39:34 +00:00
|
|
|
setupLivePrefs();
|
2021-01-01 14:27:58 +00:00
|
|
|
router.update();
|
2020-10-02 15:10:52 +00:00
|
|
|
prefs.subscribe(newUI.ids.map(newUI.prefKeyForId), () => switchUI());
|
2017-04-05 13:14:59 +00:00
|
|
|
switchUI({styleOnly: true});
|
2017-08-27 09:56:09 +00:00
|
|
|
// translate CSS manually
|
2020-11-18 11:17:15 +00:00
|
|
|
document.styleSheets[0].insertRule(
|
|
|
|
`:root {${[
|
2020-11-22 11:09:59 +00:00
|
|
|
'genericDisabledLabel',
|
2020-11-18 11:17:15 +00:00
|
|
|
'updateAllCheckSucceededSomeEdited',
|
|
|
|
'filteredStylesAllHidden',
|
|
|
|
].map(id => `--${id}:"${CSS.escape(t(id))}";`).join('')
|
|
|
|
}}`);
|
2021-01-01 14:27:58 +00:00
|
|
|
|
2022-01-27 12:21:02 +00:00
|
|
|
if (!UA.vivaldi) {
|
2021-01-05 16:54:49 +00:00
|
|
|
waitForSheet().then(() => {
|
|
|
|
fitSelectBoxesIn($('#filters'));
|
|
|
|
});
|
2020-10-15 10:55:27 +00:00
|
|
|
}
|
|
|
|
if (CHROME >= 80 && CHROME <= 88) {
|
|
|
|
// Wrong checkboxes are randomly checked after going back in history, https://crbug.com/1138598
|
2021-01-01 14:27:58 +00:00
|
|
|
window.on('pagehide', () => {
|
2020-10-15 10:55:27 +00:00
|
|
|
$$('input[type=checkbox]').forEach((el, i) => (el.name = `bug${i}`));
|
|
|
|
});
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
|
2020-10-15 10:55:27 +00:00
|
|
|
showStyles(styles, ids);
|
2021-01-01 14:27:58 +00:00
|
|
|
|
2022-01-28 23:54:56 +00:00
|
|
|
window.on('load', () => require([
|
2021-01-01 14:27:58 +00:00
|
|
|
'/manage/import-export',
|
|
|
|
'/manage/incremental-search',
|
|
|
|
'/manage/updater-ui',
|
2022-01-28 23:54:56 +00:00
|
|
|
]), {once: true});
|
2020-11-18 11:17:15 +00:00
|
|
|
})();
|
2020-10-15 10:55:27 +00:00
|
|
|
|
|
|
|
msg.onExtension(onRuntimeMessage);
|
2021-01-01 14:27:58 +00:00
|
|
|
window.on('closeOptions', () => router.updateHash(''));
|
|
|
|
router.watch({hash: '#stylus-options'}, toggleEmbeddedOptions);
|
2022-01-14 12:44:48 +00:00
|
|
|
router.watch({hash: '#injection-order'}, toggleInjectionOrder);
|
2020-10-15 10:55:27 +00:00
|
|
|
|
|
|
|
function onRuntimeMessage(msg) {
|
|
|
|
switch (msg.method) {
|
|
|
|
case 'styleUpdated':
|
|
|
|
case 'styleAdded':
|
|
|
|
case 'styleDeleted':
|
2021-01-01 14:27:58 +00:00
|
|
|
changeQueue.push(msg);
|
|
|
|
if (performance.now() - (changeQueue.time || 0) < changeQueue.THROTTLE) {
|
|
|
|
debounce(handleBulkChange, changeQueue.THROTTLE);
|
2020-10-15 10:55:27 +00:00
|
|
|
} else {
|
|
|
|
handleBulkChange();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'styleApply':
|
|
|
|
case 'styleReplaceAll':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setTimeout(sorter.updateStripes, 0, {onlyWhenColumnsChanged: true});
|
2015-01-30 17:31:20 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
async function toggleEmbeddedOptions(state) {
|
|
|
|
const el = $('#stylus-embedded-options') ||
|
2022-02-14 19:19:20 +00:00
|
|
|
state && $.root.appendChild($create('iframe', {
|
2020-11-18 11:17:15 +00:00
|
|
|
id: 'stylus-embedded-options',
|
|
|
|
src: '/options.html',
|
|
|
|
}));
|
2021-01-01 14:27:58 +00:00
|
|
|
if (state) {
|
|
|
|
el.focus();
|
|
|
|
} else if (el) {
|
|
|
|
el.contentDocument.body.classList.add('scaleout');
|
|
|
|
await animateElement(el, 'fadeout');
|
|
|
|
el.remove();
|
2020-02-02 04:36:54 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-14 12:44:48 +00:00
|
|
|
|
|
|
|
async function toggleInjectionOrder(state) {
|
|
|
|
const shown = $('.injection-order');
|
|
|
|
if (state && !shown) {
|
|
|
|
await require([
|
|
|
|
'/vendor/draggable-list/draggable-list.iife.min.js',
|
|
|
|
'/injection-order/injection-order.css',
|
|
|
|
'/injection-order/injection-order', /* global InjectionOrder */
|
|
|
|
]);
|
|
|
|
await InjectionOrder();
|
|
|
|
router.updateHash('');
|
|
|
|
} else if (!state && shown) {
|
|
|
|
await InjectionOrder(false);
|
|
|
|
}
|
|
|
|
}
|