fix for issue #37

This commit is contained in:
Alex Adam 2020-03-09 18:30:47 +02:00
parent 9a75e6e92a
commit 0eb26ddef4

View file

@ -228,40 +228,59 @@ function prepareStyles(tab, includeStyle, appliedStyles, callback) {
let currentUrl = tab[0].url; let currentUrl = tab[0].url;
let currentStyle = null; let currentStyle = null;
if (styles && styles.length > 0) { if (!styles) {
let allMatchingStyles = []; callback(appliedStyles)
}
for (let i = 0; i < styles.length; i++) { if (styles.length === 0) {
currentUrl = currentUrl.replace(/(http[s]?:\/\/|www\.)/i, '').toLowerCase(); callback(appliedStyles)
let styleUrl = styles[i].url; }
let styleUrlRegex = null;
try { let allMatchingStyles = [];
styleUrlRegex = new RegExp(styleUrl, 'i');
} catch (e) {
}
if (styleUrlRegex && styleUrlRegex.test(currentUrl)) { for (let i = 0; i < styles.length; i++) {
allMatchingStyles.push({ currentUrl = currentUrl.replace(/(http[s]?:\/\/|www\.)/i, '').toLowerCase();
index: i, let styleUrl = styles[i].url;
length: styleUrl.length let styleUrlRegex = null;
});
} try {
styleUrlRegex = new RegExp(styleUrl, 'i');
} catch (e) {
} }
if (allMatchingStyles.length >= 1) { if (styleUrlRegex && styleUrlRegex.test(currentUrl)) {
allMatchingStyles.sort((a, b) => b.length - a.length); allMatchingStyles.push({
let selStyle = allMatchingStyles[0]; index: i,
currentStyle = styles[selStyle.index]; length: styleUrl.length
});
if (currentStyle && currentStyle.style) {
chrome.tabs.insertCSS(tab[0].id, {code: currentStyle.style});
appliedStyles.push(currentStyle);
}
} }
} }
callback(appliedStyles) if (allMatchingStyles.length === 0) {
callback(appliedStyles)
}
allMatchingStyles.sort((a, b) => b.length - a.length);
let selStyle = allMatchingStyles[0];
if (!selStyle) {
callback(appliedStyles)
}
currentStyle = styles[selStyle.index];
if (!currentStyle) {
callback(appliedStyles)
}
if (!currentStyle.style) {
callback(appliedStyles)
}
chrome.tabs.insertCSS(tab[0].id, { code: currentStyle.style }, () => {
appliedStyles.push(currentStyle);
callback(appliedStyles)
});
}); });
} }