From 93a4cdf595785690a830759a1b06138dec08d73a Mon Sep 17 00:00:00 2001 From: eight Date: Sun, 14 Oct 2018 02:34:05 +0800 Subject: [PATCH] Add: findStyle API --- background/background.js | 13 +++++++------ background/style-manager.js | 11 +++++++++++ content/install-hook-userstyles.js | 4 ++-- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/background/background.js b/background/background.js index b763b637..dc7c295f 100644 --- a/background/background.js +++ b/background/background.js @@ -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]; diff --git a/background/style-manager.js b/background/style-manager.js index 81336887..d3651d24 100644 --- a/background/style-manager.js +++ b/background/style-manager.js @@ -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)); } diff --git a/content/install-hook-userstyles.js b/content/install-hook-userstyles.js index 40ed778a..e4602605 100644 --- a/content/install-hook-userstyles.js +++ b/content/install-hook-userstyles.js @@ -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,