-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchfoxMods.user.js
283 lines (271 loc) · 12.4 KB
/
searchfoxMods.user.js
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
// ==UserScript==
// @name Searchfox Mods
// @version 1.2.4
// @author aminomancer
// @homepageURL https://github.com/aminomancer/searchfox-mods
// @supportURL https://github.com/aminomancer/searchfox-mods
// @downloadURL https://cdn.jsdelivr.net/gh/aminomancer/searchfox-mods@latest/searchfoxMods.user.js
// @namespace https://github.com/aminomancer
// @match *://searchfox.org/*
// @description Make Searchfox's context menu open on Right click, and make Alt + Left click and Ctrl + Left click instantly go to definitions or search for tokens. Modifier + Right click will open the normal Firefox/OS context menu. Middle click will open definition in a new background tab, and Shift + Middle click will do the same in a new foreground tab.
// @grant none
// @run-at document-idle
// @license This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/.
// @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADQAAAA0CAYAAADFeBvrAAAAAXNSR0IArs4c6QAACrZJREFUaAXVWl+IVccZnzl33V2vrrq6Utck7UN8kICBQIIG8hAfTDE0sVI01N0NTStrU+pDpbQEqYgghVTsQx5srKVFu5I2iDWkaU0oKyktFg0GCiLFl7TqrnV1d91Gd+89Z6a/3zcz55577r37r+42ObCeO3Pmz/f7ft/3zTczavUAHvvX7y2cuDa6SVm1URm1ViVqjTG2XVvVZo1VNlFjythhZfRVa9QVndj+4heWf6C3//T+A5i+aghdVZpBwb77anu5PLHFGr3FWvucTWwRAisIDgACQgGgUvxtlNL45sChkmWr71lj3tdJ4cyiZOEZ/dqR4RlM37DpjAHZi73FeMDusYn5gUlsG4WHcA4EwIAdAQJWPCC8WA8Q/GP77G+CVVaPRca+vnjlwsN619F7qJn1M21A1v62UP79+9+yVu23senMCuWYqAguYASEA8rveSDSh6xlAILNAWv1/qXrXviF3r6dKpnxMy1A9k/ffigeL//OxPZJmlXWdETrVQxAhsBGYExYqIAiCGEmtMuA4jet9MXmKPpq8eDx6zNFNCWg0tne9bZsTsPEqlipmBn9AxiCUJ6NUJe2o74J0JsnvxOUMJQyCHHSOj1QUHrr0kMn/jYTUNFkjeM/7OxW5fgcTKGTAgTNh0mDb/CNiCYCBxOi2Ukg8EDd98oYWTD0O7aVABIUAwUmZXPuzne7uieTMf+tIUMTBJPYE5wkmFkAEoA508sIybYCzGvesyBseEEDIzIm6oJSKmNnWXLjRDbqWX6k79d54euV6wKimak4OQcttwaNVyZ0AJxgFacGAyW070e7d4xRlxfoBTda8MdJJ8b/s7pcilZDM4+ZRL2IdWgjTLRZxqzxv0zID+Zn9LhShWc7jk5tfjWAXACILxgfySRiUfGiTecvVSCtGlSJPlBctLhPd79xt57W8nV2/+4lo2a4C/32YZ5VBCbrF95VvuUBiR8aNaCbFzzV8bPJA0UVIBeaz56HwIhmACETVcwn1BFQZPUEzPFga0vHYf3yoU/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k9BAwkkXelXOXgvi3rQn14p/2zYBDNEH5q8rRa+exSnFad/ddjX1sfvk0JiA07jh+/3vECbs6U3oWFd4BC0KyqQNA3KBS/Zf6ygkv79JtrL6YWwGg1rHX0nS++9PiTjGazATWlyUG8qsf29hZv/ntkDwKC+68xEFBCuRdUAJAJb14CjrILOxiKwgtjLiORMnIzpjPMALKLJjVPBtBpaZUQ9QrM7wotj88YUBhrZMeO9vsjZfzHJb1F47AfCaWcjwd2nC9B+nxk8+DANFVwgVkzE81GudmMQCl9ataAAjC+eQtwc0htMhZnzTieRaheA5DtuN5vwztWNhrCbfcQ/PAWrvU/joz6kJuzevuZ7LjhtwOFPZtSk++cabKh02f9PU2mxqYVFD4LYJnuTBkotP7z54ahoFRhypo/IpgsC3V8a63uRbb5ic8NQ0F4MsVoBginsJcYRv0Y0LxHMA9f+c0//gtz9tR788rKkQAAAABJRU5ErkJggg==
// ==/UserScript==
function waiveXray(obj) {
if (typeof obj === "object") {
return "wrappedJSObject" in obj ? obj.wrappedJSObject : obj;
}
return obj;
}
function isMac() {
return navigator.platform.startsWith("Mac");
}
function accelKeyPressed(event) {
return isMac() ? event.metaKey : event.ctrlKey;
}
function getContextMenu() {
return waiveXray(window).ContextMenu;
}
function openLink(href, event) {
let newTarget = Object.assign(document.createElement("a"), { href });
let newEvent = new MouseEvent("click", {
button: 0,
which: event.which,
altKey: false,
shiftKey: event.shiftKey,
ctrlKey: event.ctrlKey || (!isMac() && event.button === 1),
metaKey: event.metaKey || (isMac() && event.button === 1),
});
newTarget.dispatchEvent(newEvent);
event.preventDefault();
}
function makeLinkOpener(href) {
return event => {
openLink(href, event);
};
}
function getOptions(node) {
let options = {};
if (!node.closest("code") && !node.closest("svg")) {
return options;
}
let tree = document.getElementById("data").getAttribute("data-tree");
let sourceLineClicked = null;
{
let sourceLineNode = node.closest("code");
let lineNumberNode = sourceLineNode?.previousElementSibling;
let sourcePath = Router.sourcePath;
if (lineNumberNode && sourcePath) {
sourceLineClicked = `${sourcePath}#${lineNumberNode.dataset.lineNumber}`;
}
}
let jumps = [];
let searches = [];
let extras = [];
let symbolToken = node.closest("[data-symbols]");
if (symbolToken) {
let symbols = symbolToken.getAttribute("data-symbols").split(",");
const seenSyms = new Set();
const exposeSymbolsForDebugging = [];
waiveXray(window).CLICKED_SYMBOLS = cloneInto(
exposeSymbolsForDebugging,
window,
{ cloneFunctions: true }
);
for (const sym of symbols) {
if (seenSyms.has(sym)) continue;
const symInfo = waiveXray(window).SYM_INFO[sym];
if (!symInfo) continue;
let symSearches = [];
let diagrammableSyms = [symInfo];
exposeSymbolsForDebugging.push(symInfo);
const jumpify = jumpref => {
if (!jumpref.jumps) {
return;
}
if (jumpref.jumps.idl && jumpref.jumps.idl !== sourceLineClicked) {
jumps.push(`/${tree}/source/${jumpref.jumps.idl}`);
}
if (jumpref.jumps.def && jumpref.jumps.def !== sourceLineClicked) {
jumps.push(`/${tree}/source/${jumpref.jumps.def}`);
}
if (jumpref.jumps.decl && jumpref.jumps.decl !== sourceLineClicked) {
jumps.push(`/${tree}/source/${jumpref.jumps.decl}`);
}
};
const searchify = query => {
symSearches.push(
`/${tree}/search?q=symbol:${encodeURIComponent(query)}&redirect=false`
);
};
jumpify(symInfo);
if (symInfo.meta?.slotOwner) {
let slotOwner = symInfo.meta.slotOwner;
let ownerJumpref = waiveXray(window).SYM_INFO[slotOwner.sym];
if (ownerJumpref) {
jumpify(ownerJumpref);
}
}
searchify(sym);
if (symInfo.meta?.bindingSlots) {
let implKind = symInfo.meta.implKind || "impl";
if (implKind === "idl") {
implKind = "IDL";
diagrammableSyms = [];
}
let allSearchSyms = [];
for (const slot of symInfo.meta.bindingSlots) {
let slotJumpref = waiveXray(window).SYM_INFO[slot.sym];
if (slot.slotLang) {
let lang = slot.slotLang.toUpperCase();
if (lang === "CPP") {
diagrammableSyms.push(slotJumpref);
}
}
searchify(slot.sym);
allSearchSyms.push(slot.sym);
if (slotJumpref) {
jumpify(slotJumpref);
}
}
if (allSearchSyms.length > 1) {
if (implKind === "IDL") {
symSearches.shift();
allSearchSyms.unshift(sym);
}
searchify(allSearchSyms.join(","));
}
}
searches = searches.concat(symSearches);
if (Settings.diagramming.enabled) {
for (const jumpref of diagrammableSyms) {
extras.push(
`/${tree}/query/default?q=${encodeURIComponent(
`calls-to:'${jumpref.pretty}' depth:4`
)}`
);
}
}
}
}
for (let jump of jumps) {
if (jump) {
options.goToDefinition = makeLinkOpener(jump);
break;
}
}
for (let search of searches) {
if (search) {
options.symbolSearch = makeLinkOpener(search);
break;
}
}
let word = waiveXray(window).getTargetWord();
if (word) {
options.wordSearch = makeLinkOpener(
`/${tree}/search?q=${encodeURIComponent(word)}&redirect=false`
);
}
for (let extra of extras) {
if (extra) {
options.extraLink = makeLinkOpener(extra);
}
break;
}
if (symbolToken) {
let symbols = symbolToken.getAttribute("data-symbols")?.split(",");
let visibleToken = symbolToken.textContent;
options.stickyHighlight = () => {
waiveXray(window).Hover.stickyHighlight(symbols, visibleToken);
};
}
return options;
}
function setCaret(event) {
const selection = window.getSelection();
const start = document.caretPositionFromPoint(event.pageX, event.pageY);
const range = document.createRange();
range.setStart(start.offsetNode, start.offset);
range.collapse(true);
selection.removeAllRanges();
selection.addRange(range);
event.target.focus();
}
getContextMenu().tryShowOnClick = exportFunction(function (event) {
let options = getOptions(event.target);
if (event.altKey || accelKeyPressed(event)) {
if (options.goToDefinition) {
options.goToDefinition(event);
} else if (options.symbolSearch) {
options.symbolSearch(event);
} else if (options.wordSearch && event.target.closest(".hovered")) {
options.wordSearch(event);
} else if (options.extraLink) {
options.extraLink(event);
} else if (options.stickyHighlight) {
options.stickyHighlight();
} else {
Object.getPrototypeOf(getContextMenu())?.tryShowOnClick.call(this, event);
}
return;
}
options.stickyHighlight?.();
}, window);
window.addEventListener("mousedown", event => {
if (!event.altKey && !accelKeyPressed(event) && event.button !== 1) {
return;
}
// Send a mousedown event without highlighting any text on shift click
let newEvent = new MouseEvent("mousedown", {
button: event.button,
which: event.which,
altKey: event.altKey,
shiftKey: false,
ctrlKey: event.ctrlKey,
metaKey: event.metaKey,
});
event.target.dispatchEvent(newEvent);
event.preventDefault();
event.stopPropagation();
setCaret(event);
});
window.addEventListener("auxclick", event => {
switch (event.button) {
case 1:
let options = getOptions(event.target);
if (options.goToDefinition) {
options.goToDefinition(event);
} else if (options.symbolSearch) {
options.symbolSearch(event);
} else if (options.wordSearch && event.target.closest(".hovered")) {
options.wordSearch(event);
} else if (options.extraLink) {
options.extraLink(event);
} else {
return;
}
break;
case 2:
if (event.shiftKey || event.altKey) return;
let contextMenu = getContextMenu();
setCaret(event);
Object.getPrototypeOf(contextMenu)?.tryShowOnClick.call(
contextMenu,
event
);
break;
default:
return;
}
event.preventDefault();
event.stopPropagation();
});
window.addEventListener("contextmenu", event => {
if (getContextMenu().menu.style.display === "none") {
return;
}
event.preventDefault();
event.stopPropagation();
});