diff --git a/background/search-db.js b/background/search-db.js index 8cbf838b..bf907d06 100644 --- a/background/search-db.js +++ b/background/search-db.js @@ -26,7 +26,7 @@ matchUrl = query.slice(query.indexOf(':') + 1).trim(); if (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)) { diff --git a/background/style-manager.js b/background/style-manager.js index a0d11f6e..ce68c8c1 100644 --- a/background/style-manager.js +++ b/background/style-manager.js @@ -277,9 +277,42 @@ const styleManager = (() => { } function getStylesInfoByUrl(url) { - const sections = getSectionsByUrl(url); - return Object.keys(sections) - .map(k => getStyleWithNoCode(styles.get(Number(k)).data)); + // FIXME: do we want to cache this? Who would like to rapidly using popup + // or searching the DB with the same URL? + 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) { diff --git a/popup/popup.js b/popup/popup.js index 159739a4..d1df89dd 100644 --- a/popup/popup.js +++ b/popup/popup.js @@ -25,9 +25,10 @@ getActiveTab().then(tab => (tabURL = URLS.supported(url) ? url : '') && API.getStylesInfoByUrl(tabURL), onDOMready().then(initPopup), -])).then(([styles]) => { - showStyles(styles); -}); +])).then(([results]) => { + console.log(results); + showStyles(results.map(r => Object.assign(r.data, r))); +}).catch(console.error); msg.onExtension(onRuntimeMessage);