style url as regex instead of plain starts with

This commit is contained in:
alexadam 2017-07-10 22:36:16 +03:00
parent 84dfab759f
commit 126ce6f039
3 changed files with 26 additions and 12 deletions

View file

@ -35,8 +35,8 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
// TODO move defaultStyles in a different file/location ?
var defaultStyles = [
{
title: 'reddit',
url: 'reddit.com',
title: 'Reddit Comments',
url: 'reddit\.com\/r\/[^\/]+\/comments',
style: `.class {
display: none;
}

View file

@ -115,7 +115,7 @@ function showEditor() {
urlLabelHolder.className = 'cssEditor-field-label-holder';
var urlLabel = document.createElement('label');
urlLabel.className = 'cssEditor-field-label';
urlLabel.innerText = 'URL Starts With';
urlLabel.innerText = 'URL Regex'; // TODO addd link to regex tutorial
urlLabelHolder.appendChild(urlLabel);
editorHolder.appendChild(urlLabelHolder);

View file

@ -19,6 +19,9 @@ function createStyleList(styles) {
existingStyles.removeChild(existingStyles.lastChild);
}
// if multiple URL regexes match, select the longest one
var allMatchingStyles = [];
for (var i = 0; i < styles.length; i++) {
var listItem = document.createElement('option');
listItem.id = 'option_' + i;
@ -26,20 +29,31 @@ function createStyleList(styles) {
listItem.value = 'option_' + i;
listItem.innerText = styles[i].title;
if (!foundMatchingUrl) {
currentUrl = currentUrl.replace(/(http[s]?:\/\/|www\.)/gi, '').toLowerCase();
var styleUrl = styles[i].url.replace(/(http[s]?:\/\/|www\.)/gi, '').toLowerCase();
currentUrl = currentUrl.replace(/(http[s]?:\/\/|www\.)/i, '').toLowerCase();
var styleUrl = styles[i].url;
var styleUrlRegex = new RegExp('.*' + styleUrl + '.*', 'i');
if (currentUrl.startsWith(styleUrl)) {
listItem.selected = 'selected';
foundMatchingUrl = true;
currentStyle = styles[i];
setCurrentStyle(currentStyle);
}
if (styleUrlRegex.test(currentUrl)) {
allMatchingStyles.push({
index: i,
length: styleUrl.length
});
}
existingStyles.appendChild(listItem);
}
if (allMatchingStyles.length >= 1) {
allMatchingStyles.sort(function (a, b) {
console.log(a.length, b.length);
return b.length - a.length;
});
var selStyle = allMatchingStyles[0];
var tmpListItemElem = document.getElementById('option_' + selStyle.index);
tmpListItemElem.selected = 'selected';
currentStyle = styles[selStyle.index];
setCurrentStyle(currentStyle);
}
});
}