2017-06-10 11:08:03 +00:00
|
|
|
/* global messageBox, getStyleWithNoCode, retranslateCSS */
|
2017-08-15 11:40:36 +00:00
|
|
|
/* global filtersSelector, filterAndAppend */
|
|
|
|
/* global checkUpdate, handleUpdateInstalled */
|
|
|
|
/* global objectDiff */
|
2017-10-12 08:10:17 +00:00
|
|
|
/* global configDialog */
|
2017-12-23 00:11:46 +00:00
|
|
|
/* global sorter */
|
2017-03-26 02:30:59 +00:00
|
|
|
'use strict';
|
2015-01-30 17:31:20 +00:00
|
|
|
|
2017-04-08 11:03:54 +00:00
|
|
|
let installed;
|
|
|
|
|
2017-06-27 20:13:11 +00:00
|
|
|
const ENTRY_ID_PREFIX_RAW = 'style-';
|
|
|
|
const ENTRY_ID_PREFIX = '#' + ENTRY_ID_PREFIX_RAW;
|
|
|
|
|
2017-04-05 13:14:59 +00:00
|
|
|
const newUI = {
|
|
|
|
enabled: prefs.get('manage.newUI'),
|
|
|
|
favicons: prefs.get('manage.newUI.favicons'),
|
2017-04-18 18:49:04 +00:00
|
|
|
faviconsGray: prefs.get('manage.newUI.faviconsGray'),
|
2017-04-05 13:14:59 +00:00
|
|
|
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-08-22 10:28:16 +00:00
|
|
|
usePrefsDuringPageLoad();
|
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-13 08:03:45 +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-08-27 09:56:09 +00:00
|
|
|
dieOnNullBackground();
|
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-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-12-23 00:11:46 +00:00
|
|
|
// show date installed & last update on hover
|
|
|
|
installed.addEventListener('mouseover', handleEvent.lazyAddEntryTitle);
|
|
|
|
installed.addEventListener('mouseout', handleEvent.lazyAddEntryTitle);
|
2017-03-21 01:32:38 +00:00
|
|
|
|
|
|
|
// 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-13 08:03:45 +00:00
|
|
|
$$('[data-toggle-on-click]').forEach(el => {
|
2017-04-18 18:38:22 +00:00
|
|
|
// dataset on SVG doesn't work in Chrome 49-??, works in 57+
|
|
|
|
const target = $(el.getAttribute('data-toggle-on-click'));
|
2017-12-06 06:39:45 +00:00
|
|
|
el.onclick = event => {
|
|
|
|
event.preventDefault();
|
|
|
|
target.classList.toggle('hidden');
|
|
|
|
if (target.classList.contains('hidden')) {
|
|
|
|
el.removeAttribute('open');
|
|
|
|
} else {
|
|
|
|
el.setAttribute('open', '');
|
|
|
|
}
|
|
|
|
};
|
2017-04-13 08:03:45 +00:00
|
|
|
});
|
|
|
|
|
2017-04-18 18:49:04 +00:00
|
|
|
// triggered automatically by setupLivePrefs() below
|
2017-04-13 07:54:56 +00:00
|
|
|
enforceInputRange($('#manage.newUI.targets'));
|
2017-04-05 13:14:59 +00:00
|
|
|
|
2017-04-18 18:49:04 +00:00
|
|
|
// N.B. triggers existing onchange listeners
|
2017-04-21 16:39:34 +00:00
|
|
|
setupLivePrefs();
|
2017-12-23 00:11:46 +00:00
|
|
|
sorter.init();
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-13 08:03:45 +00:00
|
|
|
$$('[id^="manage.newUI"]')
|
|
|
|
.forEach(el => (el.oninput = (el.onchange = switchUI)));
|
2017-04-05 13:14:59 +00:00
|
|
|
|
|
|
|
switchUI({styleOnly: true});
|
2017-08-27 09:56:09 +00:00
|
|
|
|
|
|
|
// translate CSS manually
|
2017-12-03 21:12:09 +00:00
|
|
|
document.head.appendChild($create('style', `
|
2017-08-27 09:56:09 +00:00
|
|
|
.disabled h2::after {
|
|
|
|
content: "${t('genericDisabledLabel')}";
|
|
|
|
}
|
|
|
|
#update-all-no-updates[data-skipped-edited="true"]:after {
|
|
|
|
content: " ${t('updateAllCheckSucceededSomeEdited')}";
|
|
|
|
}
|
2017-12-03 23:38:44 +00:00
|
|
|
body.all-styles-hidden-by-filters:after {
|
|
|
|
content: "${t('filteredStylesAllHidden')}";
|
|
|
|
}
|
2017-12-03 21:12:09 +00:00
|
|
|
`));
|
2015-01-30 17:31:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
|
|
|
|
function showStyles(styles = []) {
|
2017-12-23 00:11:46 +00:00
|
|
|
const sorted = sorter.sort({
|
|
|
|
styles: styles.map(style => ({
|
|
|
|
style,
|
|
|
|
name: style.name.toLocaleLowerCase() + '\n' + style.name,
|
|
|
|
})),
|
|
|
|
}).map((info, index) => {
|
|
|
|
info.index = index;
|
|
|
|
return info;
|
|
|
|
});
|
2017-04-22 12:09:48 +00:00
|
|
|
let index = 0;
|
2017-12-23 00:11:46 +00:00
|
|
|
installed.dataset.total = styles.length;
|
2017-04-27 19:12:32 +00:00
|
|
|
const scrollY = (history.state || {}).scrollY;
|
2017-06-27 20:01:17 +00:00
|
|
|
const shouldRenderAll = scrollY > window.innerHeight || sessionStorage.justEditedStyleId;
|
2017-03-21 01:32:38 +00:00
|
|
|
const renderBin = document.createDocumentFragment();
|
2017-04-27 19:12:32 +00:00
|
|
|
if (scrollY) {
|
2017-04-22 12:09:48 +00:00
|
|
|
renderStyles();
|
|
|
|
} else {
|
|
|
|
requestAnimationFrame(renderStyles);
|
|
|
|
}
|
2017-04-10 08:26:30 +00:00
|
|
|
|
2017-04-22 12:09:48 +00:00
|
|
|
function renderStyles() {
|
2017-03-21 01:32:38 +00:00
|
|
|
const t0 = performance.now();
|
2017-04-19 20:54:05 +00:00
|
|
|
let rendered = 0;
|
2017-07-12 20:44:59 +00:00
|
|
|
while (
|
|
|
|
index < sorted.length &&
|
|
|
|
// eslint-disable-next-line no-unmodified-loop-condition
|
2017-12-24 07:03:13 +00:00
|
|
|
(shouldRenderAll || ++rendered < 20 || performance.now() - t0 < 10)
|
2017-07-12 20:44:59 +00:00
|
|
|
) {
|
2017-03-26 08:43:45 +00:00
|
|
|
renderBin.appendChild(createStyleElement(sorted[index++]));
|
2017-03-25 02:35:54 +00:00
|
|
|
}
|
2017-04-16 11:24:49 +00:00
|
|
|
filterAndAppend({container: renderBin});
|
2017-12-24 06:42:07 +00:00
|
|
|
if (newUI.enabled && newUI.favicons) {
|
|
|
|
debounce(handleEvent.loadFavicons);
|
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
if (index < sorted.length) {
|
2017-04-22 12:09:48 +00:00
|
|
|
requestAnimationFrame(renderStyles);
|
|
|
|
return;
|
|
|
|
}
|
2017-12-13 06:19:49 +00:00
|
|
|
if ('scrollY' in (history.state || {}) && !sessionStorage.justEditedStyleId) {
|
2017-04-22 12:09:48 +00:00
|
|
|
setTimeout(window.scrollTo, 0, 0, history.state.scrollY);
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
2017-06-27 20:01:17 +00:00
|
|
|
if (sessionStorage.justEditedStyleId) {
|
|
|
|
const entry = $(ENTRY_ID_PREFIX + sessionStorage.justEditedStyleId);
|
|
|
|
delete sessionStorage.justEditedStyleId;
|
|
|
|
if (entry) {
|
|
|
|
animateElement(entry);
|
|
|
|
scrollElementIntoView(entry);
|
2017-06-27 20:13:11 +00:00
|
|
|
}
|
2017-06-27 20:01:17 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-30 17:31:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-12-23 00:11:46 +00:00
|
|
|
function createStyleElement({style, name, index}) {
|
2017-04-10 08:26:30 +00:00
|
|
|
// query the sub-elements just once, then reuse the references
|
|
|
|
if ((createStyleElement.parts || {}).newUI !== newUI.enabled) {
|
2017-04-22 12:09:48 +00:00
|
|
|
const entry = template[`style${newUI.enabled ? 'Compact' : ''}`];
|
2017-04-10 08:26:30 +00:00
|
|
|
createStyleElement.parts = {
|
|
|
|
newUI: newUI.enabled,
|
|
|
|
entry,
|
|
|
|
entryClassBase: entry.className,
|
2017-04-14 18:30:55 +00:00
|
|
|
checker: $('.checker', entry) || {},
|
2017-04-10 08:26:30 +00:00
|
|
|
nameLink: $('.style-name-link', entry),
|
|
|
|
editLink: $('.style-edit-link', entry) || {},
|
2017-06-18 12:46:53 +00:00
|
|
|
editHrefBase: 'edit.html?id=',
|
2017-04-10 08:26:30 +00:00
|
|
|
homepage: $('.homepage', entry),
|
2017-04-22 12:09:48 +00:00
|
|
|
homepageIcon: template[`homepageIcon${newUI.enabled ? 'Small' : 'Big'}`],
|
2017-04-10 08:26:30 +00:00
|
|
|
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;
|
2017-04-14 18:30:55 +00:00
|
|
|
parts.checker.checked = style.enabled;
|
2017-12-07 20:21:27 +00:00
|
|
|
parts.nameLink.textContent = tWordBreak(style.name);
|
2017-04-10 08:26:30 +00:00
|
|
|
parts.nameLink.href = parts.editLink.href = parts.editHrefBase + style.id;
|
|
|
|
parts.homepage.href = parts.homepage.title = style.url || '';
|
|
|
|
|
2017-04-14 18:30:55 +00:00
|
|
|
const entry = parts.entry.cloneNode(true);
|
2017-06-27 20:13:11 +00:00
|
|
|
entry.id = ENTRY_ID_PREFIX_RAW + style.id;
|
2017-04-14 18:30:55 +00:00
|
|
|
entry.styleId = style.id;
|
|
|
|
entry.styleNameLowerCase = name || style.name.toLocaleLowerCase();
|
2017-04-26 10:56:32 +00:00
|
|
|
entry.styleMeta = getStyleWithNoCode(style);
|
2017-04-14 18:30:55 +00:00
|
|
|
entry.className = parts.entryClassBase + ' ' +
|
|
|
|
(style.enabled ? 'enabled' : 'disabled') +
|
2017-11-22 14:21:50 +00:00
|
|
|
(style.updateUrl ? ' updatable' : '') +
|
|
|
|
(style.usercssData ? ' usercss' : '');
|
2017-12-23 00:11:46 +00:00
|
|
|
if (index !== undefined) entry.classList.add(index % 2 ? 'odd' : 'even');
|
2017-04-14 18:30:55 +00:00
|
|
|
|
2017-09-01 10:24:32 +00:00
|
|
|
if (style.url) {
|
2017-04-22 12:09:48 +00:00
|
|
|
$('.homepage', entry).appendChild(parts.homepageIcon.cloneNode(true));
|
|
|
|
}
|
|
|
|
if (style.updateUrl && newUI.enabled) {
|
|
|
|
$('.actions', entry).appendChild(template.updaterIcons.cloneNode(true));
|
|
|
|
}
|
2017-11-27 11:51:51 +00:00
|
|
|
if (style.usercssData && Object.keys(style.usercssData.vars).length > 0 && newUI.enabled) {
|
2017-08-05 16:49:25 +00:00
|
|
|
$('.actions', entry).appendChild(template.configureIcon.cloneNode(true));
|
|
|
|
}
|
2017-04-22 12:09:48 +00:00
|
|
|
|
2017-12-24 06:42:07 +00:00
|
|
|
createStyleTargetsElement({entry, style});
|
2017-04-14 18:30:55 +00:00
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-24 06:42:07 +00:00
|
|
|
function createStyleTargetsElement({entry, style}) {
|
2017-04-14 18:30:55 +00:00
|
|
|
const parts = createStyleElement.parts;
|
2017-04-10 08:26:30 +00:00
|
|
|
const targets = parts.targets.cloneNode(true);
|
|
|
|
let container = targets;
|
2017-04-05 13:14:59 +00:00
|
|
|
let numTargets = 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) {
|
2017-07-16 18:02:00 +00:00
|
|
|
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 = '';
|
2017-07-16 18:02:00 +00:00
|
|
|
if (type === 'domains') {
|
2017-04-13 08:03:45 +00:00
|
|
|
favicon = GET_FAVICON_URL + targetValue;
|
2017-04-05 13:14:59 +00:00
|
|
|
} else if (targetValue.startsWith('chrome-extension:')) {
|
|
|
|
favicon = OWN_ICON;
|
2017-07-16 18:02:00 +00:00
|
|
|
} else if (type !== 'regexps') {
|
2017-04-13 08:03:45 +00:00
|
|
|
favicon = targetValue.includes('://') && targetValue.match(/^.*?:\/\/([^/]+)/);
|
|
|
|
favicon = favicon ? GET_FAVICON_URL + favicon[1] : '';
|
2017-04-05 13:14:59 +00:00
|
|
|
}
|
|
|
|
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-04-05 13:14:59 +00:00
|
|
|
if (newUI.enabled) {
|
2017-04-14 18:30:55 +00:00
|
|
|
if (numTargets > newUI.targets) {
|
|
|
|
$('.applies-to', entry).classList.add('has-more');
|
|
|
|
}
|
2017-04-05 13:14:59 +00:00
|
|
|
}
|
2017-04-14 18:30:55 +00:00
|
|
|
const entryTargets = $('.targets', entry);
|
2017-04-10 08:26:30 +00:00
|
|
|
if (numTargets) {
|
2017-04-14 18:30:55 +00:00
|
|
|
entryTargets.parentElement.replaceChild(targets, entryTargets);
|
2017-04-10 08:26:30 +00:00
|
|
|
} else {
|
2017-04-14 18:30:55 +00:00
|
|
|
entryTargets.appendChild(template.appliesToEverything.cloneNode(true));
|
2017-04-10 08:26:30 +00:00
|
|
|
}
|
2017-04-14 18:30:55 +00:00
|
|
|
entry.classList.toggle('global', !numTargets);
|
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',
|
2017-11-26 17:25:25 +00:00
|
|
|
'.style-name': 'edit',
|
2017-04-05 13:14:59 +00:00
|
|
|
'.homepage': 'external',
|
|
|
|
'.check-update': 'check',
|
|
|
|
'.update': 'update',
|
|
|
|
'.delete': 'delete',
|
|
|
|
'.applies-to .expander': 'expandTargets',
|
2017-10-12 08:10:17 +00:00
|
|
|
'.configure-usercss': 'config'
|
2017-08-05 16:49:25 +00:00
|
|
|
},
|
|
|
|
|
2017-04-05 13:14:59 +00:00
|
|
|
entryClicked(event) {
|
|
|
|
const target = event.target;
|
|
|
|
const entry = target.closest('.entry');
|
|
|
|
for (const selector in handleEvent.ENTRY_ROUTES) {
|
2017-07-16 18:02:00 +00:00
|
|
|
for (let el = target; el && el !== entry; el = el.parentElement) {
|
2017-04-05 13:14:59 +00:00
|
|
|
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-07-16 18:02:00 +00:00
|
|
|
const left = event.button === 0;
|
|
|
|
const middle = event.button === 1;
|
2017-03-26 02:30:59 +00:00
|
|
|
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);
|
2017-11-26 17:25:25 +00:00
|
|
|
const url = $('[href]', event.target.closest('.entry')).href;
|
2017-03-21 01:32:38 +00:00
|
|
|
if (openWindow || openBackgroundTab || openForegroundTab) {
|
2017-11-25 13:24:07 +00:00
|
|
|
if (chrome.windows && openWindow) {
|
2017-03-21 01:32:38 +00:00
|
|
|
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) {
|
2017-12-09 15:25:44 +00:00
|
|
|
event.preventDefault();
|
2017-04-05 13:14:59 +00:00
|
|
|
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-12-09 15:25:44 +00:00
|
|
|
event.preventDefault();
|
2017-09-18 04:18:55 +00:00
|
|
|
const request = Object.assign(entry.updatedCode, {
|
2017-04-05 13:14:59 +00:00
|
|
|
id: entry.styleId,
|
2017-03-25 02:35:54 +00:00
|
|
|
reason: 'update',
|
2017-09-18 04:18:55 +00:00
|
|
|
});
|
|
|
|
if (entry.updatedCode.usercssData) {
|
2017-11-09 01:05:26 +00:00
|
|
|
onBackgroundReady()
|
|
|
|
.then(() => BG.usercssHelper.save(request));
|
2017-09-18 04:18:55 +00:00
|
|
|
} else {
|
|
|
|
// update everything but name
|
|
|
|
request.name = null;
|
|
|
|
saveStyleSafe(request);
|
|
|
|
}
|
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) {
|
2017-12-09 15:25:44 +00:00
|
|
|
event.preventDefault();
|
2017-04-05 13:14:59 +00:00
|
|
|
const id = entry.styleId;
|
2017-04-11 10:51:40 +00:00
|
|
|
const {name} = BG.cachedStyles.byId.get(id) || {};
|
2017-06-27 20:00:24 +00:00
|
|
|
animateElement(entry);
|
2017-03-25 19:50:37 +00:00
|
|
|
messageBox({
|
|
|
|
title: t('deleteStyleConfirm'),
|
|
|
|
contents: name,
|
|
|
|
className: 'danger center',
|
|
|
|
buttons: [t('confirmDelete'), t('confirmCancel')],
|
|
|
|
})
|
2017-12-09 15:25:44 +00:00
|
|
|
.then(({button}) => {
|
|
|
|
if (button === 0) {
|
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
|
|
|
|
2017-12-09 15:25:44 +00:00
|
|
|
expandTargets(event) {
|
|
|
|
event.preventDefault();
|
2017-04-05 13:14:59 +00:00
|
|
|
this.closest('.applies-to').classList.toggle('expanded');
|
|
|
|
},
|
|
|
|
|
2017-12-24 06:42:07 +00:00
|
|
|
loadFavicons({all = false} = {}) {
|
|
|
|
if (!installed.firstElementChild) return;
|
|
|
|
let favicons = [];
|
|
|
|
if (all) {
|
|
|
|
favicons = $$('img[data-src]', installed);
|
|
|
|
} else {
|
|
|
|
const {left, top} = installed.firstElementChild.getBoundingClientRect();
|
|
|
|
const x = Math.max(0, left);
|
|
|
|
const y = Math.max(0, top);
|
|
|
|
const first = document.elementFromPoint(x, y);
|
|
|
|
const lastOffset = first.offsetTop + window.innerHeight;
|
|
|
|
const numTargets = prefs.get('manage.newUI.targets');
|
|
|
|
let entry = first && first.closest('.entry') || installed.children[0];
|
|
|
|
while (entry && entry.offsetTop <= lastOffset) {
|
|
|
|
favicons.push(...$$('img', entry).slice(0, numTargets).filter(img => img.dataset.src));
|
|
|
|
entry = entry.nextElementSibling;
|
2017-04-05 13:14:59 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-24 06:42:07 +00:00
|
|
|
let i = 0;
|
|
|
|
for (const img of favicons) {
|
|
|
|
img.src = img.dataset.src;
|
|
|
|
delete img.dataset.src;
|
|
|
|
// loading too many icons at once will block the page while the new layout is recalculated
|
|
|
|
if (++i > 100) break;
|
|
|
|
}
|
|
|
|
if ($('img[data-src]', installed)) {
|
|
|
|
debounce(handleEvent.loadFavicons, 1, {all: true});
|
|
|
|
}
|
2017-04-16 11:24:49 +00:00
|
|
|
},
|
2017-11-27 11:51:51 +00:00
|
|
|
|
|
|
|
config(event, {styleMeta}) {
|
2017-12-18 06:57:00 +00:00
|
|
|
event.preventDefault();
|
2017-11-27 11:51:51 +00:00
|
|
|
configDialog(styleMeta);
|
|
|
|
},
|
2017-12-23 00:11:46 +00:00
|
|
|
|
|
|
|
lazyAddEntryTitle({type, target}) {
|
|
|
|
const cell = target.closest('h2.style-name');
|
|
|
|
if (cell) {
|
|
|
|
const link = $('.style-name-link', cell);
|
|
|
|
if (type === 'mouseover' && !link.title) {
|
|
|
|
debounce(handleEvent.addEntryTitle, 50, link);
|
|
|
|
} else {
|
|
|
|
debounce.unregister(handleEvent.addEntryTitle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addEntryTitle(link) {
|
|
|
|
const entry = link.closest('.entry');
|
|
|
|
link.title = [
|
|
|
|
{prop: 'installDate', name: 'dateInstalled'},
|
|
|
|
{prop: 'updateDate', name: 'dateUpdated'},
|
|
|
|
].map(({prop, name}) =>
|
|
|
|
t(name) + ': ' + (formatDate(entry.styleMeta[prop]) || '—')).join('\n');
|
|
|
|
}
|
2017-04-03 04:13:10 +00:00
|
|
|
});
|
2015-01-30 17:31:20 +00:00
|
|
|
|
|
|
|
|
2017-04-26 10:56:32 +00:00
|
|
|
function handleUpdate(style, {reason, method} = {}) {
|
|
|
|
let entry;
|
2017-06-27 20:13:11 +00:00
|
|
|
let oldEntry = $(ENTRY_ID_PREFIX + style.id);
|
2017-07-16 18:02:00 +00:00
|
|
|
if (oldEntry && method === 'styleUpdated') {
|
2017-04-26 15:35:00 +00:00
|
|
|
handleToggledOrCodeOnly();
|
2017-04-26 10:56:32 +00:00
|
|
|
}
|
|
|
|
entry = entry || createStyleElement({style});
|
2017-04-16 11:24:49 +00:00
|
|
|
if (oldEntry) {
|
2017-07-16 18:02:00 +00:00
|
|
|
if (oldEntry.styleNameLowerCase === entry.styleNameLowerCase) {
|
2017-04-16 11:24:49 +00:00
|
|
|
installed.replaceChild(entry, oldEntry);
|
2017-03-26 08:43:45 +00:00
|
|
|
} else {
|
2017-04-16 11:24:49 +00:00
|
|
|
oldEntry.remove();
|
2017-03-26 08:43:45 +00:00
|
|
|
}
|
2017-04-26 10:56:32 +00:00
|
|
|
}
|
2017-12-02 04:07:12 +00:00
|
|
|
if (reason === 'update' || reason === 'install' && entry.matches('.updatable')) {
|
|
|
|
handleUpdateInstalled(entry, reason);
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
2017-04-16 11:24:49 +00:00
|
|
|
filterAndAppend({entry});
|
2017-12-23 00:11:46 +00:00
|
|
|
sorter.update();
|
2017-07-16 18:02:00 +00:00
|
|
|
if (!entry.matches('.hidden') && reason !== 'import') {
|
2017-06-27 20:00:24 +00:00
|
|
|
animateElement(entry);
|
2017-11-29 22:13:13 +00:00
|
|
|
scrollElementIntoView(entry);
|
2017-04-05 13:14:59 +00:00
|
|
|
}
|
2017-04-26 10:56:32 +00:00
|
|
|
|
2017-04-26 15:35:00 +00:00
|
|
|
function handleToggledOrCodeOnly() {
|
2017-04-26 10:56:32 +00:00
|
|
|
const newStyleMeta = getStyleWithNoCode(style);
|
|
|
|
const diff = objectDiff(oldEntry.styleMeta, newStyleMeta);
|
2017-07-16 18:02:00 +00:00
|
|
|
if (diff.length === 0) {
|
2017-04-26 10:56:32 +00:00
|
|
|
// only code was modified
|
|
|
|
entry = oldEntry;
|
|
|
|
oldEntry = null;
|
|
|
|
}
|
2017-07-16 18:02:00 +00:00
|
|
|
if (diff.length === 1 && diff[0].key === 'enabled') {
|
2017-04-26 10:56:32 +00:00
|
|
|
oldEntry.classList.toggle('enabled', style.enabled);
|
|
|
|
oldEntry.classList.toggle('disabled', !style.enabled);
|
|
|
|
$$('.checker', oldEntry).forEach(el => (el.checked = style.enabled));
|
|
|
|
oldEntry.styleMeta = newStyleMeta;
|
|
|
|
entry = oldEntry;
|
|
|
|
oldEntry = null;
|
|
|
|
}
|
|
|
|
}
|
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-06-27 20:13:11 +00:00
|
|
|
const node = $(ENTRY_ID_PREFIX + id);
|
2017-03-21 01:32:38 +00:00
|
|
|
if (node) {
|
|
|
|
node.remove();
|
2017-04-16 11:24:49 +00:00
|
|
|
if (node.matches('.can-update')) {
|
|
|
|
const btnApply = $('#apply-all-updates');
|
|
|
|
btnApply.dataset.value = Number(btnApply.dataset.value) - 1;
|
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
2015-01-30 17:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-05 13:14:59 +00:00
|
|
|
function switchUI({styleOnly} = {}) {
|
2017-04-18 18:49:04 +00:00
|
|
|
const current = {};
|
|
|
|
const changed = {};
|
|
|
|
let someChanged = false;
|
|
|
|
// ensure the global option is processed first
|
|
|
|
for (const el of [$('#manage.newUI'), ...$$('[id^="manage.newUI."]')]) {
|
|
|
|
const id = el.id.replace(/^manage\.newUI\.?/, '') || 'enabled';
|
2017-07-16 18:02:00 +00:00
|
|
|
const value = el.type === 'checkbox' ? el.checked : Number(el.value);
|
|
|
|
const valueChanged = value !== newUI[id] && (id === 'enabled' || current.enabled);
|
2017-04-18 18:49:04 +00:00
|
|
|
current[id] = value;
|
|
|
|
changed[id] = valueChanged;
|
|
|
|
someChanged |= valueChanged;
|
|
|
|
}
|
2017-04-05 13:14:59 +00:00
|
|
|
|
2017-04-18 18:49:04 +00:00
|
|
|
if (!styleOnly && !someChanged) {
|
2017-04-05 13:14:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-18 18:49:04 +00:00
|
|
|
Object.assign(newUI, current);
|
2017-04-08 11:03:54 +00:00
|
|
|
newUI.renderClass();
|
2017-04-18 18:49:04 +00:00
|
|
|
installed.classList.toggle('has-favicons', newUI.favicons);
|
2017-04-05 13:14:59 +00:00
|
|
|
$('#style-overrides').textContent = `
|
|
|
|
.newUI .targets {
|
|
|
|
max-height: ${newUI.targets * 18}px;
|
|
|
|
}
|
2017-04-18 18:49:04 +00:00
|
|
|
` + (newUI.faviconsGray ? `
|
|
|
|
.newUI .target img {
|
|
|
|
-webkit-filter: grayscale(1);
|
|
|
|
filter: grayscale(1);
|
|
|
|
opacity: .25;
|
|
|
|
}
|
|
|
|
` : `
|
|
|
|
.newUI .target img {
|
|
|
|
-webkit-filter: none;
|
|
|
|
filter: none;
|
|
|
|
opacity: 1;
|
|
|
|
}
|
|
|
|
`);
|
2017-04-05 13:14:59 +00:00
|
|
|
|
2017-04-14 18:30:55 +00:00
|
|
|
if (styleOnly) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-18 18:49:04 +00:00
|
|
|
const missingFavicons = newUI.enabled && newUI.favicons && !$('.applies-to img');
|
|
|
|
if (changed.enabled || (missingFavicons && !createStyleElement.parts)) {
|
2017-07-19 08:19:15 +00:00
|
|
|
installed.textContent = '';
|
2017-04-05 13:14:59 +00:00
|
|
|
getStylesSafe().then(showStyles);
|
2017-04-14 18:30:55 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-18 18:49:04 +00:00
|
|
|
if (changed.targets) {
|
2017-04-10 08:26:30 +00:00
|
|
|
for (const targets of $$('.entry .targets')) {
|
|
|
|
const hasMore = targets.children.length > newUI.targets;
|
|
|
|
targets.parentElement.classList.toggle('has-more', hasMore);
|
|
|
|
}
|
2017-04-14 18:30:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (missingFavicons) {
|
|
|
|
getStylesSafe().then(styles => {
|
|
|
|
for (const style of styles) {
|
2017-06-27 20:13:11 +00:00
|
|
|
const entry = $(ENTRY_ID_PREFIX + style.id);
|
2017-04-14 18:30:55 +00:00
|
|
|
if (entry) {
|
2017-12-24 06:42:07 +00:00
|
|
|
createStyleTargetsElement({entry, style});
|
2017-04-14 18:30:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
debounce(handleEvent.loadFavicons);
|
|
|
|
});
|
|
|
|
return;
|
2017-04-05 13:14:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-16 11:24:49 +00:00
|
|
|
function rememberScrollPosition() {
|
|
|
|
history.replaceState({scrollY: window.scrollY}, document.title);
|
2017-03-26 08:43:45 +00:00
|
|
|
}
|
2017-08-22 10:28:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
function usePrefsDuringPageLoad() {
|
|
|
|
const observer = new MutationObserver(mutations => {
|
2017-08-22 14:22:15 +00:00
|
|
|
const adjustedNodes = [];
|
2017-08-22 10:28:16 +00:00
|
|
|
for (const mutation of mutations) {
|
|
|
|
for (const node of mutation.addedNodes) {
|
|
|
|
// [naively] assuming each element of addedNodes is a childless element
|
2017-11-29 16:05:47 +00:00
|
|
|
const key = node.dataset && node.dataset.pref || node.id;
|
|
|
|
const prefValue = key ? prefs.readOnlyValues[key] : undefined;
|
2017-08-22 10:28:16 +00:00
|
|
|
if (prefValue !== undefined) {
|
|
|
|
if (node.type === 'checkbox') {
|
|
|
|
node.checked = prefValue;
|
2017-11-29 16:05:47 +00:00
|
|
|
} else if (node.localName === 'details') {
|
|
|
|
node.open = prefValue;
|
2017-08-22 10:28:16 +00:00
|
|
|
} else {
|
|
|
|
node.value = prefValue;
|
|
|
|
}
|
2017-08-22 14:22:15 +00:00
|
|
|
if (node.adjustWidth) {
|
|
|
|
adjustedNodes.push(node);
|
|
|
|
}
|
2017-08-22 10:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-22 14:22:15 +00:00
|
|
|
if (adjustedNodes.length) {
|
|
|
|
observer.disconnect();
|
|
|
|
for (const node of adjustedNodes) {
|
|
|
|
node.adjustWidth();
|
|
|
|
}
|
|
|
|
startObserver();
|
|
|
|
}
|
2017-08-22 10:28:16 +00:00
|
|
|
});
|
2017-08-22 14:22:15 +00:00
|
|
|
function startObserver() {
|
|
|
|
observer.observe(document, {subtree: true, childList: true});
|
|
|
|
}
|
|
|
|
startObserver();
|
2017-08-22 10:28:16 +00:00
|
|
|
onDOMready().then(() => observer.disconnect());
|
|
|
|
}
|
2017-08-24 13:30:13 +00:00
|
|
|
|
|
|
|
|
2017-08-27 09:56:09 +00:00
|
|
|
// TODO: remove when these bugs are fixed in FF
|
2017-08-24 13:30:13 +00:00
|
|
|
function dieOnNullBackground() {
|
2017-08-27 09:56:09 +00:00
|
|
|
if (!FIREFOX || BG) {
|
2017-08-24 13:30:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-11-24 15:26:20 +00:00
|
|
|
sendMessage({method: 'healthCheck'}, health => {
|
2017-08-24 13:30:13 +00:00
|
|
|
if (health && !chrome.extension.getBackgroundPage()) {
|
|
|
|
onDOMready().then(() => {
|
2017-11-24 15:26:20 +00:00
|
|
|
sendMessage({method: 'getStyles'}, showStyles);
|
2017-08-24 13:30:13 +00:00
|
|
|
messageBox({
|
|
|
|
title: 'Stylus',
|
|
|
|
className: 'danger center',
|
|
|
|
contents: t('dysfunctionalBackgroundConnection'),
|
2017-09-02 15:36:32 +00:00
|
|
|
onshow: () => {
|
|
|
|
$('#message-box-close-icon').remove();
|
|
|
|
window.removeEventListener('keydown', messageBox.listeners.key, true);
|
|
|
|
}
|
2017-08-24 13:30:13 +00:00
|
|
|
});
|
|
|
|
document.documentElement.style.pointerEvents = 'none';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|