send press key to tabs

jh-changes
Shin'ya Ueoka 7 years ago
parent cf03d278eb
commit 3f8bbe11d9
  1. 3
      e2e/ambassador/manifest.json
  2. 25
      e2e/ambassador/src/background/index.js
  3. 29
      e2e/ambassador/src/client/keys.js
  4. 12
      e2e/ambassador/src/client/tabs.js
  5. 5
      e2e/ambassador/src/client/windows.js
  6. 30
      e2e/ambassador/src/content/index.js
  7. 13
      e2e/ambassador/src/content/ipc.js
  8. 10
      e2e/ambassador/src/shared/messages.js
  9. 19
      e2e/contents/scroll.test.js
  10. 35
      e2e/contents/tab.test.js
  11. 2
      e2e/karma-delay.js

@ -22,6 +22,7 @@
"sessions",
"storage",
"tabs",
"clipboardRead"
"clipboardRead",
"activeTab"
]
}

@ -1,12 +1,33 @@
import { WINDOWS_CREATE, WINDOWS_REMOVE } from '../shared/messages';
import {
WINDOWS_CREATE, WINDOWS_REMOVE, 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();
return browser.windows.create({ url: message.url });
case WINDOWS_REMOVE:
return browser.windows.remove(message.windowId);
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
);
}
});

@ -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 };

@ -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 };

@ -1,9 +1,10 @@
import { WINDOWS_CREATE, WINDOWS_REMOVE } from '../shared/messages';
import * as ipc from './ipc';
const create = () => {
const create = (url) => {
return ipc.send({
type: WINDOWS_CREATE
type: WINDOWS_CREATE,
url,
});
};

@ -1,11 +1,35 @@
import { WINDOWS_CREATE, WINDOWS_REMOVE } from '../shared/messages';
import {
WINDOWS_CREATE, WINDOWS_REMOVE, 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:
return ipc.send(message);
case WINDOWS_REMOVE:
return ipc.send(message);
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({});
});

@ -1,9 +1,15 @@
import { METHOD_REQUEST, METHOD_RESPONSE } from '../shared/messages';
const send = (message) => {
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;
@ -28,4 +34,7 @@ const receivePageMessage = (func) => {
});
};
export { send, receivePageMessage };
export {
sendToBackground, receiveBackgroundMesssage,
receivePageMessage,
};

@ -2,6 +2,10 @@ const METHOD_REQUEST = 'request';
const METHOD_RESPONSE = 'response';
const WINDOWS_CREATE = 'windows.create';
const WINDOWS_REMOVE = 'windows.remove';
const TABS_CREATE = 'tabs.create';
const EVENT_KEYPRESS = 'event.keypress';
const EVENT_KEYDOWN = 'event.keydown';
const EVENT_KEYUP = 'event.keyup';
export {
METHOD_REQUEST,
@ -9,4 +13,10 @@ export {
WINDOWS_CREATE,
WINDOWS_REMOVE,
TABS_CREATE,
EVENT_KEYPRESS,
EVENT_KEYDOWN,
EVENT_KEYUP,
};

@ -1,19 +0,0 @@
import { expect } from "chai";
import * as windows from "../ambassador/src/client/windows";
describe("scroll test", () => {
let targetWindow;
before(() => {
return windows.create().then((win) => {
targetWindow = win;
});
});
after(() => {
return windows.remove(targetWindow.id);
});
it('runs test', () => {
expect(targetWindow.id).be.a('number');
});
});

@ -0,0 +1,35 @@
import { expect } from "chai";
import * as windows from "../ambassador/src/client/windows";
import * as tabs from "../ambassador/src/client/tabs";
import * as keys from "../ambassador/src/client/keys";
const SERVER_URL = "localhost:11111";
describe("tab test", function() {
let targetWindow;
let targetTab;
before(() => {
return windows.create().then((win) => {
targetWindow = win;
return tabs.create(win.id, SERVER_URL).then((tab) => {
targetTab = tab;
});
});
});
after(() => {
return windows.remove(targetWindow.id);
});
it('delete tab', (done) => {
let before = window.document.documentElement.scrollTop;
keys.press(targetTab.id, 'j').then((r) => {
});
keys.press(targetTab.id, 'j').then((r) => {
});
keys.press(targetTab.id, 'G').then((r) => {
});
let after = window.document.documentElement.scrollTop;
});
});

@ -5,6 +5,6 @@ return function(){
var args = arguments
setTimeout(() => {
start(args)
}, 1000);
}, 3000);
};
}(window.__karma__.start));