diff --git a/common.js b/common.js index 95a0a33bd..ffa147798 100644 --- a/common.js +++ b/common.js @@ -5,6 +5,64 @@ const _ = s => chrome.i18n.getMessage(s); const lines = s => s ? s.split('\n') : []; const unlines = ss => ss.join('\n'); +/* Async APIs */ + +const getAuthToken = details => { + return new Promise((resolve, reject) => { + chrome.identity.getAuthToken(details, token => { + if (chrome.runtime.lastError) { + reject(new Error(chrome.runtime.lastError.message)); + return; + } + resolve(token); + }); + }); +}; + +const removeCachedAuthToken = details => { + return new Promise((resolve, reject) => { + chrome.identity.removeCachedAuthToken(details, () => { + resolve(); + }); + }); +}; + +const getLocalStorage = keys => { + return new Promise((resolve, reject) => { + chrome.storage.local.get(keys, items => { + if (chrome.runtime.lastError) { + reject(new Error(chrome.runtime.lastError.message)); + return; + } + resolve(items); + }); + }); +}; + +const setLocalStorage = items => { + return new Promise((resolve, reject) => { + chrome.storage.local.set(items, () => { + if (chrome.runtime.lastError) { + reject(new Error(chrome.runtime.lastError.message)); + return; + } + resolve(); + }); + }); +}; + +const queryTabs = queryInfo => { + return new Promise((resolve, reject) => { + chrome.tabs.query(queryInfo, result => { + if (chrome.runtime.lastError) { + reject(new Error(chrome.runtime.lastError.message)); + return; + } + resolve(result); + }); + }); +}; + /* Block Rules */ const compileBlockRule = raw => { @@ -38,28 +96,18 @@ const compileBlockRule = raw => { return null; }; -const loadBlockRules = onBlockRulesLoaded => { - chrome.storage.local.get({ blacklist: '' }, items => { - if (chrome.runtime.lastError) { - console.error('uBlacklist: storage error: ' + chrome.runtime.lastError.message); - onBlockRulesLoaded([]); - return; - } - const blockRules = lines(items.blacklist).map(raw => ({ raw, compiled: compileBlockRule(raw) })); - onBlockRulesLoaded(blockRules); - }); +const loadBlockRules = async () => { + const items = await getLocalStorage({ blacklist: '' }); + return lines(items.blacklist).map(raw => ({ raw, compiled: compileBlockRule(raw) })); }; -const saveBlockRules = blockRules => { - const blacklist = unlines(blockRules.map(rule => rule.raw)); - chrome.storage.local.set({ blacklist, timestamp: new Date().toISOString() }, () => { - if (chrome.runtime.lastError) { - console.error('uBlacklist: storage error: ' + chrome.runtime.lastError.message); - return; - } - chrome.runtime.sendMessage({}); +const saveBlockRules = async blockRules => { + await setLocalStorage({ + blacklist: unlines(blockRules.map(rule => rule.raw)), + timestamp: new Date().toISOString() }); -} + chrome.runtime.sendMessage({}); +}; const deriveBlockRule = url => { const u = new URL(url); @@ -67,48 +115,3 @@ const deriveBlockRule = url => { return s ? (s[2] ? '*' : s[1]) + '://' + u.hostname + '/*' : null; }; -/* Async APIs */ - -const getLocalStorage = keys => { - return new Promise((resolve, reject) => { - chrome.storage.local.get(keys, items => { - if (chrome.runtime.lastError) { - reject(new Error(chrome.runtime.lastError.message)); - return; - } - resolve(items); - }); - }); -} - -const setLocalStorage = items => { - return new Promise((resolve, reject) => { - chrome.storage.local.set(items, () => { - if (chrome.runtime.lastError) { - reject(new Error(chrome.runtime.lastError.message)); - return; - } - resolve(); - }); - }); -} - -const getAuthToken = details => { - return new Promise((resolve, reject) => { - chrome.identity.getAuthToken(details, token => { - if (chrome.runtime.lastError) { - reject(new Error(chrome.runtime.lastError.message)); - return; - } - resolve(token); - }); - }); -}; - -const removeCachedAuthToken = details => { - return new Promise((resolve, reject) => { - chrome.identity.removeCachedAuthToken(details, () => { - resolve(); - }); - }); -}; diff --git a/content.js b/content.js index cc828b4ab..1235f9239 100644 --- a/content.js +++ b/content.js @@ -4,9 +4,9 @@ class UBlacklist { this.blockedEntryCount = 0; this.queuedEntries = []; - loadBlockRules(blockRules => { - this.onBlockRulesLoaded(blockRules); - }); + (async () => { + this.onBlockRulesLoaded(await loadBlockRules()); + })(); new MutationObserver(records => { this.onDOMContentMutated(records); @@ -237,9 +237,13 @@ class UBlacklist { if (compiled) { this.blockRules.push({ raw, compiled }); this.rejudgeAllEntries(); - saveBlockRules(this.blockRules); + (async () => { + await saveBlockRules(this.blockRules); + blockDialog.close(); + })(); + } else { + blockDialog.close(); } - blockDialog.close(); }); blockDialog.addEventListener('click', event => { if (event.target == blockDialog) { @@ -252,8 +256,10 @@ class UBlacklist { event.preventDefault(); this.blockRules.splice(Number($('ubUnblockSelect').value), 1); this.rejudgeAllEntries(); - saveBlockRules(this.blockRules); - unblockDialog.close(); + (async () => { + await saveBlockRules(this.blockRules); + unblockDialog.close(); + })(); }); unblockDialog.addEventListener('click', event => { if (event.target == unblockDialog) { diff --git a/popup.css b/popup.css index 7a81c093e..9c19eb2dd 100644 --- a/popup.css +++ b/popup.css @@ -1,14 +1,15 @@ -body { +#blockPopup, #unblockPopup { + display: none; font-size: 13px; margin: 0; } -#ubBlockForm, #ubUnblockForm { +#blockForm, #unblockForm { padding: 20px 12px; white-space: nowrap; } -#ubBlockInput, #ubUnblockSelect { +#blockInput, #unblockSelect { margin: 0 6px; width: 280px; } diff --git a/popup.html b/popup.html index 2eb77ab15..38f2d8376 100644 --- a/popup.html +++ b/popup.html @@ -6,6 +6,25 @@