2017-03-30 23:18:41 +00:00
|
|
|
/* global SLOPPY_REGEXP_PREFIX, compileStyleRegExps */
|
2017-03-26 02:30:59 +00:00
|
|
|
'use strict';
|
2017-03-16 13:36:33 +00:00
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
let installed;
|
2017-03-30 23:18:41 +00:00
|
|
|
let tabURL;
|
2017-04-03 04:13:10 +00:00
|
|
|
const handleEvent = {};
|
2015-01-30 17:28:05 +00:00
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
getActiveTabRealURL().then(url => {
|
2017-03-30 23:18:41 +00:00
|
|
|
tabURL = RX_SUPPORTED_URLS.test(url) ? url : '';
|
2017-03-21 01:32:38 +00:00
|
|
|
Promise.all([
|
2017-03-30 23:18:41 +00:00
|
|
|
tabURL && getStylesSafe({matchUrl: tabURL}),
|
|
|
|
onDOMready().then(() => {
|
|
|
|
initPopup(tabURL);
|
|
|
|
}),
|
|
|
|
]).then(([styles]) => {
|
|
|
|
showStyles(styles);
|
|
|
|
});
|
2017-03-21 01:32:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
chrome.runtime.onMessage.addListener(msg => {
|
2017-03-31 08:46:18 +00:00
|
|
|
switch (msg.method) {
|
|
|
|
case 'styleAdded':
|
|
|
|
case 'styleUpdated':
|
|
|
|
handleUpdate(msg.style);
|
|
|
|
break;
|
|
|
|
case 'styleDeleted':
|
|
|
|
handleDelete(msg.id);
|
|
|
|
break;
|
|
|
|
case 'prefChanged':
|
|
|
|
if ('popup.stylesFirst' in msg.prefs) {
|
|
|
|
const stylesFirst = msg.prefs['popup.stylesFirst'];
|
|
|
|
const actions = $('body > .actions');
|
|
|
|
const before = stylesFirst ? actions : actions.nextSibling;
|
|
|
|
document.body.insertBefore(installed, before);
|
2017-04-04 17:21:03 +00:00
|
|
|
} else if ('popupWidth' in msg.prefs) {
|
|
|
|
setPopupWidth(msg.prefs.popupWidth);
|
2017-03-31 08:46:18 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-04-04 17:21:03 +00:00
|
|
|
function setPopupWidth(width = prefs.get('popupWidth')) {
|
|
|
|
document.body.style.width =
|
|
|
|
Math.max(200, Math.min(800, width)) + 'px';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
function initPopup(url) {
|
|
|
|
installed = $('#installed');
|
|
|
|
|
2017-04-04 17:21:03 +00:00
|
|
|
setPopupWidth();
|
2015-03-12 23:00:23 +00:00
|
|
|
|
2017-03-25 21:47:40 +00:00
|
|
|
// force Chrome to resize the popup
|
2017-04-09 06:43:51 +00:00
|
|
|
if (!FIREFOX) {
|
|
|
|
document.body.style.height = '10px';
|
|
|
|
document.documentElement.style.height = '10px';
|
|
|
|
}
|
2017-03-25 21:47:40 +00:00
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
// action buttons
|
|
|
|
$('#disableAll').onchange = () =>
|
|
|
|
installed.classList.toggle('disabled', prefs.get('disableAll'));
|
|
|
|
setupLivePrefs(['disableAll']);
|
2017-04-09 06:43:51 +00:00
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
$('#find-styles-link').onclick = handleEvent.openURLandHide;
|
2017-04-09 06:43:51 +00:00
|
|
|
$('#popup-manage-button').onclick = handleEvent.openURLandHide;
|
|
|
|
|
|
|
|
$('#popup-options-button').onclick = () => {
|
|
|
|
chrome.runtime.openOptionsPage();
|
|
|
|
window.close();
|
|
|
|
};
|
|
|
|
|
|
|
|
const shortcutsButton = $('#popup-shortcuts-button');
|
|
|
|
shortcutsButton.dataset.href = URLS.configureCommands;
|
|
|
|
shortcutsButton.onclick = handleEvent.openURLandHide;
|
2017-03-21 01:32:38 +00:00
|
|
|
|
|
|
|
if (!prefs.get('popup.stylesFirst')) {
|
|
|
|
document.body.insertBefore(
|
|
|
|
$('body > .actions'),
|
|
|
|
installed);
|
|
|
|
}
|
|
|
|
|
|
|
|
// find styles link
|
|
|
|
$('#find-styles a').href =
|
|
|
|
'https://userstyles.org/styles/browse/all/' +
|
|
|
|
encodeURIComponent(url.startsWith('file:') ? 'file:' : url);
|
|
|
|
|
|
|
|
if (!url) {
|
|
|
|
document.body.classList.add('blocked');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write new style links
|
|
|
|
const writeStyle = $('#write-style');
|
|
|
|
const matchTargets = document.createElement('span');
|
2017-04-01 04:37:25 +00:00
|
|
|
const matchWrapper = document.createElement('span');
|
|
|
|
matchWrapper.id = 'match';
|
|
|
|
matchWrapper.appendChild(matchTargets);
|
2017-03-21 01:32:38 +00:00
|
|
|
|
|
|
|
// For this URL
|
|
|
|
const urlLink = template.writeStyle.cloneNode(true);
|
|
|
|
Object.assign(urlLink, {
|
|
|
|
href: 'edit.html?url-prefix=' + encodeURIComponent(url),
|
|
|
|
title: `url-prefix("${url}")`,
|
|
|
|
textContent: prefs.get('popup.breadcrumbs.usePath')
|
|
|
|
? new URL(url).pathname.slice(1)
|
|
|
|
: t('writeStyleForURL').replace(/ /g, '\u00a0'), // this URL
|
2017-04-03 04:13:10 +00:00
|
|
|
onclick: handleEvent.openLink,
|
2017-03-21 01:32:38 +00:00
|
|
|
});
|
|
|
|
if (prefs.get('popup.breadcrumbs')) {
|
|
|
|
urlLink.onmouseenter =
|
|
|
|
urlLink.onfocus = () => urlLink.parentNode.classList.add('url()');
|
|
|
|
urlLink.onmouseleave =
|
|
|
|
urlLink.onblur = () => urlLink.parentNode.classList.remove('url()');
|
|
|
|
}
|
|
|
|
matchTargets.appendChild(urlLink);
|
|
|
|
|
|
|
|
// For domain
|
|
|
|
const domains = getDomains(url);
|
2017-03-26 02:30:59 +00:00
|
|
|
for (const domain of domains) {
|
2017-03-21 01:32:38 +00:00
|
|
|
// Don't include TLD
|
|
|
|
if (domains.length > 1 && !domain.includes('.')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const domainLink = template.writeStyle.cloneNode(true);
|
|
|
|
Object.assign(domainLink, {
|
|
|
|
href: 'edit.html?domain=' + encodeURIComponent(domain),
|
|
|
|
textContent: domain,
|
|
|
|
title: `domain("${domain}")`,
|
2017-04-03 04:13:10 +00:00
|
|
|
onclick: handleEvent.openLink,
|
2017-03-21 01:32:38 +00:00
|
|
|
});
|
|
|
|
domainLink.setAttribute('subdomain', domain.substring(0, domain.indexOf('.')));
|
|
|
|
matchTargets.appendChild(domainLink);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prefs.get('popup.breadcrumbs')) {
|
|
|
|
matchTargets.classList.add('breadcrumbs');
|
|
|
|
matchTargets.appendChild(matchTargets.removeChild(matchTargets.firstElementChild));
|
|
|
|
}
|
2017-04-01 04:37:25 +00:00
|
|
|
writeStyle.appendChild(matchWrapper);
|
2015-03-23 18:56:11 +00:00
|
|
|
}
|
2012-08-20 01:14:33 +00:00
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2012-08-20 01:14:33 +00:00
|
|
|
function showStyles(styles) {
|
2017-03-30 23:18:41 +00:00
|
|
|
if (!styles) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
if (!styles.length) {
|
2017-03-25 03:04:24 +00:00
|
|
|
installed.innerHTML = template.noStyles.outerHTML;
|
2017-03-30 23:18:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const enabledFirst = prefs.get('popup.enabledFirst');
|
|
|
|
styles.sort((a, b) => (
|
|
|
|
enabledFirst && a.enabled !== b.enabled
|
|
|
|
? !(a.enabled < b.enabled) ? -1 : 1
|
|
|
|
: a.name.localeCompare(b.name)
|
|
|
|
));
|
|
|
|
|
|
|
|
let postponeDetect = false;
|
|
|
|
const t0 = performance.now();
|
|
|
|
const container = document.createDocumentFragment();
|
|
|
|
for (const style of styles) {
|
|
|
|
createStyleElement({style, container, postponeDetect});
|
|
|
|
postponeDetect = postponeDetect || performance.now() - t0 > 100;
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
2017-03-30 23:18:41 +00:00
|
|
|
installed.appendChild(container);
|
|
|
|
|
|
|
|
getStylesSafe({matchUrl: tabURL, strictRegexp: false})
|
|
|
|
.then(unscreenedStyles => {
|
|
|
|
for (const unscreened of unscreenedStyles) {
|
|
|
|
if (!styles.includes(unscreened)) {
|
|
|
|
postponeDetect = postponeDetect || performance.now() - t0 > 100;
|
|
|
|
createStyleElement({
|
|
|
|
style: Object.assign({appliedSections: [], postponeDetect}, unscreened),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2012-08-20 01:14:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-03-30 23:18:41 +00:00
|
|
|
function createStyleElement({
|
|
|
|
style,
|
|
|
|
container = installed,
|
|
|
|
postponeDetect,
|
|
|
|
}) {
|
2017-03-21 01:32:38 +00:00
|
|
|
const entry = template.style.cloneNode(true);
|
|
|
|
entry.setAttribute('style-id', style.id);
|
|
|
|
Object.assign(entry, {
|
2017-03-30 23:18:41 +00:00
|
|
|
id: 'style-' + style.id,
|
2017-03-21 01:32:38 +00:00
|
|
|
styleId: style.id,
|
2017-03-25 03:04:24 +00:00
|
|
|
className: entry.className + ' ' + (style.enabled ? 'enabled' : 'disabled'),
|
2017-04-03 04:13:10 +00:00
|
|
|
onmousedown: handleEvent.middleClick,
|
|
|
|
onauxclick: handleEvent.middleClick,
|
2017-03-21 01:32:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const checkbox = $('.checker', entry);
|
|
|
|
Object.assign(checkbox, {
|
|
|
|
id: 'style-' + style.id,
|
|
|
|
checked: style.enabled,
|
2017-04-03 04:13:10 +00:00
|
|
|
onclick: handleEvent.toggle,
|
2017-03-21 01:32:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const editLink = $('.style-edit-link', entry);
|
|
|
|
Object.assign(editLink, {
|
|
|
|
href: editLink.getAttribute('href') + style.id,
|
2017-04-03 04:13:10 +00:00
|
|
|
onclick: handleEvent.openLink,
|
2017-03-21 01:32:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const styleName = $('.style-name', entry);
|
|
|
|
Object.assign(styleName, {
|
|
|
|
htmlFor: 'style-' + style.id,
|
2017-04-03 04:13:10 +00:00
|
|
|
onclick: handleEvent.name,
|
2017-03-21 01:32:38 +00:00
|
|
|
});
|
|
|
|
styleName.checkbox = checkbox;
|
|
|
|
styleName.appendChild(document.createTextNode(style.name));
|
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
$('.enable', entry).onclick = handleEvent.toggle;
|
|
|
|
$('.disable', entry).onclick = handleEvent.toggle;
|
|
|
|
$('.delete', entry).onclick = handleEvent.delete;
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-03-30 23:18:41 +00:00
|
|
|
if (postponeDetect) {
|
|
|
|
setTimeout(detectSloppyRegexps, 0, {entry, style});
|
|
|
|
} else {
|
|
|
|
detectSloppyRegexps({entry, style});
|
|
|
|
}
|
|
|
|
|
|
|
|
const oldElement = $('#style-' + style.id);
|
|
|
|
if (oldElement) {
|
|
|
|
oldElement.parentNode.replaceChild(entry, oldElement);
|
|
|
|
} else {
|
|
|
|
container.appendChild(entry);
|
|
|
|
}
|
2012-08-20 01:14:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
Object.assign(handleEvent, {
|
2017-03-25 03:18:14 +00:00
|
|
|
|
2017-04-09 18:33:17 +00:00
|
|
|
getClickedStyleId(event) {
|
|
|
|
return (handleEvent.getClickedStyleElement(event) || {}).styleId;
|
|
|
|
},
|
|
|
|
|
|
|
|
getClickedStyleElement(event) {
|
|
|
|
return event.target.closest('.entry');
|
|
|
|
},
|
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
name(event) {
|
2017-03-25 03:18:14 +00:00
|
|
|
this.checkbox.click();
|
|
|
|
event.preventDefault();
|
2017-04-03 04:13:10 +00:00
|
|
|
},
|
2017-03-25 03:18:14 +00:00
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
toggle(event) {
|
2017-03-25 03:18:14 +00:00
|
|
|
saveStyle({
|
2017-04-09 18:33:17 +00:00
|
|
|
id: handleEvent.getClickedStyleId(event),
|
2017-03-25 03:18:14 +00:00
|
|
|
enabled: this.type == 'checkbox' ? this.checked : this.matches('.enable'),
|
|
|
|
});
|
2017-04-03 04:13:10 +00:00
|
|
|
},
|
2017-03-25 03:18:14 +00:00
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
delete(event) {
|
2017-04-09 18:33:17 +00:00
|
|
|
const id = handleEvent.getClickedStyleId(event);
|
2017-03-25 19:50:37 +00:00
|
|
|
const box = $('#confirm');
|
|
|
|
box.dataset.display = true;
|
|
|
|
box.style.cssText = '';
|
2017-03-30 23:18:41 +00:00
|
|
|
$('b', box).textContent = (cachedStyles.byId.get(id) || {}).name;
|
2017-03-25 19:50:37 +00:00
|
|
|
$('[data-cmd="ok"]', box).onclick = () => confirm(true);
|
|
|
|
$('[data-cmd="cancel"]', box).onclick = () => confirm(false);
|
|
|
|
window.onkeydown = event => {
|
|
|
|
if (!event.shiftKey && !event.ctrlKey && !event.altKey && !event.metaKey
|
|
|
|
&& (event.keyCode == 13 || event.keyCode == 27)) {
|
|
|
|
event.preventDefault();
|
|
|
|
confirm(event.keyCode == 13);
|
2017-03-25 03:18:14 +00:00
|
|
|
}
|
2017-03-25 19:50:37 +00:00
|
|
|
};
|
|
|
|
function confirm(ok) {
|
|
|
|
window.onkeydown = null;
|
|
|
|
animateElement(box, {className: 'lights-on'})
|
2017-03-26 02:30:59 +00:00
|
|
|
.then(() => (box.dataset.display = false));
|
2017-03-25 19:50:37 +00:00
|
|
|
if (ok) {
|
|
|
|
deleteStyle(id).then(() => {
|
|
|
|
// update view with 'No styles installed for this site' message
|
|
|
|
if (!installed.children.length) {
|
|
|
|
showStyles([]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-04-03 04:13:10 +00:00
|
|
|
},
|
2017-03-25 03:18:14 +00:00
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
indicator(event) {
|
2017-04-09 18:33:17 +00:00
|
|
|
const entry = handleEvent.getClickedStyleElement(event);
|
2017-03-30 23:18:41 +00:00
|
|
|
const info = template.regexpProblemExplanation.cloneNode(true);
|
|
|
|
$$('#' + info.id).forEach(el => el.remove());
|
2017-04-03 04:13:10 +00:00
|
|
|
$$('a', info).forEach(el => (el.onclick = handleEvent.openURLandHide));
|
|
|
|
$$('button', info).forEach(el => (el.onclick = handleEvent.closeExplanation));
|
2017-03-30 23:18:41 +00:00
|
|
|
entry.appendChild(info);
|
2017-04-03 04:13:10 +00:00
|
|
|
},
|
2017-03-30 23:18:41 +00:00
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
closeExplanation(event) {
|
2017-03-30 23:18:41 +00:00
|
|
|
$('#regexp-explanation').remove();
|
2017-04-03 04:13:10 +00:00
|
|
|
},
|
2017-03-25 03:18:14 +00:00
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
openLink(event) {
|
|
|
|
if (!prefs.get('openEditInWindow', false)) {
|
|
|
|
handleEvent.openURLandHide.call(this, event);
|
|
|
|
return;
|
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
event.preventDefault();
|
2017-04-03 04:13:10 +00:00
|
|
|
chrome.windows.create(
|
|
|
|
Object.assign({
|
|
|
|
url: this.href
|
|
|
|
}, prefs.get('windowPosition', {}))
|
|
|
|
);
|
|
|
|
close();
|
|
|
|
},
|
|
|
|
|
|
|
|
middleClick(event) {
|
|
|
|
if (event.button != 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// open an editor on middleclick
|
|
|
|
if (event.target.matches('.entry, .style-name, .style-edit-link')) {
|
|
|
|
$('.style-edit-link', this).click();
|
|
|
|
event.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// prevent the popup being opened in a background tab
|
|
|
|
// when an irrelevant link was accidentally clicked
|
|
|
|
if (event.target.closest('a')) {
|
|
|
|
event.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
openURLandHide(event) {
|
|
|
|
event.preventDefault();
|
2017-04-09 06:43:51 +00:00
|
|
|
openURL({url: this.href || this.dataset.href})
|
|
|
|
.then(window.close);
|
2017-04-03 04:13:10 +00:00
|
|
|
},
|
|
|
|
});
|
2012-08-20 01:14:33 +00:00
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2012-08-20 01:14:33 +00:00
|
|
|
function handleUpdate(style) {
|
2017-03-30 23:18:41 +00:00
|
|
|
if ($('#style-' + style.id)) {
|
|
|
|
createStyleElement({style});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Add an entry when a new style for the current url is installed
|
|
|
|
if (tabURL && getApplicableSections({style, matchUrl: tabURL, stopOnFirst: true}).length) {
|
|
|
|
$('#unavailable').style.display = 'none';
|
|
|
|
createStyleElement({style});
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
2012-08-20 01:14:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2012-08-20 01:14:33 +00:00
|
|
|
function handleDelete(id) {
|
2017-03-30 23:18:41 +00:00
|
|
|
$$('#style-' + id).forEach(el => el.remove());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
According to CSS4 @document specification the entire URL must match.
|
|
|
|
Stylish-for-Chrome implemented it incorrectly since the very beginning.
|
|
|
|
We'll detect styles that abuse the bug by finding the sections that
|
|
|
|
would have been applied by Stylish but not by us as we follow the spec.
|
|
|
|
Additionally we'll check for invalid regexps.
|
|
|
|
*/
|
|
|
|
function detectSloppyRegexps({entry, style}) {
|
|
|
|
const {
|
|
|
|
appliedSections = getApplicableSections({style, matchUrl: tabURL}),
|
|
|
|
wannabeSections = getApplicableSections({style, matchUrl: tabURL, strictRegexp: false}),
|
|
|
|
} = style;
|
|
|
|
|
|
|
|
compileStyleRegExps({style, compileAll: true});
|
|
|
|
entry.hasInvalidRegexps = wannabeSections.some(section =>
|
|
|
|
section.regexps.some(rx => !cachedStyles.regexps.has(rx)));
|
|
|
|
entry.sectionsSkipped = wannabeSections.length - appliedSections.length;
|
|
|
|
|
|
|
|
if (!appliedSections.length) {
|
|
|
|
entry.classList.add('not-applied');
|
|
|
|
$('.style-name', entry).title = t('styleNotAppliedRegexpProblemTooltip');
|
|
|
|
}
|
|
|
|
if (entry.sectionsSkipped || entry.hasInvalidRegexps) {
|
|
|
|
entry.classList.toggle('regexp-partial', entry.sectionsSkipped);
|
|
|
|
entry.classList.toggle('regexp-invalid', entry.hasInvalidRegexps);
|
|
|
|
const indicator = template.regexpProblemIndicator.cloneNode(true);
|
|
|
|
indicator.appendChild(document.createTextNode(entry.sectionsSkipped || '!'));
|
2017-04-03 04:13:10 +00:00
|
|
|
indicator.onclick = handleEvent.indicator;
|
2017-03-30 23:18:41 +00:00
|
|
|
$('.main-controls', entry).appendChild(indicator);
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
2015-03-24 14:07:59 +00:00
|
|
|
}
|