-
Notifications
You must be signed in to change notification settings - Fork 27
/
oneClickOneOffSearchButtons.uc.js
357 lines (338 loc) · 13.4 KB
/
oneClickOneOffSearchButtons.uc.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// ==UserScript==
// @name One-click One-off Search Buttons
// @version 1.8.5
// @author aminomancer
// @homepageURL https://github.com/aminomancer
// @long-description
// @description
/*
Restore old behavior for one-off search engine buttons. It used to be that, if you entered a search term in the url bar, clicking a search engine button would immediately execute a search with that engine. This was changed in an update so that clicking the buttons only changes the "active" engine — you still have to press enter to actually execute the search.
You also used to be able to advance through your one-off search engine buttons by pressing left/right arrow keys. Until late 2021 these functions could be overridden with a preference in <about:config>, but those settings were removed. This script restores the old functionality. If you want to restore the one-click functionality but don't want the horizontal key navigation, go to <about:config> and toggle this custom setting to false: `userChrome.urlbar.oneOffs.keyNavigation`.
This script also has some conditional functions to work together with [scrollingOneOffs.uc.js][]. They don't require each other at all, but they heavily improve each other both functionally and visually. Changing search engines with the arrow keys will scroll the one-offs container to keep the selected one-off button in view. And exiting the query in any way will automatically scroll back to the beginning of the one-offs container, so that it's reset for the next time you use it.
It's hard to explain in words exactly what's going on so I'll just say to try them out yourself. The script also hides the one-off search settings button, but it can be unhidden by toggling `userChrome.urlbar.oneOffs.hideSettingsButton` in <about:config>.
[scrollingOneOffs.uc.js]: https://github.com/aminomancer/uc.css.js#scrolling-search-one-offs
*/
// @downloadURL https://cdn.jsdelivr.net/gh/aminomancer/uc.css.js@master/JS/oneClickOneOffSearchButtons.uc.js
// @updateURL https://cdn.jsdelivr.net/gh/aminomancer/uc.css.js@master/JS/oneClickOneOffSearchButtons.uc.js
// @license This Source Code Form is subject to the terms of the Creative Commons Attribution-NonCommercial-ShareAlike International License, v. 4.0. If a copy of the CC BY-NC-SA 4.0 was not distributed with this file, You can obtain one at http://creativecommons.org/licenses/by-nc-sa/4.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
// ==/UserScript==
(() => {
// change this in about:config if you don't want the arrow keys
// to switch between one-off search engine buttons.
const keyNavPref = "userChrome.urlbar.oneOffs.keyNavigation";
// change this in about:config if you don't want to disable the search settings button.
const hideSettingsPref = "userChrome.urlbar.oneOffs.hideSettingsButton";
// change this in about:config if you want arrow keys to ONLY cycle through
// urlbar results, rather than cycling through search engines and urlbar
// results. e.g. if you press arrow up after clicking the urlbar, normally it
// would select the last search engine rather than the last urlbar result. if
// this pref is set to true it will skip all the search engines and just go
// straight to the urlbar results.
const skipOneOffsPref = "userChrome.urlbar.oneOffs.skipOneOffsOnArrowKey";
const prefsvc = Services.prefs;
const branch = "userChrome.urlbar.oneOffs";
function init() {
let oneOffs = gURLBar.view.oneOffSearchButtons;
let searchbar = document.getElementById("PopupSearchAutoComplete");
let searchbarOneOffs = searchbar.oneOffButtons;
let handler = {
handleEvent(e) {
if (e.type === "unload") {
window.removeEventListener("unload", this);
prefsvc.removeObserver(branch, this);
}
},
observe(sub, _top, pref) {
switch (pref) {
case keyNavPref:
searchbarOneOffs.disableOneOffsHorizontalKeyNavigation =
oneOffs.disableOneOffsHorizontalKeyNavigation =
!sub.getBoolPref(pref);
break;
case hideSettingsPref:
toggleSettingsButton(sub.getBoolPref(pref));
break;
case skipOneOffsPref:
toggleKeyNavCallback(sub.getBoolPref(pref));
break;
}
},
attachListeners() {
window.addEventListener("unload", this);
prefsvc.addObserver(branch, this);
this.observe(prefsvc, null, keyNavPref);
this.observe(prefsvc, null, hideSettingsPref);
this.observe(prefsvc, null, skipOneOffsPref);
},
};
function rectX(el) {
return el.getBoundingClientRect().x;
}
function parseWidth(el) {
let style = window.getComputedStyle(el),
width = el.clientWidth,
margin = parseFloat(style.marginLeft) + parseFloat(style.marginRight),
padding =
parseFloat(style.paddingLeft) + parseFloat(style.paddingRight),
border =
parseFloat(style.borderLeftWidth) +
parseFloat(style.borderRightWidth);
return width + margin + padding + border;
}
function toggleSettingsButton(hide) {
if (hide) {
SearchOneOffs.prototype.getSelectableButtons = function () {
return [
...this.buttons.querySelectorAll(".searchbar-engine-one-off-item"),
];
};
for (let instance of [oneOffs, searchbarOneOffs]) {
instance.settingsButton.style.display = "none";
}
} else {
SearchOneOffs.prototype.getSelectableButtons = function (
aIncludeNonEngineButtons
) {
const buttons = [
...this.buttons.querySelectorAll(".searchbar-engine-one-off-item"),
];
if (aIncludeNonEngineButtons) buttons.push(this.settingsButton);
return buttons;
};
for (let instance of [oneOffs, searchbarOneOffs]) {
instance.settingsButton.style.removeProperty("display");
}
}
}
function toggleKeyNavCallback(disable) {
if (disable) {
const lazy = { UrlbarUtils };
gURLBar.view.controller._dismissSelectedResult = function (event) {
if (!this._lastQueryContextWrapper) {
console.error(
"Cannot dismiss selected result, last query not present"
);
return false;
}
let { queryContext } = this._lastQueryContextWrapper;
let { selectedElement } = this.input.view;
if (selectedElement?.classList.contains("urlbarView-button")) {
// For results with buttons, delete them only when the main part of the
// row is selected, not a button.
return false;
}
let result = this.input.view.selectedResult;
if (!result || result.heuristic) {
return false;
}
this.engagementEvent.record(event, {
result,
selType: "dismiss",
searchString: queryContext.searchString,
});
return true;
};
eval(
`gURLBar.view.controller.handleKeyNavigation = function ${gURLBar.view.controller.handleKeyNavigation
.toSource()
.replace(/#dismissSelectedResult/, "_dismissSelectedResult")
.replace(
/(this\.\_lastQueryContextWrapper)/,
`$1 && this.allowOneOffKeyNav`
)}`
);
} else {
delete gURLBar.view.controller._dismissSelectedResult;
delete gURLBar.view.controller.handleKeyNavigation;
}
}
oneOffs.slider = oneOffs.buttons.parentElement;
searchbarOneOffs.slider = searchbarOneOffs.buttons.parentElement;
oneOffs.handleSearchCommand = function (event, searchMode) {
if (
this.selectedButton == this.view.oneOffSearchButtons.settingsButton ||
this.selectedButton.classList.contains(
"searchbar-engine-one-off-add-engine"
)
) {
this.input.controller.engagementEvent.discard();
this.selectedButton.doCommand();
this.selectedButton = null;
return;
}
let startQueryParams = {
allowAutofill:
!searchMode.engineName &&
searchMode.source != UrlbarUtils.RESULT_SOURCE.SEARCH,
event,
};
let userTypedSearchString =
this.input.value &&
this.input.getAttribute("pageproxystate") != "valid";
let engine = Services.search.getEngineByName(searchMode.engineName);
let { where, params } = this._whereToOpen(event);
if (userTypedSearchString && engine) {
this.input.handleNavigation({
event,
oneOffParams: {
openWhere: where,
openParams: params,
engine: this.selectedButton.engine,
},
});
this.selectedButton = null;
if (
this.canScroll &&
!gURLBar.searchMode &&
!this.window.gBrowser.userTypedValue
) {
this.slider.scrollTo(0, 0);
}
return;
}
switch (where) {
case "current": {
this.input.searchMode = searchMode;
this.input.startQuery(startQueryParams);
break;
}
case "tab": {
searchMode.isPreview = false;
let newTab = this.input.window.gBrowser.addTrustedTab("about:newtab");
this.input.setSearchMode(searchMode, newTab.linkedBrowser);
if (userTypedSearchString) {
newTab.linkedBrowser.userTypedValue = this.input.value;
}
if (!params?.inBackground) {
this.input.window.gBrowser.selectedTab = newTab;
newTab.ownerGlobal.gURLBar.startQuery(startQueryParams);
}
break;
}
default: {
this.input.searchMode = searchMode;
this.input.startQuery(startQueryParams);
this.input.select();
break;
}
}
this.selectedButton = null;
if (
this.canScroll &&
!gURLBar.searchMode &&
!this.window.gBrowser.userTypedValue
) {
this.slider.scrollTo(0, 0);
}
};
oneOffs.scrollToButton = function (el) {
if (!el) el = oneOffs.buttons.firstElementChild;
let { slider } = this;
if (!slider) return;
let buttonX = rectX(el) - rectX(slider.firstElementChild);
let buttonWidth = parseWidth(el);
let midpoint = slider.clientWidth / 2;
slider.scrollTo({
left: buttonX + buttonWidth / 2 - midpoint,
behavior: "auto",
});
};
oneOffs.advanceSelection = function (
aForward,
aIncludeNonEngineButtons,
aWrapAround
) {
let buttons = this.getSelectableButtons(aIncludeNonEngineButtons);
let index;
if (this.selectedButton) {
let inc = aForward ? 1 : -1;
let oldIndex = buttons.indexOf(this.selectedButton);
index = (oldIndex + inc + buttons.length) % buttons.length;
if (
!aWrapAround &&
((aForward && index <= oldIndex) || (!aForward && oldIndex <= index))
) {
index = -1;
}
} else {
index = aForward ? 0 : buttons.length - 1;
}
this.selectedButton = index < 0 ? null : buttons[index];
if (this.canScroll) {
if (this.selectedButton) this.scrollToButton(this.selectedButton);
else this.slider.scrollTo(0, 0);
}
};
oneOffs.onViewOpen = function onViewOpen() {
this._on_popupshowing();
if (
this.canScroll &&
!gURLBar.searchMode &&
!this.window.gBrowser.userTypedValue
) {
this.slider.scrollTo(0, 0);
}
};
oneOffs.onViewClose = function onViewClose() {
this._on_popuphidden();
if (this.canScroll && !gURLBar.searchMode) this.slider.scrollTo(0, 0);
};
Object.defineProperty(oneOffs, "query", {
set(val) {
this._query = val;
if (this.isViewOpen) {
let isOneOffSelected =
this.selectedButton &&
this.selectedButton.classList.contains(
"searchbar-engine-one-off-item"
) &&
!(
this.selectedButton == this.settingsButton &&
this.hasAttribute("is_searchbar")
);
if (this.selectedButton && !isOneOffSelected) {
this.selectedButton = null;
}
if (this.canScroll && !gURLBar.searchMode) this.slider.scrollTo(0, 0);
}
},
get() {
return this._query;
},
});
if (SearchOneOffs.prototype._handleKeyDown.name) {
eval(
`SearchOneOffs.prototype._handleKeyDown = function ${SearchOneOffs.prototype._handleKeyDown
.toSource()
.replace(/_handleKeyDown/, "")
.replace(
/this\.selectedButton &&\n\s*this\.selectedButton\.engine &&\n\s*/g,
""
)}`
);
}
handler.attachListeners();
}
for (const pref of [
{ token: keyNavPref, default: true },
{ token: hideSettingsPref, default: true },
{ token: skipOneOffsPref, default: false },
]) {
prefsvc.getDefaultBranch("").setBoolPref(pref.token, pref.default);
}
// Delayed startup
if (gBrowserInit.delayedStartupFinished) {
init();
} else {
let delayedListener = (subject, topic) => {
if (topic == "browser-delayed-startup-finished" && subject == window) {
Services.obs.removeObserver(delayedListener, topic);
init();
}
};
Services.obs.addObserver(
delayedListener,
"browser-delayed-startup-finished"
);
}
})();