From 171339f7109b5f795f37ec2af3a06c14e203b7ec Mon Sep 17 00:00:00 2001 From: eight Date: Sat, 6 Oct 2018 04:39:48 +0800 Subject: [PATCH] WIP: drop api.js --- content/api.js | 46 -------------------- edit.html | 2 +- js/msg.js | 112 +++++++++++++++++++++++++++++-------------------- manage.html | 2 +- manifest.json | 8 ++-- popup.html | 2 +- 6 files changed, 73 insertions(+), 99 deletions(-) delete mode 100644 content/api.js diff --git a/content/api.js b/content/api.js deleted file mode 100644 index fa87f720..00000000 --- a/content/api.js +++ /dev/null @@ -1,46 +0,0 @@ -/* global promisify */ -'use strict'; - -const API = (() => { - const preparing = chrome.runtime.getBackgroundPage ? - promisify(chrome.runtime.getBackgroundPage.bind(chrome.runtime))() - .catch(() => null) : - Promise.resolve(null); - const runtimeSendMessage = promisify(chrome.runtime.sendMessage.bind(chrome.runtime)); - return new Proxy(() => {}, { - get: (target, name) => - (...args) => invokeBG(name, args), - }); - - function sendMessage(msg) { - return runtimeSendMessage(msg) - .then(result => { - if (result && result.__ERROR__) { - throw new Error(result.__ERROR__); - } - return result; - }); - } - - function invokeBG(name, args) { - return preparing.then(BG => { - if (!BG) { - return sendMessage({ - method: 'invokeAPI', - name, - args - }); - } - // FIXME: why deep-copying input/output? - if (BG !== window) { - args = BG.deepCopy(args); - } - const fn = BG.API_METHODS[name]; - if (!fn) { - throw new Error(`unknown API method: ${name}`); - } - return Promise.resolve(fn(...args)) - .then(BG.deepCopy); - }); - } -})(); diff --git a/edit.html b/edit.html index 2387fefb..01c91b83 100644 --- a/edit.html +++ b/edit.html @@ -26,7 +26,7 @@ - + diff --git a/js/msg.js b/js/msg.js index 31ee7a43..524ba220 100644 --- a/js/msg.js +++ b/js/msg.js @@ -1,4 +1,5 @@ /* global promisify deepCopy */ +// deepCopy is only used if the script is executed in extension pages. 'use strict'; const msg = (() => { @@ -119,54 +120,56 @@ const msg = (() => { tab: [], extension: [] }; - chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { - const handlers = message.target === 'tab' ? - handler.tab.concat(handler.both) : message.target === 'extension' ? - handler.extension.concat(handler.both) : - handler.both.concat(handler.extension, handler.tab); - if (!handlers.length) { - return; - } - if (message.type === 'exchange') { - const pending = exchangeGet(message, true); - if (pending) { - pending.then(response); - return true; - } - } - return response(); + chrome.runtime.onMessage.addListener(handleMessage); + } - function response() { - let result; - for (const handle of handlers) { - const data = handle(message.data, sender); - if (data !== undefined && result === undefined) { - result = data; - } - } - if (result === undefined) { - return; - } - Promise.resolve(result) - .then( - data => ({error: false, data}), - err => ({error: true, data: err.message || String(err)}) - ) - .then(function doResponse(responseMessage) { - if (message.from === 'extension' && bg === undefined) { - return preparing.then(() => doResponse(responseMessage)); - } - if (message.from === 'extension' && bg) { - exchangeSet(responseMessage); - } else { - responseMessage.type = 'direct'; - } - return responseMessage; - }) - .then(sendResponse); + function handleMessage(message, sender, sendResponse) { + const handlers = message.target === 'tab' ? + handler.tab.concat(handler.both) : message.target === 'extension' ? + handler.extension.concat(handler.both) : + handler.both.concat(handler.extension, handler.tab); + if (!handlers.length) { + return; + } + if (message.type === 'exchange') { + const pending = exchangeGet(message, true); + if (pending) { + pending.then(response); return true; } - }); + } + return response(); + + function response() { + let result; + for (const handle of handlers) { + const data = withPromiseError(handle, message.data, sender); + if (data !== undefined && result === undefined) { + result = data; + } + } + if (result === undefined) { + return; + } + Promise.resolve(result) + .then( + data => ({error: false, data}), + err => ({error: true, data: err.message || String(err)}) + ) + .then(function doResponse(responseMessage) { + if (message.from === 'extension' && bg === undefined) { + return preparing.then(() => doResponse(responseMessage)); + } + if (message.from === 'extension' && bg) { + exchangeSet(responseMessage); + } else { + responseMessage.type = 'direct'; + } + return responseMessage; + }) + .then(sendResponse); + return true; + } } function exchangeGet(message, keepStorage = false) { @@ -175,7 +178,7 @@ const msg = (() => { } message.data = bg._msg.storage.get(message.id); if (keepStorage) { - message.data = deepCopy(data); + message.data = deepCopy(message.data); } else { bg._msg.storage.delete(message.id); } @@ -190,6 +193,14 @@ const msg = (() => { delete message.data; } + function withPromiseError(fn, ...args) { + try { + return fn(...args); + } catch (err) { + return Promise.reject(err); + } + } + function withCleanup(p, fn) { return p.then( result => { @@ -229,3 +240,12 @@ const msg = (() => { } } })(); + +const API = new Proxy({}, { + get: (target, name) => + (...args) => msg.send({ + method: 'invokeAPI', + name, + args + }) +}); diff --git a/manage.html b/manage.html index 85d5e3b7..6c97fc4e 100644 --- a/manage.html +++ b/manage.html @@ -157,7 +157,7 @@ - + diff --git a/manifest.json b/manifest.json index 3062e02e..31ba8391 100644 --- a/manifest.json +++ b/manifest.json @@ -61,19 +61,19 @@ "run_at": "document_start", "all_frames": true, "match_about_blank": true, - "js": ["js/promisify.js", "content/api.js", "content/apply.js"] + "js": ["js/promisify.js", "js/msg.js", "content/apply.js"] }, { "matches": ["http://userstyles.org/*", "https://userstyles.org/*"], "run_at": "document_start", "all_frames": true, - "js": ["js/promisify.js", "content/api.js", "content/install-hook-userstyles.js"] + "js": ["js/promisify.js", "js/msg.js", "content/install-hook-userstyles.js"] }, { "matches": ["https://openusercss.org/*", "https://openusercss.com/*"], "run_at": "document_start", "all_frames": false, - "js": ["js/promisify.js", "content/api.js", "content/install-hook-openusercss.js"] + "js": ["js/promisify.js", "js/msg.js", "content/install-hook-openusercss.js"] }, { "matches": [ @@ -97,7 +97,7 @@ ], "run_at": "document_idle", "all_frames": false, - "js": ["js/promisify.js", "content/api.js", "content/install-hook-usercss.js"] + "js": ["js/promisify.js", "js/msg.js", "content/install-hook-usercss.js"] } ], "browser_action": { diff --git a/popup.html b/popup.html index 1ac9f685..4332ac23 100644 --- a/popup.html +++ b/popup.html @@ -156,7 +156,7 @@ - +