Correctly omit @namespace-only code in popup + speed up

This commit is contained in:
tophf 2017-03-28 11:24:31 +03:00
parent 7d9a6b5a3f
commit 1c2c14d231

View File

@ -24,6 +24,12 @@ function getDatabase(ready, error) {
} }
const RX_NAMESPACE = new RegExp([/[\s\r\n]*/,
/(@namespace[\s\r\n]+(?:[^\s\r\n]+[\s\r\n]+)?url\(http:\/\/.*?\);)/,
/[\s\r\n]*/].map(rx => rx.source).join(''), 'g');
const RX_CSS_COMMENTS = /\/\*[\s\S]*?\*\//g;
// Let manage/popup/edit reuse background page variables // Let manage/popup/edit reuse background page variables
// Note, only 'var'-declared variables are visible from another extension page // Note, only 'var'-declared variables are visible from another extension page
// eslint-disable-next-line no-var // eslint-disable-next-line no-var
@ -427,49 +433,40 @@ function getType(o) {
return 'undefined'; return 'undefined';
} }
const namespacePattern = /^\s*(@namespace[^;]+;\s*)+$/;
function getApplicableSections(style, url) { function getApplicableSections(style, url) {
const sections = []; const sections = [];
checkingSections:
for (const section of style.sections) { for (const section of style.sections) {
if (sectionAppliesToUrl(section, url)) {
sections.push(section);
}
}
// ignore if it's just namespaces
if (sections.length == 1 && namespacePattern.test(sections[0].code)) {
return [];
}
return sections;
}
function sectionAppliesToUrl(section, url) {
// only http, https, file, ftp, and chrome-extension://OWN_EXTENSION_ID allowed // only http, https, file, ftp, and chrome-extension://OWN_EXTENSION_ID allowed
if (!url.startsWith('http') if (!url.startsWith('http')
&& !url.startsWith('ftp') && !url.startsWith('ftp')
&& !url.startsWith('file') && !url.startsWith('file')
&& !url.startsWith(OWN_ORIGIN)) { && !url.startsWith(OWN_ORIGIN)) {
return false; continue checkingSections;
} }
if (section.urls.length == 0 if (section.urls.length == 0
&& section.domains.length == 0 && section.domains.length == 0
&& section.urlPrefixes.length == 0 && section.urlPrefixes.length == 0
&& section.regexps.length == 0) { && section.regexps.length == 0) {
return true; sections.push(section);
continue checkingSections;
} }
if (section.urls.indexOf(url) != -1) { if (section.urls.indexOf(url) != -1) {
return true; sections.push(section);
continue checkingSections;
} }
for (const urlPrefix of section.urlPrefixes) { for (const urlPrefix of section.urlPrefixes) {
if (url.startsWith(urlPrefix)) { if (url.startsWith(urlPrefix)) {
return true; sections.push(section);
continue checkingSections;
} }
} }
const urlDomains = cachedStyles.urlDomains.get(url) || getDomains(url); const urlDomains = cachedStyles.urlDomains.get(url) || getDomains(url);
for (const domain of urlDomains) { for (const domain of urlDomains) {
if (section.domains.indexOf(domain) != -1) { if (section.domains.indexOf(domain) != -1) {
return true; sections.push(section);
continue checkingSections;
} }
} }
for (const regexp of section.regexps) { for (const regexp of section.regexps) {
@ -487,10 +484,19 @@ function sectionAppliesToUrl(section, url) {
} }
} }
if (rx.test(url)) { if (rx.test(url)) {
return true; sections.push(section);
continue checkingSections;
} }
} }
return false; }
// ignore @namespace-only results
if (sections.length == 1
&& sections[0].code
&& sections[0].code.indexOf('@namespace') >= 0
&& sections[0].code.replace(RX_CSS_COMMENTS, '').replace(RX_NAMESPACE, '').trim() == '') {
return [];
}
return sections;
} }