Add: getStylesInfoByUrl

This commit is contained in:
eight 2018-10-06 13:45:37 +08:00
parent 095998f07c
commit fedf844ddd
3 changed files with 57 additions and 43 deletions

View File

@ -16,6 +16,7 @@ window.API_METHODS = Object.assign(window.API_METHODS || {}, {
getStylesInfo: styleManager.getStylesInfo,
toggleStyle: styleManager.toggleStyle,
deleteStyle: styleManager.deleteStyle,
getStylesInfoByUrl: styleManager.getStylesInfoByUrl,
// saveStyle,
// deleteStyle,

View File

@ -25,7 +25,7 @@ const styleManager = (() => {
editSave,
toggleStyle,
getAllStyles, // used by import-export
getStylesInfoForUrl, // used by popup
getStylesInfoByUrl, // used by popup
countStyles,
// TODO: get all styles API?
// TODO: get style by ID?
@ -88,12 +88,23 @@ const styleManager = (() => {
return true;
}
function editSave(style) {
// const style =
return saveStyle(style);
function editSave(data) {
data = Object.assign({}, styles.get(data.id).data, data);
return saveStyle(data)
.then(newData =>
broadcastStyleUpdated(newData)
.then(() => newData)
);
}
function setStyleExclusions() {}
function setStyleExclusions(id, exclusions) {
const data = Object.assign({}, styles.get(id), {exclusions});
return saveStyle(data)
.then(newData =>
broadcastStyleUpdated(newData)
.then(() => newData)
);
}
function ensurePrepared(methods) {
for (const [name, fn] in Object.entries(methods)) {
@ -163,7 +174,7 @@ const styleManager = (() => {
msg.broadcastExtension({method: 'styleAdded', style: getStyleWithNoCode(newData)}),
msg.broadcastTab(tab => getStyleAddedMessage(tab, newData, appliesTo))
]);
} else {
}
const excluded = new Set();
const updated = new Map();
for (const url of style.appliesTo) {
@ -199,7 +210,6 @@ const styleManager = (() => {
})
]);
}
}
function getStyleAddedMessage(tab, data, appliesTo) {
const code = getAppliedCode(tab.url, data);
@ -243,8 +253,10 @@ const styleManager = (() => {
});
}
function getStylesInfoForUrl(url) {
function getStylesInfoByUrl(url) {
const sections = getSectionsByUrl(url);
return Object.keys(sections)
.map(k => getStyleWithNoCode(styles.get(Number(k)).data));
}
function getSectionsByUrl(url, filterId) {
@ -276,6 +288,7 @@ const styleManager = (() => {
code += section.code;
}
}
// FIXME: trim comment?
return code;
}

View File

@ -20,7 +20,7 @@ getActiveTab().then(tab =>
: getTabRealURL(tab)
).then(url => Promise.all([
(tabURL = URLS.supported(url) ? url : '') &&
API.getStylesInfoForUrl(tabURL),
API.getStylesInfoByUrl(tabURL),
onDOMready().then(initPopup),
])).then(([styles]) => {
showStyles(styles);