Add: return excluded/sloppy state in getStylesInfoByUrl

This commit is contained in:
eight 2018-10-12 16:59:23 +08:00
parent f6ce78f55b
commit 36e13f88f0
3 changed files with 41 additions and 7 deletions

View File

@ -26,7 +26,7 @@
matchUrl = query.slice(query.indexOf(':') + 1).trim(); matchUrl = query.slice(query.indexOf(':') + 1).trim();
if (matchUrl) { if (matchUrl) {
return styleManager.getStylesInfoByUrl(matchUrl) return styleManager.getStylesInfoByUrl(matchUrl)
.then(styles => styles.map(style => style.id)); .then(results => results.map(r => r.data.id));
} }
} }
if (query.startsWith('/') && /^\/(.+?)\/([gimsuy]*)$/.test(query)) { if (query.startsWith('/') && /^\/(.+?)\/([gimsuy]*)$/.test(query)) {

View File

@ -277,9 +277,42 @@ const styleManager = (() => {
} }
function getStylesInfoByUrl(url) { function getStylesInfoByUrl(url) {
const sections = getSectionsByUrl(url); // FIXME: do we want to cache this? Who would like to rapidly using popup
return Object.keys(sections) // or searching the DB with the same URL?
.map(k => getStyleWithNoCode(styles.get(Number(k)).data)); const result = [];
for (const style of styles.values()) {
let excluded = false;
let sloppy = false;
let sectionMatched = false;
const match = urlMatchStyle(url, style.data);
if (match === false) {
continue;
}
if (match === 'excluded') {
excluded = true;
}
for (const section of style.data.sections) {
if (styleCodeEmpty(section.code)) {
continue;
}
const match = urlMatchSection(url, section);
if (match) {
if (match === 'sloppy') {
sloppy = true;
}
sectionMatched = true;
break;
}
}
if (sectionMatched) {
result.push({
data: getStyleWithNoCode(style.data),
excluded,
sloppy
});
}
}
return result;
} }
function getSectionsByUrl(url, filter) { function getSectionsByUrl(url, filter) {

View File

@ -25,9 +25,10 @@ getActiveTab().then(tab =>
(tabURL = URLS.supported(url) ? url : '') && (tabURL = URLS.supported(url) ? url : '') &&
API.getStylesInfoByUrl(tabURL), API.getStylesInfoByUrl(tabURL),
onDOMready().then(initPopup), onDOMready().then(initPopup),
])).then(([styles]) => { ])).then(([results]) => {
showStyles(styles); console.log(results);
}); showStyles(results.map(r => Object.assign(r.data, r)));
}).catch(console.error);
msg.onExtension(onRuntimeMessage); msg.onExtension(onRuntimeMessage);