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, getStylesInfo: styleManager.getStylesInfo,
toggleStyle: styleManager.toggleStyle, toggleStyle: styleManager.toggleStyle,
deleteStyle: styleManager.deleteStyle, deleteStyle: styleManager.deleteStyle,
getStylesInfoByUrl: styleManager.getStylesInfoByUrl,
// saveStyle, // saveStyle,
// deleteStyle, // deleteStyle,

View File

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

View File

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