parent
bb5b6d367c
commit
f6f64297aa
10 changed files with 149 additions and 0 deletions
@ -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); |
||||
} |
||||
}); |
||||
|
@ -0,0 +1,7 @@ |
||||
const receiveContentMessage = (func) => { |
||||
browser.runtime.onMessage.addListener((message) => { |
||||
return func(message); |
||||
}); |
||||
}; |
||||
|
||||
export { receiveContentMessage }; |
@ -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 }; |
@ -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 }; |
@ -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); |
||||
} |
||||
}); |
@ -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 }; |
@ -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, |
||||
}; |
@ -0,0 +1,19 @@ |
||||
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,10 @@ |
||||
'use strict'; |
||||
|
||||
window.__karma__.start = (function(start){ |
||||
return function(){ |
||||
var args = arguments |
||||
setTimeout(() => { |
||||
start(args) |
||||
}, 1000); |
||||
}; |
||||
}(window.__karma__.start)); |
Reference in new issue