From f194155830b98e1f6a8c8f4fb25fe3501cf18f70 Mon Sep 17 00:00:00 2001 From: eight Date: Fri, 6 Oct 2017 16:29:06 +0800 Subject: [PATCH] Add: close source tab after install --- background/background.js | 29 ++++++++++++++++++++++++++++- background/storage.js | 5 ----- content/install-user-css.js | 8 ++++++++ install-usercss/install-usercss.js | 6 ++++++ 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/background/background.js b/background/background.js index a9a6cf21..f649d64c 100644 --- a/background/background.js +++ b/background/background.js @@ -1,6 +1,6 @@ /* global dbExec, getStyles, saveStyle */ /* global handleCssTransitionBug */ -/* global usercssHelper */ +/* global usercssHelper openEditor */ 'use strict'; // eslint-disable-next-line no-var @@ -334,9 +334,36 @@ function onRuntimeMessage(request, sender, sendResponse) { case 'initUsercssInstallPage': usercssHelper.initInstallPage(sender.tab.id, request).then(sendResponse); return KEEP_CHANNEL_OPEN; + + case 'closeTab': + closeTab(sender.tab.id, request).then(sendResponse); + return KEEP_CHANNEL_OPEN; + + case 'openEditor': + openEditor(request.id); + return; } } +function closeTab(tabId, request) { + return new Promise(resolve => { + if (request.tabId) { + tabId = request.tabId; + } + chrome.tabs.remove(tabId, () => { + const {lastError} = chrome.runtime; + if (lastError) { + resolve({ + status: 'error', + error: lastError.message || String(lastError) + }); + return; + } + resolve({status: 'success'}); + }); + }); +} + function injectContent(tabId, {files}) { return Promise.all(files.map(inject)) .then(() => ({status: 'success'})) diff --git a/background/storage.js b/background/storage.js index a12a57d8..71e9a1ff 100644 --- a/background/storage.js +++ b/background/storage.js @@ -1,5 +1,4 @@ /* global LZString */ -/* global openEditor */ 'use strict'; @@ -457,10 +456,6 @@ function saveStyle(style) { style, codeIsUpdated, reason, }); } - if (style.usercssData && !existed && reason === 'update') { - // open the editor for usercss with the first install? - openEditor(style.id); - } return style; } } diff --git a/content/install-user-css.js b/content/install-user-css.js index dc542c5c..99c31496 100644 --- a/content/install-user-css.js +++ b/content/install-user-css.js @@ -99,6 +99,14 @@ function initUsercssInstall() { case 'liveReloadStop': watcher.stop(); break; + + case 'closeTab': + if (history.length > 1) { + history.back(); + } else { + runtimeSend({method: 'closeTab'}); + } + break; } }); }); diff --git a/install-usercss/install-usercss.js b/install-usercss/install-usercss.js index 96338841..68c8196e 100644 --- a/install-usercss/install-usercss.js +++ b/install-usercss/install-usercss.js @@ -184,6 +184,12 @@ updateMeta(result); + runtimeSend({method: 'openEditor', id: result.id}); + + if (!liveReload) { + port.postMessage({method: 'closeTab'}); + } + window.dispatchEvent(new CustomEvent('installed')); }) .catch(err => {