From 36e13f88f00a3b01074ad4f41a1d1e056ee9c561 Mon Sep 17 00:00:00 2001 From: eight Date: Fri, 12 Oct 2018 16:59:23 +0800 Subject: [PATCH] Add: return excluded/sloppy state in getStylesInfoByUrl --- background/search-db.js | 2 +- background/style-manager.js | 39 ++++++++++++++++++++++++++++++++++--- popup/popup.js | 7 ++++--- 3 files changed, 41 insertions(+), 7 deletions(-) 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);