diff options
Diffstat (limited to 'e2e/ambassador/src')
-rw-r--r-- | e2e/ambassador/src/background/index.js | 36 | ||||
-rw-r--r-- | e2e/ambassador/src/background/ipc.js | 7 | ||||
-rw-r--r-- | e2e/ambassador/src/background/tabs.js | 18 | ||||
-rw-r--r-- | e2e/ambassador/src/client/ipc.js | 29 | ||||
-rw-r--r-- | e2e/ambassador/src/client/keys.js | 29 | ||||
-rw-r--r-- | e2e/ambassador/src/client/tabs.js | 12 | ||||
-rw-r--r-- | e2e/ambassador/src/client/windows.js | 27 | ||||
-rw-r--r-- | e2e/ambassador/src/content/index.js | 37 | ||||
-rw-r--r-- | e2e/ambassador/src/content/ipc.js | 40 | ||||
-rw-r--r-- | e2e/ambassador/src/shared/messages.js | 24 |
10 files changed, 259 insertions, 0 deletions
diff --git a/e2e/ambassador/src/background/index.js b/e2e/ambassador/src/background/index.js new file mode 100644 index 0000000..f9fda7e --- /dev/null +++ b/e2e/ambassador/src/background/index.js @@ -0,0 +1,36 @@ +import { + WINDOWS_CREATE, WINDOWS_REMOVE, WINDOWS_GET, + TABS_CREATE, + EVENT_KEYPRESS, EVENT_KEYDOWN, EVENT_KEYUP, +} from '../shared/messages'; +import * as tabs from './tabs'; +import { receiveContentMessage } from './ipc'; + +receiveContentMessage((message) => { + switch (message.type) { + case WINDOWS_CREATE: + return browser.windows.create({ url: message.url }); + case WINDOWS_REMOVE: + return browser.windows.remove(message.windowId); + case WINDOWS_GET: + return browser.windows.get(message.windowId, { populate: true }); + case TABS_CREATE: + return tabs.create({ + url: message.url, + windowId: message.windowId, + }); + } +}); + + +receiveContentMessage((message) => { + switch (message.type) { + case EVENT_KEYPRESS: + case EVENT_KEYDOWN: + case EVENT_KEYUP: + return browser.tabs.sendMessage( + message.tabId, + message + ); + } +}); 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/background/tabs.js b/e2e/ambassador/src/background/tabs.js new file mode 100644 index 0000000..93d47a3 --- /dev/null +++ b/e2e/ambassador/src/background/tabs.js @@ -0,0 +1,18 @@ +const create = (props = {}) => { + return new Promise((resolve) => { + browser.tabs.create(props).then((createdTab) => { + let callback = (tabId, changeInfo, tab) => { + if (tab.url !== 'about:blank' && tabId === createdTab.id && + changeInfo.status === 'complete') { + browser.tabs.onUpdated.removeListener(callback); + resolve(tab); + } + }; + browser.tabs.onUpdated.addListener(callback); + }); + }); +}; + +export { + create, +}; 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/keys.js b/e2e/ambassador/src/client/keys.js new file mode 100644 index 0000000..af0fb3d --- /dev/null +++ b/e2e/ambassador/src/client/keys.js @@ -0,0 +1,29 @@ +import { EVENT_KEYPRESS, EVENT_KEYDOWN, EVENT_KEYUP } from '../shared/messages'; +import * as ipc from './ipc'; + +const press = (tabId, key) => { + return ipc.send({ + type: EVENT_KEYPRESS, + tabId, + key, + }); +}; + +const down = (tabId, key) => { + return ipc.send({ + type: EVENT_KEYDOWN, + tabId, + key, + }); +}; + + +const up = (tabId, key) => { + return ipc.send({ + type: EVENT_KEYUP, + tabId, + key, + }); +}; + +export { press, down, up }; diff --git a/e2e/ambassador/src/client/tabs.js b/e2e/ambassador/src/client/tabs.js new file mode 100644 index 0000000..4db3c11 --- /dev/null +++ b/e2e/ambassador/src/client/tabs.js @@ -0,0 +1,12 @@ +import { TABS_CREATE } from '../shared/messages'; +import * as ipc from './ipc'; + +const create = (windowId, url) => { + return ipc.send({ + type: TABS_CREATE, + windowId, + url, + }); +}; + +export { create }; diff --git a/e2e/ambassador/src/client/windows.js b/e2e/ambassador/src/client/windows.js new file mode 100644 index 0000000..f92405a --- /dev/null +++ b/e2e/ambassador/src/client/windows.js @@ -0,0 +1,27 @@ +import { + WINDOWS_CREATE, WINDOWS_REMOVE, WINDOWS_GET +} from '../shared/messages'; +import * as ipc from './ipc'; + +const create = (url) => { + return ipc.send({ + type: WINDOWS_CREATE, + url, + }); +}; + +const remove = (windowId) => { + return ipc.send({ + type: WINDOWS_REMOVE, + windowId, + }); +}; + +const get = (windowId) => { + return ipc.send({ + type: WINDOWS_GET, + windowId, + }); +}; + +export { create, remove, get }; diff --git a/e2e/ambassador/src/content/index.js b/e2e/ambassador/src/content/index.js new file mode 100644 index 0000000..8573d66 --- /dev/null +++ b/e2e/ambassador/src/content/index.js @@ -0,0 +1,37 @@ +import { + WINDOWS_CREATE, WINDOWS_REMOVE, WINDOWS_GET, + TABS_CREATE, + EVENT_KEYPRESS, EVENT_KEYDOWN, EVENT_KEYUP, +} from '../shared/messages'; +import * as ipc from './ipc'; + +ipc.receivePageMessage((message) => { + switch (message.type) { + case WINDOWS_CREATE: + case WINDOWS_REMOVE: + case WINDOWS_GET: + case TABS_CREATE: + case EVENT_KEYPRESS: + case EVENT_KEYDOWN: + case EVENT_KEYUP: + return ipc.sendToBackground(message); + } +}); + +ipc.receiveBackgroundMesssage((message) => { + switch (message.type) { + case EVENT_KEYPRESS: + document.body.dispatchEvent( + new KeyboardEvent('keypress', { 'key': message.key })); + break; + case EVENT_KEYDOWN: + document.body.dispatchEvent( + new KeyboardEvent('keydown', { 'key': message.key })); + break; + case EVENT_KEYUP: + document.body.dispatchEvent( + new KeyboardEvent('keyup', { 'key': message.key })); + break; + } + return Promise.resolve({}); +}); diff --git a/e2e/ambassador/src/content/ipc.js b/e2e/ambassador/src/content/ipc.js new file mode 100644 index 0000000..917623c --- /dev/null +++ b/e2e/ambassador/src/content/ipc.js @@ -0,0 +1,40 @@ +import { METHOD_REQUEST, METHOD_RESPONSE } from '../shared/messages'; + +const sendToBackground = (message) => { + return browser.runtime.sendMessage(message); +}; + +const receiveBackgroundMesssage = (func) => { + return browser.runtime.onMessage.addListener((message) => { + return Promise.resolve(func(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 { + sendToBackground, receiveBackgroundMesssage, + receivePageMessage, +}; diff --git a/e2e/ambassador/src/shared/messages.js b/e2e/ambassador/src/shared/messages.js new file mode 100644 index 0000000..32b7aa2 --- /dev/null +++ b/e2e/ambassador/src/shared/messages.js @@ -0,0 +1,24 @@ +const METHOD_REQUEST = 'request'; +const METHOD_RESPONSE = 'response'; +const WINDOWS_CREATE = 'windows.create'; +const WINDOWS_REMOVE = 'windows.remove'; +const WINDOWS_GET = 'windows.get'; +const TABS_CREATE = 'tabs.create'; +const EVENT_KEYPRESS = 'event.keypress'; +const EVENT_KEYDOWN = 'event.keydown'; +const EVENT_KEYUP = 'event.keyup'; + +export { + METHOD_REQUEST, + METHOD_RESPONSE, + + WINDOWS_CREATE, + WINDOWS_REMOVE, + WINDOWS_GET, + + TABS_CREATE, + + EVENT_KEYPRESS, + EVENT_KEYDOWN, + EVENT_KEYUP, +}; |