2017-06-10 11:08:03 +00:00
|
|
|
/* global retranslateCSS */
|
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-06-27 20:13:11 +00:00
|
|
|
const ENTRY_ID_PREFIX_RAW = 'style-';
|
|
|
|
const ENTRY_ID_PREFIX = '#' + ENTRY_ID_PREFIX_RAW;
|
|
|
|
|
2017-08-18 11:41:27 +00:00
|
|
|
getActiveTab().then(tab =>
|
|
|
|
FIREFOX && tab.url === 'about:blank' && tab.status === 'loading'
|
|
|
|
? getTabRealURLFirefox(tab)
|
|
|
|
: getTabRealURL(tab)
|
|
|
|
).then(url => {
|
2017-08-16 17:40:24 +00:00
|
|
|
tabURL = URLS.supported(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-04-11 10:51:40 +00:00
|
|
|
chrome.runtime.onMessage.addListener(onRuntimeMessage);
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
function onRuntimeMessage(msg) {
|
2017-03-31 08:46:18 +00:00
|
|
|
switch (msg.method) {
|
|
|
|
case 'styleAdded':
|
|
|
|
case 'styleUpdated':
|
2017-05-23 19:28:45 +00:00
|
|
|
// notifyAllTabs sets msg.style's code to null so we have to get the actual style
|
|
|
|
// because we analyze its code in detectSloppyRegexps
|
|
|
|
handleUpdate(BG.cachedStyles.byId.get(msg.style.id));
|
2017-03-31 08:46:18 +00:00
|
|
|
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-11 10:51:40 +00:00
|
|
|
}
|
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
|
2017-07-16 16:49:31 +00:00
|
|
|
$('#disableAll').onchange = function () {
|
2017-04-11 11:22:00 +00:00
|
|
|
installed.classList.toggle('disabled', this.checked);
|
|
|
|
};
|
2017-04-21 16:39:34 +00:00
|
|
|
setupLivePrefs();
|
2017-04-09 06:43:51 +00:00
|
|
|
|
2017-04-03 04:13:10 +00:00
|
|
|
$('#find-styles-link').onclick = handleEvent.openURLandHide;
|
2017-08-22 16:29:26 +00:00
|
|
|
Object.assign($('#popup-manage-button'), {
|
|
|
|
onclick: handleEvent.openManager,
|
|
|
|
onmouseup: handleEvent.openManager,
|
|
|
|
oncontextmenu: handleEvent.openManager,
|
|
|
|
});
|
2017-04-09 06:43:51 +00:00
|
|
|
|
|
|
|
$('#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');
|
2017-08-27 09:56:09 +00:00
|
|
|
document.body.insertBefore(template.unavailableInfo, document.body.firstChild);
|
2017-03-21 01:32:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-18 11:41:27 +00:00
|
|
|
getActiveTab().then(function ping(tab, retryCountdown = 10) {
|
2017-04-12 09:57:40 +00:00
|
|
|
chrome.tabs.sendMessage(tab.id, {method: 'ping'}, {frameId: 0}, pong => {
|
2017-08-18 11:41:27 +00:00
|
|
|
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
|
2017-08-23 06:46:36 +00:00
|
|
|
if (
|
|
|
|
retryCountdown > 0 && (
|
|
|
|
tab.status !== 'complete' ||
|
|
|
|
FIREFOX && tab.url === 'about:blank'
|
|
|
|
)
|
|
|
|
) {
|
2017-08-18 11:41:27 +00:00
|
|
|
setTimeout(ping, 100, tab, --retryCountdown);
|
|
|
|
} else {
|
2017-04-12 09:57:40 +00:00
|
|
|
document.body.classList.add('unreachable');
|
2017-08-27 09:56:09 +00:00
|
|
|
document.body.insertBefore(template.unreachableInfo, document.body.firstChild);
|
2017-04-12 09:57:40 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
// 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
|
2017-04-11 10:51:40 +00:00
|
|
|
const domains = BG.getDomains(url);
|
2017-03-26 02:30:59 +00:00
|
|
|
for (const domain of domains) {
|
2017-08-14 18:15:29 +00:00
|
|
|
const numParts = domain.length - domain.replace(/\./g, '').length + 1;
|
2017-03-21 01:32:38 +00:00
|
|
|
// Don't include TLD
|
2017-08-14 18:15:29 +00:00
|
|
|
if (domains.length > 1 && numParts === 1) {
|
2017-03-21 01:32:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const domainLink = template.writeStyle.cloneNode(true);
|
|
|
|
Object.assign(domainLink, {
|
|
|
|
href: 'edit.html?domain=' + encodeURIComponent(domain),
|
2017-08-14 18:15:29 +00:00
|
|
|
textContent: numParts > 2 ? domain.split('.')[0] : domain,
|
2017-03-21 01:32:38 +00:00
|
|
|
title: `domain("${domain}")`,
|
2017-04-03 04:13:10 +00:00
|
|
|
onclick: handleEvent.openLink,
|
2017-03-21 01:32:38 +00:00
|
|
|
});
|
2017-08-14 18:15:29 +00:00
|
|
|
domainLink.setAttribute('subdomain', numParts > 1 ? 'true' : '');
|
2017-03-21 01:32:38 +00:00
|
|
|
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-07-19 08:22:16 +00:00
|
|
|
installed.textContent = '';
|
|
|
|
installed.appendChild(template.noStyles.cloneNode(true));
|
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-06-27 20:13:11 +00:00
|
|
|
id: ENTRY_ID_PREFIX_RAW + 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-17 16:48:39 +00:00
|
|
|
onmousedown: handleEvent.maybeEdit,
|
2017-03-21 01:32:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const checkbox = $('.checker', entry);
|
|
|
|
Object.assign(checkbox, {
|
2017-06-27 20:13:11 +00:00
|
|
|
id: ENTRY_ID_PREFIX_RAW + style.id,
|
2017-03-21 01:32:38 +00:00
|
|
|
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, {
|
2017-06-27 20:13:11 +00:00
|
|
|
htmlFor: ENTRY_ID_PREFIX_RAW + 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-06-27 12:15:49 +00:00
|
|
|
invokeOrPostpone(!postponeDetect, detectSloppyRegexps, {entry, style});
|
2017-03-30 23:18:41 +00:00
|
|
|
|
2017-06-27 20:13:11 +00:00
|
|
|
const oldElement = $(ENTRY_ID_PREFIX + style.id);
|
2017-03-30 23:18:41 +00:00
|
|
|
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-04-11 10:51:40 +00:00
|
|
|
saveStyleSafe({
|
2017-04-09 18:33:17 +00:00
|
|
|
id: handleEvent.getClickedStyleId(event),
|
2017-07-16 18:02:00 +00:00
|
|
|
enabled: this.type === 'checkbox' ? this.checked : this.matches('.enable'),
|
2017-03-25 03:18:14 +00:00
|
|
|
});
|
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-04-11 10:51:40 +00:00
|
|
|
$('b', box).textContent = (BG.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 => {
|
2017-04-18 18:56:32 +00:00
|
|
|
const keyCode = event.keyCode || event.which;
|
2017-03-25 19:50:37 +00:00
|
|
|
if (!event.shiftKey && !event.ctrlKey && !event.altKey && !event.metaKey
|
2017-07-16 18:02:00 +00:00
|
|
|
&& (keyCode === 13 || keyCode === 27)) {
|
2017-03-25 19:50:37 +00:00
|
|
|
event.preventDefault();
|
2017-07-16 18:02:00 +00:00
|
|
|
confirm(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) {
|
2017-04-11 10:51:40 +00:00
|
|
|
deleteStyleSafe({id}).then(() => {
|
2017-03-25 19:50:37 +00:00
|
|
|
// 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-11 10:51:40 +00:00
|
|
|
closeExplanation() {
|
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();
|
|
|
|
},
|
|
|
|
|
2017-04-17 16:48:39 +00:00
|
|
|
maybeEdit(event) {
|
|
|
|
if (!(
|
2017-07-16 18:02:00 +00:00
|
|
|
event.button === 0 && (event.ctrlKey || event.metaKey) ||
|
|
|
|
event.button === 1 ||
|
|
|
|
event.button === 2)) {
|
2017-04-03 04:13:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// open an editor on middleclick
|
|
|
|
if (event.target.matches('.entry, .style-name, .style-edit-link')) {
|
2017-04-17 16:48:39 +00:00
|
|
|
this.onmouseup = () => $('.style-edit-link', this).click();
|
2017-04-26 15:44:23 +00:00
|
|
|
this.oncontextmenu = event => event.preventDefault();
|
2017-04-03 04:13:10 +00:00
|
|
|
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
|
|
|
},
|
2017-08-22 16:29:26 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
},
|
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-06-27 20:13:11 +00:00
|
|
|
if ($(ENTRY_ID_PREFIX + style.id)) {
|
2017-03-30 23:18:41 +00:00
|
|
|
createStyleElement({style});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Add an entry when a new style for the current url is installed
|
2017-04-11 10:51:40 +00:00
|
|
|
if (tabURL && BG.getApplicableSections({style, matchUrl: tabURL, stopOnFirst: true}).length) {
|
2017-04-12 13:56:41 +00:00
|
|
|
document.body.classList.remove('blocked');
|
2017-08-27 09:56:09 +00:00
|
|
|
$$('.blocked-info').forEach(el => el.remove());
|
2017-03-30 23:18:41 +00:00
|
|
|
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-06-27 20:13:11 +00:00
|
|
|
$$(ENTRY_ID_PREFIX + id).forEach(el => el.remove());
|
2017-03-30 23:18:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
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}) {
|
2017-06-25 10:16:23 +00:00
|
|
|
// make sure all regexps are compiled
|
|
|
|
const rxCache = BG.cachedStyles.regexps;
|
2017-08-16 17:41:50 +00:00
|
|
|
let hasRegExp = false;
|
2017-06-25 10:16:23 +00:00
|
|
|
for (const section of style.sections) {
|
|
|
|
for (const regexp of section.regexps) {
|
2017-08-16 17:41:50 +00:00
|
|
|
hasRegExp = true;
|
2017-06-25 10:16:23 +00:00
|
|
|
for (let pass = 1; pass <= 2; pass++) {
|
2017-07-16 18:02:00 +00:00
|
|
|
const cacheKey = pass === 1 ? regexp : BG.SLOPPY_REGEXP_PREFIX + regexp;
|
2017-06-25 10:16:23 +00:00
|
|
|
if (!rxCache.has(cacheKey)) {
|
|
|
|
// according to CSS4 @document specification the entire URL must match
|
2017-07-16 18:02:00 +00:00
|
|
|
const anchored = pass === 1 ? '^(?:' + regexp + ')$' : '^' + regexp + '$';
|
2017-08-08 16:33:43 +00:00
|
|
|
// create in the bg context to avoid leaking of "dead objects"
|
|
|
|
const rx = BG.tryRegExp(anchored);
|
2017-06-25 10:16:23 +00:00
|
|
|
rxCache.set(cacheKey, rx || false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-16 17:41:50 +00:00
|
|
|
if (!hasRegExp) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-30 23:18:41 +00:00
|
|
|
const {
|
2017-04-11 10:51:40 +00:00
|
|
|
appliedSections =
|
|
|
|
BG.getApplicableSections({style, matchUrl: tabURL}),
|
|
|
|
wannabeSections =
|
|
|
|
BG.getApplicableSections({style, matchUrl: tabURL, strictRegexp: false}),
|
2017-03-30 23:18:41 +00:00
|
|
|
} = style;
|
|
|
|
|
|
|
|
entry.hasInvalidRegexps = wannabeSections.some(section =>
|
2017-06-25 10:16:23 +00:00
|
|
|
section.regexps.some(rx => !rxCache.has(rx)));
|
2017-03-30 23:18:41 +00:00
|
|
|
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
|
|
|
}
|
2017-08-18 11:41:27 +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});
|
|
|
|
});
|
|
|
|
}
|