File size: 2,634 Bytes
89a2873
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { WebContents } from "electron"

const focusDebuggerOwners = new WeakSet<WebContents>()
const forcedFocusNodes = new WeakMap<WebContents, number[]>()
const focusableSelector = `
  a[href],
  button:not([disabled]),
  input:not([disabled]),
  select:not([disabled]),
  textarea:not([disabled]),
  summary,
  [contenteditable="true"],
  [tabindex]:not([tabindex="-1"])
`

export async function setForceFocus(contents: WebContents, enabled: boolean) {
  const debuggerApi = contents.debugger
  if (!debuggerApi.isAttached()) {
    if (!enabled) {
      focusDebuggerOwners.delete(contents)
      forcedFocusNodes.delete(contents)
      return
    }
    debuggerApi.attach("1.3")
    focusDebuggerOwners.add(contents)
    debuggerApi.once("detach", () => {
      focusDebuggerOwners.delete(contents)
      forcedFocusNodes.delete(contents)
    })
  }

  if (!enabled) {
    await Promise.allSettled(
      (forcedFocusNodes.get(contents) ?? []).map((nodeId) =>
        debuggerApi.sendCommand("CSS.forcePseudoState", {
          nodeId,
          forcedPseudoClasses: [],
        }),
      ),
    )
    forcedFocusNodes.delete(contents)
    if (!focusDebuggerOwners.delete(contents)) return
    debuggerApi.detach()
    return
  }

  await debuggerApi.sendCommand("DOM.enable")
  await debuggerApi.sendCommand("CSS.enable")
  const document: unknown = await debuggerApi.sendCommand("DOM.getDocument", {
    depth: -1,
    pierce: true,
  })
  const nodes: unknown = await debuggerApi.sendCommand("DOM.querySelectorAll", {
    nodeId: readDocumentNodeId(document),
    selector: focusableSelector,
  })
  const nodeIds = readNodeIds(nodes)
  forcedFocusNodes.set(contents, [...new Set([...(forcedFocusNodes.get(contents) ?? []), ...nodeIds])])
  await Promise.allSettled(
    nodeIds.map((nodeId) =>
      debuggerApi.sendCommand("CSS.forcePseudoState", {
        nodeId,
        forcedPseudoClasses: ["focus", "focus-visible"],
      }),
    ),
  )
}

function readDocumentNodeId(value: unknown) {
  if (
    !value ||
    typeof value !== "object" ||
    !("root" in value) ||
    !value.root ||
    typeof value.root !== "object" ||
    !("nodeId" in value.root) ||
    typeof value.root.nodeId !== "number"
  ) {
    throw new Error("Invalid DOM.getDocument response")
  }
  return value.root.nodeId
}

function readNodeIds(value: unknown) {
  if (
    !value ||
    typeof value !== "object" ||
    !("nodeIds" in value) ||
    !Array.isArray(value.nodeIds) ||
    !value.nodeIds.every((nodeId) => typeof nodeId === "number")
  ) {
    throw new Error("Invalid DOM.querySelectorAll response")
  }
  return value.nodeIds
}