Add: findStyle API

This commit is contained in:
eight 2018-10-14 02:34:05 +08:00
parent 8e75871b9b
commit 93a4cdf595
3 changed files with 20 additions and 8 deletions

View File

@ -9,17 +9,18 @@ var backgroundWorker = workerUtil.createWorker({
});
window.API_METHODS = Object.assign(window.API_METHODS || {}, {
deleteStyle: styleManager.deleteStyle,
editSave: styleManager.editSave,
findStyle: styleManager.findStyle,
getAllStyles: styleManager.getAllStyles, // used by importer
getSectionsByUrl: styleManager.getSectionsByUrl,
getStyle: styleManager.get,
getStylesInfo: styleManager.getStylesInfo,
getStylesInfoByUrl: styleManager.getStylesInfoByUrl,
toggleStyle: styleManager.toggleStyle,
deleteStyle: styleManager.deleteStyle,
installStyle: styleManager.installStyle,
editSave: styleManager.editSave,
importStyle: styleManager.importStyle,
installStyle: styleManager.installStyle,
styleExists: styleManager.styleExists,
getStyle: styleManager.get,
getAllStyles: styleManager.getAllStyles, // used by importer
toggleStyle: styleManager.toggleStyle,
getTabUrlPrefix() {
return this.sender.tab.url.match(/^([\w-]+:\/+[^/#]+)/)[1];

View File

@ -54,6 +54,7 @@ const styleManager = (() => {
installStyle,
deleteStyle,
editSave,
findStyle,
importStyle,
toggleStyle,
setStyleExclusions,
@ -116,6 +117,16 @@ const styleManager = (() => {
.map(s => getStyleWithNoCode(s.data));
}
// used by install-hook-userstyles.js
function findStyle(filter) {
for (const style of styles.values()) {
if (filterMatch(filter, style.data)) {
return style.data;
}
}
return null;
}
function styleExists(filter) {
return [...styles.value()].some(s => filterMatch(filter, s.data));
}

View File

@ -30,7 +30,7 @@
gotBody = true;
// TODO: remove the following statement when USO pagination title is fixed
document.title = document.title.replace(/^(\d+)&\w+=/, '#$1: ');
API.getStylesInfo({
API.findStyle({
md5Url: getMeta('stylish-md5-url') || location.href
}).then(checkUpdatability);
}
@ -66,7 +66,7 @@
return jsonUrl + (paramsMissing ? textUrl.replace(/^[^?]+/, '') : '');
}
function checkUpdatability([installedStyle]) {
function checkUpdatability(installedStyle) {
// TODO: remove the following statement when USO is fixed
document.dispatchEvent(new CustomEvent('stylusFixBuggyUSOsettings', {
detail: installedStyle && installedStyle.updateUrl,