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
|
(() => {
/** @typedef {{ key: string; elem: HTMLElement; labelElem: HTMLElement | null }} Match */
const addRoot = () => {
const root = document.createElement('div');
document.body.appendChild(root);
return root;
}
/**
* @param match {Match}
* @return {Promise<HTMLElement | null>}
*/
const createLabelElem = (match) => new Promise((resolve, reject) => {
const label = Object.assign(document.createElement('div'), {
className: '__nullbrowser_findlabel',
textContent: match.key,
});
requestAnimationFrame(() => {
const rect = match.elem.getBoundingClientRect();
if (rect.top < 0 | rect.top > window.innerHeight) return reject(null);
if (rect.left < 0 | rect.left > window.innerWidth) return reject(null);
resolve(label);
requestAnimationFrame(() => {
Object.assign(label.style, {
position: 'fixed',
zIndex: 999999999, // maybe a few more 9s will do
top: `${rect.top}px`,
left: `${rect.left}px`,
backgroundColor: 'yellow',
border: '1px solid #000',
boxShadow: '0 0 3px #555',
color: '#222',
padding: '1px 2px',
fontSize: '10px',
fontWeight: 'bold',
fontFamily: 'monospace',
});
});
});
});
const finder = {
/** @type {string} */
keys: '',
/** @type {Array<Match>} */
matches: [],
/** @type {HTMLElement | null} */
labelsRoot: null,
/** @type {boolean} */
openInNewView: false,
stop() {
finder.matches = [];
finder.keys = '';
[...(finder.labelsRoot?.children ?? [])].forEach(child => {
child.remove();
});
},
/**
* @param selector {string}
* @param new_view {boolean}
*/
start(selector, new_view) {
finder.stop()
finder.labelsRoot ??= addRoot();
finder.createMatches(selector);
finder.openInNewView = new_view;
},
/** @param char {string} */
filterOutByKey(char) {
if (!/[0-9]+/.test(char)) return; // NOTE: temporary
finder.keys = finder.keys + char;
finder.matches = finder.matches.filter(m => {
const isMatch = m.key.startsWith(finder.keys);
if (!isMatch) {
m.labelElem?.remove();
return false;
}
return true;
});
if (finder.matches.length <= 1) {
const match = finder.matches[0]
finder.stop();
if (match) {
if (match.elem?.href) {
if (finder.openInNewView) {
window.open(match.elem?.href)
} else {
location.href = match.elem?.href
}
} else {
match.elem?.click()
}
}
}
},
/** @param selector {string} */
createMatches(selector) {
finder.matches = [];
const elements = document.querySelectorAll(selector);
const matches = [...elements].map(async (elem, index) => {
/** @type {Match} */
const match = {
elem,
key: `${index}`.padStart(`${elements.length}`.length, '0'),
labelElem: null,
}
match.labelElem = await createLabelElem(match);
return match
});
Promise.allSettled(matches).then(matches => {
matches.forEach(result => {
if (result.status !== 'fulfilled') return;
if (result.value.labelElem)
finder.labelsRoot.appendChild(result.value.labelElem)
finder.matches.push(result.value)
});
});
},
};
window._nullbrowser ||= {};
window._nullbrowser.finder ||= finder;
})();
|