From 057111b171ad414c9cd152bac03711b83073da68 Mon Sep 17 00:00:00 2001 From: eight Date: Sun, 7 Oct 2018 22:59:31 +0800 Subject: [PATCH] Update usercss API --- background/background.js | 4 ++-- background/storage.js | 5 ----- background/update.js | 13 +++++++------ background/usercss-helper.js | 13 +++++++++---- content/install-hook-openusercss.js | 3 +-- content/install-hook-usercss.js | 2 +- edit/source-editor.js | 8 ++------ install-usercss/install-usercss.js | 7 +++---- manage/config-dialog.js | 6 +++--- manage/import-export.js | 2 +- manage/manage.js | 4 ++-- 11 files changed, 31 insertions(+), 36 deletions(-) diff --git a/background/background.js b/background/background.js index 10338ac1..eaa12eec 100644 --- a/background/background.js +++ b/background/background.js @@ -1,5 +1,4 @@ /* -global dbExec getStyles saveStyle deleteStyle global handleCssTransitionBug detectSloppyRegexps global openEditor global styleViaAPI @@ -15,6 +14,7 @@ window.API_METHODS = Object.assign(window.API_METHODS || {}, { toggleStyle: styleManager.toggleStyle, deleteStyle: styleManager.deleteStyle, getStylesInfoByUrl: styleManager.getStylesInfoByUrl, + installStyle: styleManager.installStyle, getStyleFromDB: id => db.exec('get', id).then(event => event.target.result), @@ -306,7 +306,7 @@ function webNavUsercssInstallerFF(data) { getTab(tabId), ]).then(([pong, tab]) => { if (pong !== true && tab.url !== 'about:blank') { - window.API_METHODS.installUsercss({direct: true}, {tab}); + window.API_METHODS.openUsercssInstallPage({direct: true}, {tab}); } }); } diff --git a/background/storage.js b/background/storage.js index 59c74e06..e3e01e3f 100644 --- a/background/storage.js +++ b/background/storage.js @@ -149,11 +149,6 @@ function filterStylesInternal({ : filtered; } - -function saveStyle(style) { -} - - function deleteStyle({id, notify = true}) { id = Number(id); return dbExec('delete', id).then(() => { diff --git a/background/update.js b/background/update.js index e63bf4a8..99bb126b 100644 --- a/background/update.js +++ b/background/update.js @@ -1,5 +1,5 @@ /* -global getStyles saveStyle styleSectionsEqual +global styleSectionsEqual global calcStyleDigest cachedStyles getStyleWithNoCode global usercss semverCompare global API_METHODS styleManager @@ -173,7 +173,7 @@ global API_METHODS styleManager json.id = style.id; json.updateDate = Date.now(); - json.reason = 'update'; + // json.reason = 'update'; // keep current state delete json.enabled; @@ -185,10 +185,11 @@ global API_METHODS styleManager json.originalName = json.name; } + const newStyle = Object.assign({}, style, json); if (styleSectionsEqual(json, style, {checkSource: true})) { // update digest even if save === false as there might be just a space added etc. - json.reason = 'update-digest'; - return saveStyle(json) + // json.reason = 'update-digest'; + return styleManager.installStyle(newStyle) .then(saved => { style.originalDigest = saved.originalDigest; return Promise.reject(STATES.SAME_CODE); @@ -200,8 +201,8 @@ global API_METHODS styleManager } return save ? - API_METHODS[json.usercssData ? 'saveUsercss' : 'saveStyle'](json) : - json; + API_METHODS[json.usercssData ? 'installUsercss' : 'installStyle'](newStyle) : + newStyle; } function styleJSONseemsValid(json) { diff --git a/background/usercss-helper.js b/background/usercss-helper.js index c2f2fe84..1079511c 100644 --- a/background/usercss-helper.js +++ b/background/usercss-helper.js @@ -1,12 +1,17 @@ -/* global API_METHODS usercss saveStyle getStyles chromeLocal cachedStyles */ +/* global API_METHODS usercss chromeLocal */ 'use strict'; (() => { - API_METHODS.saveUsercss = style => save(style, false); - API_METHODS.saveUsercssUnsafe = style => save(style, true); + // API_METHODS.saveUsercss = style => save(style, false); + // API_METHODS.saveUsercssUnsafe = style => save(style, true); + API_METHODS.installUsercss + API_METHODS.editSaveUsercss + API_METHODS.configUsercssVars = (id, vars) => newVars + API_METHODS.buildUsercss = build; - API_METHODS.installUsercss = install; + API_METHODS.openUsercssInstallPage + // API_METHODS.installUsercss = install; API_METHODS.parseUsercss = parse; API_METHODS.findUsercss = find; diff --git a/content/install-hook-openusercss.js b/content/install-hook-openusercss.js index 93d709b5..7f438513 100644 --- a/content/install-hook-openusercss.js +++ b/content/install-hook-openusercss.js @@ -128,8 +128,7 @@ && event.data.type === 'ouc-install-usercss' && allowedOrigins.includes(event.origin) ) { - API.saveUsercss({ - reason: 'install', + API.installUsercss({ name: event.data.title, sourceCode: event.data.code, }).then(style => { diff --git a/content/install-hook-usercss.js b/content/install-hook-usercss.js index 9c42f5b9..b3e4fba8 100644 --- a/content/install-hook-usercss.js +++ b/content/install-hook-usercss.js @@ -16,7 +16,7 @@ let sourceCode, port, timer; chrome.runtime.onConnect.addListener(onConnected); - API.installUsercss({url}) + API.openUsercssInstallPage({url}) .catch(err => alert(err)); function onConnected(newPort) { diff --git a/edit/source-editor.js b/edit/source-editor.js index a16a27af..56278681 100644 --- a/edit/source-editor.js +++ b/edit/source-editor.js @@ -224,17 +224,13 @@ function createSourceEditor(style) { exclusionList }); return ensureUniqueStyle(code) - .then(() => API.saveUsercssUnsafe({ + .then(() => API.editSaveUsercss({ id: style.id, - reason: 'editSave', enabled: style.enabled, sourceCode: code, exclusions: exclusionList })) - .then(({style, errors}) => { - replaceStyle(style); - if (errors) return Promise.reject(errors); - }) + .then(replaceStyle) .catch(err => { if (err.handled) return; if (err.message === t('styleMissingMeta', 'name')) { diff --git a/install-usercss/install-usercss.js b/install-usercss/install-usercss.js index 663b40ec..7164c8de 100644 --- a/install-usercss/install-usercss.js +++ b/install-usercss/install-usercss.js @@ -86,13 +86,12 @@ cm.setCursor(cursor); cm.scrollTo(scrollInfo.left, scrollInfo.top); - API.saveUsercssUnsafe({ + API.installUsercss({ id: (installed || installedDup).id, reason: 'update', sourceCode - }).then(({style, errors}) => { + }).then(style => { updateMeta(style); - if (errors) return Promise.reject(errors); }).catch(showError); }); } @@ -301,7 +300,7 @@ data.version, ])) ).then(ok => ok && - API.saveUsercss(Object.assign(style, dup && {reason: 'update'})) + API.installUsercss(style) .then(install) .catch(err => messageBox.alert(t('styleInstallFailed', err), 'pre')) ); diff --git a/manage/config-dialog.js b/manage/config-dialog.js index 1f603b7b..353e98fb 100644 --- a/manage/config-dialog.js +++ b/manage/config-dialog.js @@ -171,9 +171,9 @@ function configDialog(style) { return; } saving = true; - return API.saveUsercss(style) - .then(saved => { - varsInitial = getInitialValues(saved.usercssData.vars); + return API.configUsercssVar(style.id, style.usercssData.vars) + .then(newVars => { + varsInitial = getInitialValues(newVars); vars.forEach(va => onchange({target: va.input, justSaved: true})); renderValues(); updateButtons(); diff --git a/manage/import-export.js b/manage/import-export.js index a1dc3f23..b61b98c9 100644 --- a/manage/import-export.js +++ b/manage/import-export.js @@ -41,7 +41,7 @@ function importFromFile({fileTypeFilter, file} = {}) { importFromString(text) : getOwnTab().then(tab => { tab.url = URL.createObjectURL(new Blob([text], {type: 'text/css'})); - return API.installUsercss({direct: true, tab}) + return API.openUsercssInstallPage({direct: true, tab}) .then(() => URL.revokeObjectURL(tab.url)); }) ).then(numStyles => { diff --git a/manage/manage.js b/manage/manage.js index 91e73917..decc50ca 100644 --- a/manage/manage.js +++ b/manage/manage.js @@ -415,8 +415,8 @@ Object.assign(handleEvent, { event.preventDefault(); const json = entry.updatedCode; json.id = entry.styleId; - json.reason = 'update'; - API[json.usercssData ? 'saveUsercss' : 'saveStyle'](json); + // json.reason = 'update'; + API[json.usercssData ? 'installUsercss' : 'installStyle'](json); }, delete(event, entry) {