2017-10-02 00:59:51 +01:00
|
|
|
/** Shim for the keyboard API because it won't hit in FF57. */
|
2017-09-25 04:44:56 +01:00
|
|
|
|
2017-10-02 00:59:51 +01:00
|
|
|
// {{{ Helper functions
|
2017-09-25 04:44:56 +01:00
|
|
|
|
2017-10-02 00:59:51 +01:00
|
|
|
function pick (o, ...props) {
|
|
|
|
return Object.assign({}, ...props.map(prop => ({[prop]: o[prop]})))
|
|
|
|
}
|
2017-09-25 04:44:56 +01:00
|
|
|
|
2017-10-02 00:59:51 +01:00
|
|
|
// Shallow copy of keyevent.
|
|
|
|
function shallowKeyboardEvent (ke): Event {
|
|
|
|
let shallow = pick(
|
|
|
|
ke,
|
|
|
|
'shiftKey', 'metaKey', 'altKey', 'ctrlKey', 'repeat', 'key',
|
|
|
|
'bubbles', 'composed', 'defaultPrevented', 'eventPhase',
|
|
|
|
'timeStamp', 'type', 'isTrusted'
|
|
|
|
)
|
|
|
|
shallow.target = pick(ke.target, 'tagName')
|
|
|
|
shallow.target.ownerDocument = pick(ke.target.ownerDocument, 'URL')
|
|
|
|
return shallow
|
|
|
|
} // }}}
|
|
|
|
|
|
|
|
function keyeventHandler(ke: KeyboardEvent) {
|
|
|
|
// Suppress events, if requested
|
|
|
|
if (preventDefault) {
|
|
|
|
ke.preventDefault()
|
2017-09-25 04:44:56 +01:00
|
|
|
}
|
2017-10-02 00:59:51 +01:00
|
|
|
if (stopPropagation) {
|
|
|
|
ke.stopPropagation()
|
2017-09-25 04:44:56 +01:00
|
|
|
}
|
2017-10-02 00:59:51 +01:00
|
|
|
browser.runtime.sendMessage({command: "keydown", event: shallowKeyboardEvent(ke)})
|
|
|
|
}
|
2017-09-25 04:44:56 +01:00
|
|
|
|
2017-10-02 00:59:51 +01:00
|
|
|
// Listen for suppression messages from bg script.
|
|
|
|
function backgroundListener(message) {
|
|
|
|
if (message.command === "keydown-suppress") {
|
|
|
|
if ('preventDefault' in message.data) {
|
|
|
|
preventDefault = message.data.preventDefault
|
|
|
|
}
|
|
|
|
if ('stopPropagation' in message.data) {
|
|
|
|
stopPropagation = message.data.stopPropagation
|
|
|
|
}
|
2017-09-25 04:44:56 +01:00
|
|
|
}
|
2017-10-02 00:59:51 +01:00
|
|
|
}
|
2017-09-25 04:44:56 +01:00
|
|
|
|
2017-10-02 00:59:51 +01:00
|
|
|
// State
|
|
|
|
let preventDefault = false
|
|
|
|
let stopPropagation = false
|
2017-09-25 04:44:56 +01:00
|
|
|
|
2017-10-02 00:59:51 +01:00
|
|
|
// Add listeners
|
|
|
|
window.addEventListener("keydown", keyeventHandler)
|
|
|
|
browser.runtime.onMessage.addListener(backgroundListener)
|
2017-09-25 04:44:56 +01:00
|
|
|
|
2017-10-02 00:59:51 +01:00
|
|
|
// Dummy export so that TS treats this as a module.
|
|
|
|
export {}
|