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 ? // TODO move defaultStyles in a different file/location ?
var defaultStyles = [ var defaultStyles = [
{ {
title: 'reddit', title: 'Reddit Comments',
url: 'reddit.com', url: 'reddit\.com\/r\/[^\/]+\/comments',
style: `.class { style: `.class {
display: none; display: none;
} }

View file

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

View file

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