-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathpage_script.js
More file actions
61 lines (56 loc) · 2.09 KB
/
Copy pathpage_script.js
File metadata and controls
61 lines (56 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// source- https://github.com/philc/sheetkeys/blob/master/page_scripts/page_script.js
// This script gets inserted into the page by our content script.
// It receives requests from the content script to simulate keypresses.
// Messages are passed to this script via "doc-keys-simulate-keypress" events, which are dispatched
// on the window object by the content script.
// How to simulate a keypress in Chrome: http://stackoverflow.com/a/10520017/46237
// Note that we have to do this keypress simulation in an injected script, because events dispatched
// by content scripts do not preserve overridden properties.
// - args: an object with keys keyCode, shiftKey
const simulateKeyEvent = function(eventType, el, args) {
// How to do this in Chrome: http://stackoverflow.com/q/10455626/46237
const event = document.createEvent("KeyboardEvent");
Object.defineProperty(event, "keyCode", {
get() {
return this.keyCodeVal;
},
});
Object.defineProperty(event, "which", {
get() {
return this.keyCodeVal;
},
});
const mods = args.mods || {};
event.initKeyboardEvent(
eventType, // eventName
true, // canBubble
true, //canceleable
document.defaultView, // view
"", // keyIdentifier string
false, // (not sure)
!!mods.control, // control
!!mods.alt, // alt
!!mods.shift, // shift
!!mods.meta, // meta
args.keyCode, // keyCode
args.keyCode, // (not sure)
);
event.keyCodeVal = args.keyCode;
Object.defineProperty(event, "altKey", {
get() {
return !!mods.alt;
},
});
Object.defineProperty(event, "metaKey", {
get() {
return !!mods.meta;
},
});
el.dispatchEvent(event);
};
const editorEl = document.querySelector(".docs-texteventtarget-iframe").contentDocument.activeElement;
window.addEventListener("doc-keys-simulate-keypress", function(event) {
const args = event.detail
simulateKeyEvent("keydown", editorEl, args);
simulateKeyEvent("keyup", editorEl, args);
});