From 671b276532da42012e0ce3786fdcde8fab681e10 Mon Sep 17 00:00:00 2001 From: Rob Garrison Date: Sun, 31 Dec 2017 23:17:43 -0600 Subject: [PATCH] Attempt to get favicon for RegExp match (#321) show favicons for regexp() in applies-to column --- manage/manage.js | 65 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/manage/manage.js b/manage/manage.js index be62dccd..92018467 100644 --- a/manage/manage.js +++ b/manage/manage.js @@ -248,28 +248,9 @@ function createStyleTargetsElement({entry, style, iconsOnly}) { } else if (numTargets > 1) { container.appendChild(template.appliesToSeparator.cloneNode(true)); } - } else if (newUI.favicons && entry.parentElement) { - let favicon = ''; - if (type === 'domains') { - favicon = GET_FAVICON_URL + targetValue; - } else if (targetValue.startsWith('chrome-extension:')) { - favicon = OWN_ICON; - } else if (type !== 'regexps') { - favicon = targetValue.includes('://') && targetValue.match(/^.*?:\/\/([^/]+)/); - favicon = favicon ? GET_FAVICON_URL + favicon[1] : ''; - } - if (favicon) { - const img = element.children[0]; - if (!img || img.localName !== 'img') { - element.insertAdjacentElement('afterbegin', document.createElement('img')) - .dataset.src = favicon; - } else if ((img.dataset.src || img.src) !== favicon) { - img.src = ''; - img.dataset.src = favicon; - } - } } if (!iconsOnly) { + element.dataset.type = type; element.appendChild( document.createTextNode( (parts.decorations[type + 'Before'] || '') + @@ -307,10 +288,52 @@ function recreateStyleTargets({styles, iconsOnly = false} = {}) { }); } } - debounce(handleEvent.loadFavicons); + if (newUI.favicons) { + debounce(getFaviconImgSrc); + } }); } +function getFaviconImgSrc() { + const targets = $$('.target', installed); + const regexpRemoveNegativeLookAhead = /(\?!([^)]+\))|\(\?![\w(]+[^)]+[\w|)]+)/g; + // replace extra characters & all but the first group entry "(abc|def|ghi)xyz" => abcxyz + const regexpReplaceExtraCharacters = /[\\(]|((\|\w+)+\))/g; + const domainExt = 'com,org,co,net,im,io,edu,gov,biz,info,de,cn,uk,nl,eu,ru'.split(','); + const regexpMatchRegExp = new RegExp(`[\\w-]+[\\.(]+(${domainExt.join('|')})\\b`, 'g'); + const regexpMatchDomain = /^.*?:\/\/([^/]+)/; + for (const target of targets) { + const type = target.dataset.type; + const targetValue = target.textContent; + let favicon = ''; + if (type === 'domains') { + favicon = GET_FAVICON_URL + targetValue; + } else if (targetValue.includes('chrome-extension:') || targetValue.includes('moz-extension:')) { + favicon = OWN_ICON; + } else if (type === 'regexps') { + favicon = targetValue + .replace(regexpRemoveNegativeLookAhead, '') + .replace(regexpReplaceExtraCharacters, '') + .match(regexpMatchRegExp); + favicon = favicon ? GET_FAVICON_URL + favicon.shift() : ''; + } else { + favicon = targetValue.includes('://') && targetValue.match(regexpMatchDomain); + favicon = favicon ? GET_FAVICON_URL + favicon[1] : ''; + } + if (favicon) { + const img = target.children[0]; + if (!img || img.localName !== 'img') { + target.insertAdjacentElement('afterbegin', document.createElement('img')) + .dataset.src = favicon; + } else if ((img.dataset.src || img.src) !== favicon) { + img.src = ''; + img.dataset.src = favicon; + } + } + } + handleEvent.loadFavicons(); +} + Object.assign(handleEvent, {