-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathcontent.ts
306 lines (281 loc) · 12.9 KB
/
content.ts
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import { FirenvimElement } from "./FirenvimElement";
import { autofill } from "./autofill";
import { confReady, getConf } from "./utils/configuration";
import { getNeovimFrameFunctions, getActiveContentFunctions, getTabFunctions } from "./page";
if (document.location.href.startsWith("https://github.com/")
|| document.location.protocol === "file:" && document.location.href.endsWith("github.html")) {
addEventListener("load", autofill);
let lastUrl = location.href;
// We have to use a MutationObserver to trigger autofill because Github
// uses "progressive enhancement" and thus doesn't always trigger a load
// event. But we can't always rely on the MutationObserver without the load
// event because the MutationObserver won't be triggered on hard page
// reloads!
new MutationObserver(() => {
const url = location.href;
if (url !== lastUrl) {
lastUrl = url;
if (lastUrl === "https://github.com/glacambre/firenvim/issues/new") {
autofill();
}
}
}).observe(document, {subtree: true, childList: true});
}
// Promise used to implement a locking mechanism preventing concurrent creation
// of neovim frames
let frameIdLock = Promise.resolve();
export const firenvimGlobal = {
// Whether Firenvim is disabled in this tab
disabled: browser.runtime.sendMessage({
args: ["disabled"],
funcName: ["getTabValue"],
})
// Note: this relies on setDisabled existing in the object returned by
// getFunctions and attached to the window object
.then((disabled: boolean) => (window as any).setDisabled(disabled)),
// Promise-resolution function called when a frameId is received from the
// background script
frameIdResolve: (_: number): void => undefined,
// lastFocusedContentScript keeps track of the last content frame that has
// been focused. This is necessary in pages that contain multiple frames
// (and thus multiple content scripts): for example, if users press the
// global keyboard shortcut <C-n>, the background script sends a "global"
// message to all of the active tab's content scripts. For a content script
// to know if it should react to a global message, it just needs to check
// if it is the last active content script.
lastFocusedContentScript: 0,
// nvimify: triggered when an element is focused, takes care of creating
// the editor iframe, appending it to the page and focusing it.
nvimify: async (evt: { target: EventTarget }) => {
if (firenvimGlobal.disabled instanceof Promise) {
await firenvimGlobal.disabled;
}
// When creating new frames, we need to know their frameId in order to
// communicate with them. This can't be retrieved through a
// synchronous, in-page call so the new frame has to tell the
// background script to send its frame id to the page. Problem is, if
// multiple frames are created in a very short amount of time, we
// aren't guaranteed to receive these frameIds in the order in which
// the frames were created. So we have to implement a locking mechanism
// to make sure that we don't create new frames until we received the
// frameId of the previously created frame.
let lock;
while (lock !== frameIdLock) {
lock = frameIdLock;
await frameIdLock;
}
frameIdLock = new Promise(async (unlock: any) => {
// auto is true when nvimify() is called as an event listener, false
// when called from forceNvimify()
const auto = (evt instanceof FocusEvent);
const takeover = getConf().takeover;
if (firenvimGlobal.disabled || (auto && takeover === "never")) {
unlock();
return;
}
const firenvim = new FirenvimElement(
evt.target as HTMLElement,
firenvimGlobal.nvimify,
(id: number) => firenvimGlobal.firenvimElems.delete(id)
);
const editor = firenvim.getEditor();
// If this element already has a neovim frame, stop
const alreadyRunning = Array.from(firenvimGlobal.firenvimElems.values())
.find((instance) => instance.getElement() === editor.getElement());
if (alreadyRunning !== undefined) {
// The span might have been removed from the page by the page
// (this happens on Jira/Confluence for example) so we check
// for that.
const span = alreadyRunning.getSpan();
if (span.ownerDocument.contains(span)) {
alreadyRunning.show();
alreadyRunning.focus();
unlock();
return;
} else {
// If the span has been removed from the page, the editor
// is dead because removing an iframe from the page kills
// the websocket connection inside of it.
// We just tell the editor to clean itself up and go on as
// if it didn't exist.
alreadyRunning.detachFromPage();
}
}
if (auto && (takeover === "empty" || takeover === "nonempty")) {
const content = (await editor.getContent()).trim();
if ((content !== "" && takeover === "empty")
|| (content === "" && takeover === "nonempty")) {
unlock();
return;
}
}
firenvim.prepareBufferInfo();
const frameIdPromise = new Promise((resolve: (_: number) => void, reject) => {
firenvimGlobal.frameIdResolve = resolve;
// TODO: make this timeout the same as the one in background.ts
setTimeout(reject, 10000);
});
frameIdPromise.then((frameId: number) => {
firenvimGlobal.firenvimElems.set(frameId, firenvim);
firenvimGlobal.frameIdResolve = () => undefined;
unlock();
});
frameIdPromise.catch(unlock);
firenvim.attachToPage(frameIdPromise);
});
},
// fienvimElems maps frame ids to firenvim elements.
firenvimElems: new Map<number, FirenvimElement>(),
};
const ownFrameId = browser.runtime.sendMessage({ args: [], funcName: ["getOwnFrameId"] });
async function announceFocus () {
const frameId = await ownFrameId;
firenvimGlobal.lastFocusedContentScript = frameId;
browser.runtime.sendMessage({
args: {
args: [ frameId ],
funcName: ["setLastFocusedContentScript"]
},
funcName: ["messagePage"]
});
}
// When the frame is created, we might receive focus, check for that
ownFrameId.then(_ => {
if (document.hasFocus()) {
announceFocus();
}
});
async function addFocusListener () {
window.removeEventListener("focus", announceFocus);
window.addEventListener("focus", announceFocus);
}
addFocusListener();
// We need to use setInterval to periodically re-add the focus listeners as in
// frames the document could get deleted and re-created without our knowledge.
const intervalId = setInterval(addFocusListener, 100);
// But we don't want to syphon the user's battery so we stop checking after a second
setTimeout(() => clearInterval(intervalId), 1000);
export const frameFunctions = getNeovimFrameFunctions(firenvimGlobal);
export const activeFunctions = getActiveContentFunctions(firenvimGlobal);
export const tabFunctions = getTabFunctions(firenvimGlobal);
Object.assign(window, frameFunctions, activeFunctions, tabFunctions);
browser.runtime.onMessage.addListener(async (request: { funcName: string[], args: any[] }) => {
// All content scripts must react to tab functions
let fn = request.funcName.reduce((acc: any, cur: string) => acc[cur], tabFunctions);
if (fn !== undefined) {
return fn(...request.args);
}
// The only content script that should react to activeFunctions is the active one
fn = request.funcName.reduce((acc: any, cur: string) => acc[cur], activeFunctions);
if (fn !== undefined) {
if (firenvimGlobal.lastFocusedContentScript === await ownFrameId) {
return fn(...request.args);
}
return new Promise(() => undefined);
}
// The only content script that should react to frameFunctions is the one
// that owns the frame that sent the request
fn = request.funcName.reduce((acc: any, cur: string) => acc[cur], frameFunctions);
if (fn !== undefined) {
if (firenvimGlobal.firenvimElems.get(request.args[0]) !== undefined) {
return fn(...request.args);
}
return new Promise(() => undefined);
}
throw new Error(`Error: unhandled content request: ${JSON.stringify(request)}.`);
});
function onScroll(cont: boolean) {
window.requestAnimationFrame(() => {
const posChanged = Array.from(firenvimGlobal.firenvimElems.entries())
.map(([_, elem]) => elem.putEditorCloseToInputOrigin())
.find(changed => changed.posChanged);
if (posChanged) {
// As long as one editor changes position, try to resize
onScroll(true);
} else if (cont) {
// No editor has moved, but this might be because the website
// implements some kind of smooth scrolling that doesn't make
// the textarea move immediately. In order to deal with these
// cases, schedule a last redraw in a few milliseconds
setTimeout(() => onScroll(false), 100);
}
});
}
function doScroll() {
return onScroll(true);
}
function addNvimListener(elem: Element) {
elem.removeEventListener("focus", firenvimGlobal.nvimify);
elem.addEventListener("focus", firenvimGlobal.nvimify);
let parent = elem.parentElement;
while (parent) {
parent.removeEventListener("scroll", doScroll);
parent.addEventListener("scroll", doScroll);
parent = parent.parentElement;
}
}
function setupListeners(selector: string) {
window.addEventListener("scroll", doScroll);
window.addEventListener("wheel", doScroll);
(new ((window as any).ResizeObserver)((_: any[]) => {
onScroll(true);
})).observe(document.documentElement);
(new MutationObserver((changes, _) => {
if (changes.filter(change => change.addedNodes.length > 0).length <= 0) {
return;
}
// This mutation observer is triggered every time an element is
// added/removed from the page. When this happens, try to apply
// listeners again, in case a new textarea/input field has been added.
const toPossiblyNvimify = Array.from(document.querySelectorAll(selector));
toPossiblyNvimify.forEach(elem => addNvimListener(elem));
const takeover = getConf().takeover;
function shouldNvimify(node: any) {
// Ideally, the takeover !== "never" check shouldn't be performed
// here: it should live in nvimify(). However, nvimify() only
// checks for takeover === "never" if it is called from an event
// handler (this is necessary in order to allow manually nvimifying
// elements). Thus, we need to check if takeover !== "never" here
// too.
return takeover !== "never"
&& document.activeElement === node
&& toPossiblyNvimify.includes(node);
}
// We also need to check if the currently focused element is among the
// newly created elements and if it is, nvimify it.
// Note that we can't do this unconditionally: we would turn the active
// element into a neovim frame even for unrelated dom changes.
for (const mr of changes) {
for (const node of mr.addedNodes) {
if (shouldNvimify(node)) {
activeFunctions.forceNvimify();
return;
}
const walker = document.createTreeWalker(node, NodeFilter.SHOW_ELEMENT);
while (walker.nextNode()) {
if (shouldNvimify(walker.currentNode)) {
activeFunctions.forceNvimify();
return;
}
}
}
}
})).observe(window.document, { subtree: true, childList: true });
let elements: HTMLElement[];
try {
elements = Array.from(document.querySelectorAll(selector));
} catch {
alert(`Firenvim error: invalid CSS selector (${selector}) in your g:firenvim_config.`);
elements = [];
}
elements.forEach(elem => addNvimListener(elem));
}
export const listenersSetup = new Promise(resolve => {
confReady.then(() => {
const conf = getConf();
if (conf.selector !== undefined && conf.selector !== "") {
setupListeners(conf.selector);
}
resolve(undefined);
});
});