diff options
Diffstat (limited to 'e2e/ambassador/src')
-rw-r--r-- | e2e/ambassador/src/background/index.js | 12 | ||||
-rw-r--r-- | e2e/ambassador/src/background/ipc.js | 7 | ||||
-rw-r--r-- | e2e/ambassador/src/client/ipc.js | 29 | ||||
-rw-r--r-- | e2e/ambassador/src/client/windows.js | 17 | ||||
-rw-r--r-- | e2e/ambassador/src/content/index.js | 11 | ||||
-rw-r--r-- | e2e/ambassador/src/content/ipc.js | 31 | ||||
-rw-r--r-- | e2e/ambassador/src/shared/messages.js | 12 |
7 files changed, 119 insertions, 0 deletions
diff --git a/e2e/ambassador/src/background/index.js b/e2e/ambassador/src/background/index.js index e69de29..027c953 100644 --- a/e2e/ambassador/src/background/index.js +++ b/e2e/ambassador/src/background/index.js @@ -0,0 +1,12 @@ +import { WINDOWS_CREATE, WINDOWS_REMOVE } from '../shared/messages'; +import { receiveContentMessage } from './ipc'; + +receiveContentMessage((message) => { + switch (message.type) { + case WINDOWS_CREATE: + return browser.windows.create(); + case WINDOWS_REMOVE: + return browser.windows.remove(message.windowId); + } +}); + diff --git a/e2e/ambassador/src/background/ipc.js b/e2e/ambassador/src/background/ipc.js new file mode 100644 index 0000000..95d2164 --- /dev/null +++ b/e2e/ambassador/src/background/ipc.js @@ -0,0 +1,7 @@ +const receiveContentMessage = (func) => { + browser.runtime.onMessage.addListener((message) => { + return func(message); + }); +}; + +export { receiveContentMessage }; diff --git a/e2e/ambassador/src/client/ipc.js b/e2e/ambassador/src/client/ipc.js new file mode 100644 index 0000000..9f232ea --- /dev/null +++ b/e2e/ambassador/src/client/ipc.js @@ -0,0 +1,29 @@ +import { METHOD_REQUEST, METHOD_RESPONSE } from '../shared/messages'; + +const generateId = () => { + return Math.random().toString(); +}; + +const send = (message) => { + return new Promise((resolve) => { + let id = generateId(); + let callback = (e) => { + let packet = e.data; + if (e.source !== window || packet.method !== METHOD_RESPONSE || + packet.id !== id) { + return; + } + window.removeEventListener('message', callback); + resolve(packet.message); + }; + window.addEventListener('message', callback); + + window.postMessage({ + id, + method: METHOD_REQUEST, + message + }, window.origin); + }); +}; + +export { send }; diff --git a/e2e/ambassador/src/client/windows.js b/e2e/ambassador/src/client/windows.js new file mode 100644 index 0000000..69b8905 --- /dev/null +++ b/e2e/ambassador/src/client/windows.js @@ -0,0 +1,17 @@ +import { WINDOWS_CREATE, WINDOWS_REMOVE } from '../shared/messages'; +import * as ipc from './ipc'; + +const create = () => { + return ipc.send({ + type: WINDOWS_CREATE + }); +}; + +const remove = (windowId) => { + return ipc.send({ + type: WINDOWS_REMOVE, + windowId, + }); +}; + +export { create, remove }; diff --git a/e2e/ambassador/src/content/index.js b/e2e/ambassador/src/content/index.js index e69de29..dd46688 100644 --- a/e2e/ambassador/src/content/index.js +++ b/e2e/ambassador/src/content/index.js @@ -0,0 +1,11 @@ +import { WINDOWS_CREATE, WINDOWS_REMOVE } from '../shared/messages'; +import * as ipc from './ipc'; + +ipc.receivePageMessage((message) => { + switch (message.type) { + case WINDOWS_CREATE: + return ipc.send(message); + case WINDOWS_REMOVE: + return ipc.send(message); + } +}); diff --git a/e2e/ambassador/src/content/ipc.js b/e2e/ambassador/src/content/ipc.js new file mode 100644 index 0000000..62fcac1 --- /dev/null +++ b/e2e/ambassador/src/content/ipc.js @@ -0,0 +1,31 @@ +import { METHOD_REQUEST, METHOD_RESPONSE } from '../shared/messages'; + +const send = (message) => { + return browser.runtime.sendMessage(message); +}; + +const receivePageMessage = (func) => { + window.addEventListener('message', (e) => { + let packet = e.data; + if (e.origin !== window.origin || packet.method !== METHOD_REQUEST) { + return; + } + + let resp = { + id: packet.id, + method: METHOD_RESPONSE, + }; + let respMessage = func(packet.message); + if (respMessage instanceof Promise) { + return respMessage.then((data) => { + resp.message = data; + e.source.postMessage(resp, e.origin); + }); + } else if (respMessage) { + resp.message = respMessage; + } + e.source.postMessage(resp, e.origin); + }); +}; + +export { send, receivePageMessage }; diff --git a/e2e/ambassador/src/shared/messages.js b/e2e/ambassador/src/shared/messages.js new file mode 100644 index 0000000..e8a0438 --- /dev/null +++ b/e2e/ambassador/src/shared/messages.js @@ -0,0 +1,12 @@ +const METHOD_REQUEST = 'request'; +const METHOD_RESPONSE = 'response'; +const WINDOWS_CREATE = 'windows.create'; +const WINDOWS_REMOVE = 'windows.remove'; + +export { + METHOD_REQUEST, + METHOD_RESPONSE, + + WINDOWS_CREATE, + WINDOWS_REMOVE, +}; |