stylus/popup/popup-exclusions.js

208 lines
6.3 KiB
JavaScript
Raw Normal View History

2018-01-25 01:42:02 +00:00
/*
global messageBox
global exclusions
*/
'use strict';
const popupExclusions = (() => {
// return matches on url ending to prevent duplicates in the exclusion list
// e.g. http://test.com and http://test.com/* are equivalent
// this function would return ['', '/*']
function exclusionExists(array, value) {
const match = [];
['', '*', '/', '/*'].forEach(ending => {
if (array.includes(value + ending)) {
match.push(ending);
}
});
return match;
}
/* Modal in Popup.html */
2018-03-07 17:54:05 +00:00
function processURL(url) {
2018-01-25 01:42:02 +00:00
const results = [];
const protocol = url.match(/\w+:\/\//);
2018-04-19 18:00:27 +00:00
// remove ending '/', protocol, hash & search strings
const parts = url.replace(/\/$/, '').replace(/(\w+:\/\/|[#?].*$)/g, '').split('/');
2018-01-25 01:42:02 +00:00
const domain = parts[0].split('.');
/*
Domain: a.b.com
Domain: b.com
Prefix: https://a.b.com
Prefix: https://a.b.com/current
Prefix: https://a.b.com/current/page
*/
while (parts.length > 1) {
results.push([t('excludedPrefix'), protocol + parts.join('/')]);
parts.pop();
}
while (domain.length > 1) {
results.push([t('excludedDomain'), domain.join('.')]);
domain.shift();
}
2018-03-07 17:54:05 +00:00
return results.reverse();
}
2018-04-19 18:00:27 +00:00
function shortenURL(text) {
const len = text.length;
let prefix = '\u2026';
// account for URL that end with a '/'
let index = (text.endsWith('/') ? text.substring(0, len - 1) : text).lastIndexOf('/');
if (index < 0 || len - index < 2) {
index = 0;
prefix = '';
}
return prefix + text.substring(index, len);
}
2018-03-07 17:54:05 +00:00
function createOption(option) {
// ["Domain/Prefix", "{url}"]
return $create('option', {
value: option[1],
title: option[1],
2018-04-19 18:00:27 +00:00
textContent: `${option[0]}: ${shortenURL(option[1])}`
2018-03-07 17:54:05 +00:00
});
}
2018-04-19 18:00:27 +00:00
function getMultiOptions({select, selectedOnly} = {}) {
return [...select.children].reduce((acc, opt) => {
if (selectedOnly && opt.selected || !selectedOnly) {
acc.push(opt.value);
}
return acc;
}, []);
}
function updatePopupContent(url) {
2018-03-07 17:54:05 +00:00
const options = processURL(url);
2018-04-19 18:00:27 +00:00
const renderBin = document.createDocumentFragment();
options.map(option => renderBin.appendChild(createOption(option)));
$('#popup-exclusions').textContent = '';
$('#popup-exclusions').appendChild(renderBin);
2018-01-25 01:42:02 +00:00
}
2018-03-07 17:54:05 +00:00
function getIframeURLs(style) {
getActiveTab().then(tab => {
if (tab && tab.status === 'complete') {
chrome.webNavigation.getAllFrames({
tabId: tab.id
}, frames => {
2018-04-19 18:00:27 +00:00
const urls = frames.reduce((acc, frame) => [...acc, ...processURL(frame.url)], []);
2018-03-07 17:54:05 +00:00
updateSelections(style, urls);
});
}
});
}
2018-04-19 18:00:27 +00:00
function selectExclusions(exclusions) {
const select = $('#exclude select');
const excludes = Object.keys(exclusions || {});
[...select.children].forEach(option => {
if (exclusionExists(excludes, option.value).length) {
option.selected = true;
}
});
}
2018-03-07 17:54:05 +00:00
function updateSelections(style, newOptions = []) {
2018-04-19 18:00:27 +00:00
const wrap = $('#exclude');
const select = $('select', wrap);
2018-03-07 17:54:05 +00:00
if (newOptions.length) {
const currentOptions = [...select.children].map(opt => opt.value);
newOptions.forEach(opt => {
if (!currentOptions.includes(opt[1])) {
select.appendChild(createOption(opt));
2018-04-19 18:00:27 +00:00
// newOptions may have duplicates (e.g. multiple iframes from same source)
currentOptions.push(opt[1]);
2018-03-07 17:54:05 +00:00
}
});
select.size = select.children.length;
2018-04-19 18:00:27 +00:00
// hide select, then calculate & adjust height
select.style.height = '0';
document.body.style.height = `${select.scrollHeight + wrap.offsetHeight}px`;
select.style.height = '';
2018-03-07 17:54:05 +00:00
}
2018-04-19 18:00:27 +00:00
selectExclusions(style.exclusions);
}
function isExcluded(matchUrl, exclusions = {}) {
const values = Object.values(exclusions);
return values.length && values.some(exclude => tryRegExp(exclude).test(matchUrl));
2018-03-07 17:54:05 +00:00
}
2018-04-19 18:00:27 +00:00
function openPopupDialog(entry, tabURL) {
const style = entry.styleMeta;
updateSelections(style, updatePopupContent(tabURL));
getIframeURLs(style);
const box = $('#exclude');
box.dataset.display = true;
box.style.cssText = '';
$('strong', box).textContent = style.name;
$('[data-cmd="ok"]', box).focus();
$('[data-cmd="ok"]', box).onclick = () => confirm(true);
$('[data-cmd="cancel"]', box).onclick = () => confirm(false);
window.onkeydown = event => {
const keyCode = event.keyCode || event.which;
if (!event.shiftKey && !event.ctrlKey && !event.altKey && !event.metaKey
&& (keyCode === 13 || keyCode === 27)) {
event.preventDefault();
confirm(keyCode === 13);
2018-01-25 01:42:02 +00:00
}
2018-04-19 18:00:27 +00:00
};
function confirm(ok) {
window.onkeydown = null;
animateElement(box, {
className: 'lights-on',
onComplete: () => (box.dataset.display = false),
});
2018-04-25 22:09:45 +00:00
document.body.style.height = '';
2018-07-22 14:15:09 +00:00
const excluded = isExcluded(tabURL, style.exclusions);
2018-04-19 18:00:27 +00:00
if (ok) {
handlePopupSave(style);
entry.styleMeta = style;
2018-07-22 14:15:09 +00:00
entry.classList.toggle('excluded', excluded);
2018-04-19 18:00:27 +00:00
}
2018-07-22 14:15:09 +00:00
notifyAllTabs({method: 'exclusionsUpdated', style, id: entry.styleId, affects: true, excluded});
2018-04-19 18:00:27 +00:00
}
return Promise.resolve();
2018-01-25 01:42:02 +00:00
}
2018-04-19 18:00:27 +00:00
function handlePopupSave(style) {
2018-04-25 23:11:37 +00:00
if (typeof style.exclusions === 'undefined') {
style.exclusions = {};
}
2018-01-25 01:42:02 +00:00
const current = Object.keys(style.exclusions);
const select = $('#popup-exclusions', messageBox.element);
2018-04-19 18:00:27 +00:00
const all = getMultiOptions({select});
const selected = getMultiOptions({select, selectedOnly: true});
2018-01-25 01:42:02 +00:00
// Add exclusions
selected.forEach(value => {
let exists = exclusionExists(current, value);
if (!exists.length) {
style.exclusions[value] = exclusions.createRegExp(value);
exists = [''];
}
exists.forEach(ending => {
const index = all.indexOf(value + ending);
if (index > -1) {
all.splice(index, 1);
}
});
});
// Remove exclusions (unselected in popup modal)
all.forEach(value => {
exclusionExists(current, value).forEach(ending => {
delete style.exclusions[value + ending];
});
});
2018-04-19 18:00:27 +00:00
exclusions.save({
id: style.id,
exclusionList: style.exclusions
});
2018-01-25 01:42:02 +00:00
}
2018-04-19 18:00:27 +00:00
return {openPopupDialog, selectExclusions, isExcluded};
2018-01-25 01:42:02 +00:00
})();