From f85d4de39b3ee2636c44ca46b3ee92045e43696a Mon Sep 17 00:00:00 2001 From: eight Date: Sun, 14 Oct 2018 18:59:29 +0800 Subject: [PATCH] Fix: handle styleAdded message in popup --- background/search-db.js | 2 +- background/style-manager.js | 12 +++++++----- popup/popup.js | 21 ++++++++------------- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/background/search-db.js b/background/search-db.js index 78a721f9..75318304 100644 --- a/background/search-db.js +++ b/background/search-db.js @@ -25,7 +25,7 @@ if (/^url:/i.test(query)) { matchUrl = query.slice(query.indexOf(':') + 1).trim(); if (matchUrl) { - return styleManager.getStylesByUrl(matchUrl, true) + return styleManager.getStylesByUrl(matchUrl) .then(results => results.map(r => r.data.id)); } } diff --git a/background/style-manager.js b/background/style-manager.js index f58ef822..316e43c2 100644 --- a/background/style-manager.js +++ b/background/style-manager.js @@ -277,15 +277,17 @@ const styleManager = (() => { } // get styles matching a URL, including sloppy regexps and excluded items. - function getStylesByUrl(url, noCode = false) { + function getStylesByUrl(url, id = null) { // FIXME: do we want to cache this? Who would like to open popup rapidly // or search the DB with the same URL? const result = []; - for (const style of styles.values()) { + const datas = !id ? styles.values.map(s => s.data) : + styles.has(id) ? [styles.get(id).data] : []; + for (const data of datas) { let excluded = false; let sloppy = false; let sectionMatched = false; - const match = urlMatchStyle(url, style.data); + const match = urlMatchStyle(url, data); // TODO: enable this when the function starts returning false // if (match === false) { // continue; @@ -293,7 +295,7 @@ const styleManager = (() => { if (match === 'excluded') { excluded = true; } - for (const section of style.data.sections) { + for (const section of data.sections) { if (styleCodeEmpty(section.code)) { continue; } @@ -308,7 +310,7 @@ const styleManager = (() => { } if (sectionMatched) { result.push({ - data: noCode ? getStyleWithNoCode(style.data) : style.data, + data: getStyleWithNoCode(data), excluded, sloppy }); diff --git a/popup/popup.js b/popup/popup.js index 7cf59b58..74228730 100644 --- a/popup/popup.js +++ b/popup/popup.js @@ -25,7 +25,7 @@ getActiveTab() ) .then(url => Promise.all([ (tabURL = URLS.supported(url) ? url : '') && - API.getStylesByUrl(tabURL, true), + API.getStylesByUrl(tabURL), onDOMready().then(initPopup), ])) .then(([results]) => { @@ -506,18 +506,13 @@ function handleUpdate(style) { } if (!tabURL) return; // Add an entry when a new style for the current url is installed - // FIXME: what does this do? - // API.getStyles({ - // matchUrl: tabURL, - // stopOnFirst: true, - // omitCode: true, - // }).then(([style]) => { - // if (style) { - // document.body.classList.remove('blocked'); - // $$.remove('.blocked-info, #no-styles'); - // createStyleElement({style, check: true}); - // } - // }); + API.getStylesByUrl(tabURL, style.id).then(([style]) => { + if (style) { + document.body.classList.remove('blocked'); + $$.remove('.blocked-info, #no-styles'); + createStyleElement({style, check: true}); + } + }); }