stylus/popup/popup.js

604 lines
18 KiB
JavaScript
Raw Normal View History

2017-12-05 21:14:21 +00:00
/* global configDialog hotkeys */
'use strict';
let installed;
let tabURL;
const handleEvent = {};
const ENTRY_ID_PREFIX_RAW = 'style-';
const ENTRY_ID_PREFIX = '#' + ENTRY_ID_PREFIX_RAW;
toggleSideBorders();
2018-10-12 20:31:59 +00:00
if (prefs.get('popupui') === 1) {
document.documentElement.classList.add('classicUI');
} else {
document.documentElement.classList.add('iconUI');
}
if (prefs.get('popupclick') === 1) {
document.documentElement.classList.add('toggleUI');
} else {
document.documentElement.classList.add('directLinkUI');
}
if (!prefs.get('popup.stylesFirst')) {
document.documentElement.classList.add('actions-top');
}
getActiveTab().then(tab =>
FIREFOX && tab.url === 'about:blank' && tab.status === 'loading'
? getTabRealURLFirefox(tab)
: getTabRealURL(tab)
2018-01-01 17:02:49 +00:00
).then(url => Promise.all([
(tabURL = URLS.supported(url) ? url : '') &&
API.getStyles({
matchUrl: tabURL,
omitCode: !BG,
}),
onDOMready().then(initPopup),
])).then(([styles]) => {
showStyles(styles);
});
chrome.runtime.onMessage.addListener(onRuntimeMessage);
function onRuntimeMessage(msg) {
switch (msg.method) {
case 'styleAdded':
case 'styleUpdated':
if (msg.reason === 'editPreview') return;
2018-01-01 17:02:49 +00:00
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);
} else if ('popup.borders' in msg.prefs) {
toggleSideBorders(msg.prefs['popup.borders']);
}
break;
}
2017-12-10 01:03:04 +00:00
dispatchEvent(new CustomEvent(msg.method, {detail: msg}));
}
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';
}
function toggleSideBorders(state = prefs.get('popup.borders')) {
// runs before <body> is parsed
const style = document.documentElement.style;
if (CHROME >= 3167 && state) {
style.cssText +=
'border-left: 2px solid white !important;' +
'border-right: 2px solid white !important;';
} else if (style.cssText) {
style.borderLeft = style.borderRight = '';
}
}
2018-01-01 17:02:49 +00:00
function initPopup() {
installed = $('#installed');
2017-04-04 17:21:03 +00:00
setPopupWidth();
// action buttons
$('#disableAll').onchange = function () {
2018-10-12 20:31:59 +00:00
document.body.classList.toggle('disabled', this.checked);
};
$('#disable-all-icon').onclick = () => {
$('#disableAll').click();
};
$('#find-styles-icon').onclick = () => {
$('#find-styles-link').click();
};
2018-10-12 20:31:59 +00:00
setupLivePrefs();
Object.assign($('#popup-manage-button'), {
onclick: handleEvent.openManager,
onmouseup: handleEvent.openManager,
oncontextmenu: handleEvent.openManager,
});
2018-10-12 20:31:59 +00:00
Object.assign($('#popup-manage-icon'), {
onclick: handleEvent.openManager,
onmouseup: handleEvent.openManager,
oncontextmenu: handleEvent.openManager,
});
$('#find-external-icon').onclick = () => {
event.preventDefault();
$('input.toggle-inline').click()
};
$('#find-inline-icon').onclick = () => {
event.preventDefault();
$('input.toggle-inline').click()
};
$('#popup-options-button').onclick = () => {
chrome.runtime.openOptionsPage();
window.close();
};
2018-10-12 20:31:59 +00:00
$('#popup-options-icon').onclick = () => {
event.preventDefault();
chrome.runtime.openOptionsPage();
window.close();
};
$('#popup-wiki-button').onclick = handleEvent.openURLandHide;
2018-04-12 18:02:34 +00:00
2018-10-12 20:31:59 +00:00
$('#popup-wiki-icon').onclick = handleEvent.openURLandHide;
2018-01-01 17:02:49 +00:00
if (!tabURL) {
document.body.classList.add('blocked');
document.body.insertBefore(template.unavailableInfo, document.body.firstChild);
return;
}
getActiveTab().then(function ping(tab, retryCountdown = 10) {
sendMessage({tabId: tab.id, method: 'ping', frameId: 0}, pong => {
if (pong) {
return;
}
ignoreChromeError();
// FF and some Chrome forks (e.g. CentBrowser) implement tab-on-demand
// so we'll wait a bit to handle popup being invoked right after switching
if (retryCountdown > 0 && (
tab.status !== 'complete' ||
FIREFOX && tab.url === 'about:blank')) {
setTimeout(ping, 100, tab, --retryCountdown);
return;
}
const info = template.unreachableInfo;
if (FIREFOX && tabURL.startsWith(URLS.browserWebStore)) {
$('label', info).textContent = t('unreachableAMO');
const note = (FIREFOX < 59 ? t('unreachableAMOHintOldFF') : t('unreachableAMOHint')) +
(FIREFOX < 60 ? '' : '\n' + t('unreachableAMOHintNewFF'));
2018-10-12 23:27:57 +00:00
const renderToken = s => s[0] === '<' ? $create('b', tWordBreak(s.slice(1, -1))) : s;
const renderLine = line => $create('p', line.split(/(<.*?>)/).map(renderToken));
const noteNode = $create('fragment', note.split('\n').map(renderLine));
const target = $('p', info);
target.parentNode.insertBefore(noteNode, target);
target.remove();
}
document.body.classList.add('unreachable');
document.body.insertBefore(info, document.body.firstChild);
});
});
// Write new style links
const writeStyle = $('#write-style');
const matchTargets = document.createElement('span');
const matchWrapper = document.createElement('span');
matchWrapper.id = 'match';
matchWrapper.appendChild(matchTargets);
// For this URL
const urlLink = template.writeStyle.cloneNode(true);
Object.assign(urlLink, {
2018-01-01 17:02:49 +00:00
href: 'edit.html?url-prefix=' + encodeURIComponent(tabURL),
title: `url-prefix("${tabURL}")`,
textContent: prefs.get('popup.breadcrumbs.usePath')
2018-01-01 17:02:49 +00:00
? new URL(tabURL).pathname.slice(1)
2017-08-20 20:14:12 +00:00
// this&nbsp;URL
: t('writeStyleForURL').replace(/ /g, '\u00a0'),
onclick: handleEvent.openLink,
});
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
2018-01-01 17:02:49 +00:00
const domains = getDomains(tabURL);
for (const domain of domains) {
const numParts = domain.length - domain.replace(/\./g, '').length + 1;
// Don't include TLD
if (domains.length > 1 && numParts === 1) {
continue;
}
const domainLink = template.writeStyle.cloneNode(true);
Object.assign(domainLink, {
href: 'edit.html?domain=' + encodeURIComponent(domain),
textContent: numParts > 2 ? domain.split('.')[0] : domain,
title: `domain("${domain}")`,
onclick: handleEvent.openLink,
});
domainLink.setAttribute('subdomain', numParts > 1 ? 'true' : '');
matchTargets.appendChild(domainLink);
}
if (prefs.get('popup.breadcrumbs')) {
matchTargets.classList.add('breadcrumbs');
matchTargets.appendChild(matchTargets.removeChild(matchTargets.firstElementChild));
}
writeStyle.appendChild(matchWrapper);
2018-01-01 17:02:49 +00:00
function getDomains(url) {
let d = /.*?:\/*([^/:]+)|$/.exec(url)[1];
if (!d || url.startsWith('file:')) {
return [];
}
const domains = [d];
while (d.indexOf('.') !== -1) {
d = d.substring(d.indexOf('.') + 1);
domains.push(d);
}
return domains;
}
}
function showStyles(styles) {
if (!styles) {
return;
}
if (!styles.length) {
2017-07-19 08:22:16 +00:00
installed.appendChild(template.noStyles.cloneNode(true));
2017-12-10 01:03:04 +00:00
window.dispatchEvent(new Event('showStyles:done'));
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)
));
const container = document.createDocumentFragment();
2018-01-01 17:02:49 +00:00
styles.forEach(style => createStyleElement({style, container}));
installed.appendChild(container);
2018-01-01 17:02:49 +00:00
setTimeout(detectSloppyRegexps, 100, styles);
API.getStyles({
matchUrl: tabURL,
strictRegexp: false,
omitCode: true,
}).then(unscreenedStyles => {
for (const style of unscreenedStyles) {
if (!styles.find(({id}) => id === style.id)) {
createStyleElement({style, check: true});
}
2018-01-01 17:02:49 +00:00
}
window.dispatchEvent(new Event('showStyles:done'));
});
2018-10-12 20:31:59 +00:00
var reverseZebra = $('.entry:last-child:nth-of-type(odd)') !== null;
$('#installed').classList.toggle('reverse-stripe', reverseZebra);
}
function createStyleElement({
style,
2018-01-01 17:02:49 +00:00
check = false,
container = installed,
}) {
const entry = template.style.cloneNode(true);
entry.setAttribute('style-id', style.id);
Object.assign(entry, {
id: ENTRY_ID_PREFIX_RAW + style.id,
styleId: style.id,
2017-12-05 21:14:21 +00:00
styleIsUsercss: Boolean(style.usercssData),
className: entry.className + ' ' + (style.enabled ? 'enabled' : 'disabled'),
onmousedown: handleEvent.maybeEdit,
});
const checkbox = $('.checker', entry);
Object.assign(checkbox, {
id: ENTRY_ID_PREFIX_RAW + style.id,
checked: style.enabled,
onclick: handleEvent.toggle,
});
const editLink = $('.style-edit-link', entry);
Object.assign(editLink, {
href: editLink.getAttribute('href') + style.id,
onclick: handleEvent.openLink,
});
2018-10-12 20:31:59 +00:00
const editLinkAccess = $('.style-edit-link-accessibility', entry);
Object.assign(editLinkAccess, {
href: editLinkAccess.getAttribute('href') + style.id,
onclick: handleEvent.openLink,
});
const styleName = $('.style-name', entry);
Object.assign(styleName, {
htmlFor: ENTRY_ID_PREFIX_RAW + style.id,
onclick: handleEvent.name,
});
styleName.checkbox = checkbox;
styleName.appendChild(document.createTextNode(style.name));
setTimeout((el = styleName) => {
if (el.scrollWidth > el.clientWidth + 1) {
el.title = el.textContent;
}
});
2017-12-05 21:14:21 +00:00
const config = $('.configure', entry);
if (!style.usercssData && style.updateUrl && style.updateUrl.includes('?') && style.url) {
config.href = style.url;
config.target = '_blank';
config.title = t('configureStyleOnHomepage');
config.dataset.sendMessage = JSON.stringify({method: 'openSettings'});
$('use', config).attributes['xlink:href'].nodeValue = '#svg-icon-config-uso';
2017-12-05 21:14:21 +00:00
} else if (!style.usercssData || !Object.keys(style.usercssData.vars || {}).length) {
config.style.display = 'none';
}
$('.enable', entry).onclick = handleEvent.toggle;
$('.disable', entry).onclick = handleEvent.toggle;
$('.delete', entry).onclick = handleEvent.delete;
2017-12-05 21:14:21 +00:00
$('.configure', entry).onclick = handleEvent.configure;
2018-01-01 17:02:49 +00:00
if (check) detectSloppyRegexps([style]);
const oldElement = $(ENTRY_ID_PREFIX + style.id);
2018-10-12 21:47:08 +00:00
if (oldElement && oldElement.contains(document.activeElement)) {
// preserve the focused element inside
const {className} = document.activeElement;
oldElement.parentNode.replaceChild(entry, oldElement);
// we're not using $() since className may contain multiple tokens
const el = entry.getElementsByClassName(className)[0];
if (el) el.focus();
} else if (oldElement) {
oldElement.parentNode.replaceChild(entry, oldElement);
} else {
container.appendChild(entry);
}
}
Object.assign(handleEvent, {
2017-03-25 03:18:14 +00:00
getClickedStyleId(event) {
return (handleEvent.getClickedStyleElement(event) || {}).styleId;
},
getClickedStyleElement(event) {
return event.target.closest('.entry');
},
name(event) {
2018-10-12 20:31:59 +00:00
if (prefs.get('popupclick') === 1) {
this.checkbox.dispatchEvent(new MouseEvent('click'));
} else {
const entry = handleEvent.getClickedStyleElement(event);
$('.style-edit-link', entry).click();
}
2017-03-25 03:18:14 +00:00
event.preventDefault();
},
2017-03-25 03:18:14 +00:00
toggle(event) {
2018-01-01 17:02:49 +00:00
API.saveStyle({
id: handleEvent.getClickedStyleId(event),
2018-01-01 17:02:49 +00:00
enabled: this.matches('.enable') || this.checked,
2017-03-25 03:18:14 +00:00
});
},
2017-03-25 03:18:14 +00:00
delete(event) {
2018-01-01 17:02:49 +00:00
const entry = handleEvent.getClickedStyleElement(event);
const id = entry.styleId;
const box = $('#confirm');
box.dataset.display = true;
box.style.cssText = '';
2018-01-01 17:02:49 +00:00
$('b', box).textContent = $('.style-name', entry).textContent;
2018-10-12 20:31:59 +00:00
$('button[data-cmd="ok"]', box).focus();
$('button[data-cmd="ok"]', box).onclick = () => confirm(true);
$('button[data-cmd="cancel"]', box).onclick = () => confirm(false);
$('a[data-cmd="ok"]', box).focus();
$('a[data-cmd="ok"]', box).onclick = () => confirm(true); event.preventDefault();
$('a[data-cmd="cancel"]', box).onclick = () => confirm(false); event.preventDefault();
window.onkeydown = event => {
2017-04-18 18:56:32 +00:00
const keyCode = event.keyCode || event.which;
if (!event.shiftKey && !event.ctrlKey && !event.altKey && !event.metaKey
2017-07-16 18:02:00 +00:00
&& (keyCode === 13 || keyCode === 27)) {
event.preventDefault();
2017-07-16 18:02:00 +00:00
confirm(keyCode === 13);
2017-03-25 03:18:14 +00:00
}
};
function confirm(ok) {
window.onkeydown = null;
animateElement(box, {
className: 'lights-on',
onComplete: () => (box.dataset.display = false),
});
2018-01-01 17:02:49 +00:00
if (ok) API.deleteStyle({id});
}
},
2017-03-25 03:18:14 +00:00
2017-12-05 21:14:21 +00:00
configure(event) {
const {styleId, styleIsUsercss} = handleEvent.getClickedStyleElement(event);
if (styleIsUsercss) {
2018-01-01 17:02:49 +00:00
API.getStyles({id: styleId}).then(([style]) => {
2017-12-05 21:14:21 +00:00
hotkeys.setState(false);
2018-01-05 10:26:11 +00:00
configDialog(style).then(() => {
2017-12-05 21:14:21 +00:00
hotkeys.setState(true);
});
});
} else {
handleEvent.openURLandHide.call(this, event);
2017-12-05 21:14:21 +00:00
}
},
indicator(event) {
const entry = handleEvent.getClickedStyleElement(event);
const info = template.regexpProblemExplanation.cloneNode(true);
$.remove('#' + info.id);
$$('a', info).forEach(el => (el.onclick = handleEvent.openURLandHide));
$$('button', info).forEach(el => (el.onclick = handleEvent.closeExplanation));
entry.appendChild(info);
},
closeExplanation() {
$('#regexp-explanation').remove();
},
2017-03-25 03:18:14 +00:00
openLink(event) {
2017-11-25 13:24:07 +00:00
if (!chrome.windows || !prefs.get('openEditInWindow', false)) {
handleEvent.openURLandHide.call(this, event);
return;
}
event.preventDefault();
chrome.windows.create(
Object.assign({
url: this.href
}, prefs.get('windowPosition', {}))
);
close();
},
maybeEdit(event) {
if (!(
2017-07-16 18:02:00 +00:00
event.button === 0 && (event.ctrlKey || event.metaKey) ||
event.button === 1 ||
event.button === 2)) {
return;
}
// open an editor on middleclick
if (event.target.matches('.entry, .style-name, .style-edit-link')) {
this.onmouseup = () => $('.style-edit-link', this).click();
this.oncontextmenu = event => event.preventDefault();
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;
}
},
openURLandHide(event) {
event.preventDefault();
getActiveTab()
.then(activeTab => API.openURL({
2018-10-12 20:31:59 +00:00
url: this.hasAttribute('data-href') ? this.dataset.href : this.href,
index: activeTab.index + 1,
message: tryJSONparse(this.dataset.sendMessage),
}))
.then(window.close);
},
openManager(event) {
event.preventDefault();
if (!this.eventHandled) {
this.eventHandled = true;
this.dataset.href += event.shiftKey || event.button === 2 ?
'?url=' + encodeURIComponent(tabURL) : '';
handleEvent.openURLandHide.call(this, event);
}
},
});
function handleUpdate(style) {
if ($(ENTRY_ID_PREFIX + style.id)) {
2018-01-01 17:02:49 +00:00
createStyleElement({style, check: true});
return;
}
2018-01-01 17:02:49 +00:00
if (!tabURL) return;
// Add an entry when a new style for the current url is installed
2018-01-01 17:02:49 +00:00
API.getStyles({
matchUrl: tabURL,
stopOnFirst: true,
omitCode: true,
}).then(([style]) => {
if (style) {
document.body.classList.remove('blocked');
$$.remove('.blocked-info, #no-styles');
createStyleElement({style, check: true});
}
});
}
function handleDelete(id) {
$.remove(ENTRY_ID_PREFIX + id);
if (!$('.entry')) {
installed.appendChild(template.noStyles.cloneNode(true));
}
2018-10-12 20:31:59 +00:00
var reverseZebra = $('.entry:last-child:nth-of-type(odd)') !== null;
$('#installed').classList.toggle('reverse-stripe', reverseZebra);
}
2018-01-01 17:02:49 +00:00
function detectSloppyRegexps(styles) {
API.detectSloppyRegexps({
matchUrl: tabURL,
ids: styles.map(({id}) => id),
}).then(results => {
for (const {id, applied, skipped, hasInvalidRegexps} of results) {
const entry = $(ENTRY_ID_PREFIX + id);
if (!entry) continue;
if (!applied) {
entry.classList.add('not-applied');
$('.style-name', entry).title = t('styleNotAppliedRegexpProblemTooltip');
}
if (skipped || hasInvalidRegexps) {
entry.classList.toggle('regexp-partial', Boolean(skipped));
entry.classList.toggle('regexp-invalid', Boolean(hasInvalidRegexps));
const indicator = template.regexpProblemIndicator.cloneNode(true);
indicator.appendChild(document.createTextNode(entry.skipped || '!'));
indicator.onclick = handleEvent.indicator;
$('.main-controls', entry).appendChild(indicator);
}
}
2018-01-01 17:02:49 +00:00
});
}
function getTabRealURLFirefox(tab) {
// wait for FF tab-on-demand to get a real URL (initially about:blank), 5 sec max
return new Promise(resolve => {
function onNavigation({tabId, url, frameId}) {
if (tabId === tab.id && frameId === 0) {
detach();
resolve(url);
}
}
function detach(timedOut) {
if (timedOut) {
resolve(tab.url);
} else {
debounce.unregister(detach);
}
chrome.webNavigation.onBeforeNavigate.removeListener(onNavigation);
chrome.webNavigation.onCommitted.removeListener(onNavigation);
chrome.tabs.onRemoved.removeListener(detach);
chrome.tabs.onReplaced.removeListener(detach);
}
chrome.webNavigation.onBeforeNavigate.addListener(onNavigation);
chrome.webNavigation.onCommitted.addListener(onNavigation);
chrome.tabs.onRemoved.addListener(detach);
chrome.tabs.onReplaced.addListener(detach);
debounce(detach, 5000, {timedOut: true});
});
}