Fix: handle styleAdded message in popup

This commit is contained in:
eight 2018-10-14 18:59:29 +08:00
parent 81f3e69574
commit f85d4de39b
3 changed files with 16 additions and 19 deletions

View File

@ -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));
}
}

View File

@ -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
});

View File

@ -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});
}
});
}