Repository navigation
Expand file tree
/
Copy pathoneClickOneOffSearchButtons.uc.js
More file actions
259 lines (240 loc) · 12.7 KB
/
Copy pathoneClickOneOffSearchButtons.uc.js
File metadata and controls
259 lines (240 loc) · 12.7 KB
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
// ==UserScript==
// @name One-click One-off Search Buttons
// @version 1.7
// @author aminomancer
// @homepage https://github.com/aminomancer
// @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 recently these functions could be overridden with a preference in about:config, but those settings were removed, e.g. browser.urlbar.update2.disableOneOffsHorizontalKeyNavigation. 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 exactly what's going on so for now I'll just say to try them out yourself. The script also hides the one-off search settings button, but this can be turned off in about:config with userChrome.urlbar.oneOffs.hideSettingsButton.
// ==/UserScript==
(() => {
const prefsvc = Services.prefs;
const keyNavPref = "userChrome.urlbar.oneOffs.keyNavigation"; // change this in about:config if you don't want the arrow keys to switch between one-off search engine buttons.
const hideSettingsPref = "userChrome.urlbar.oneOffs.hideSettingsButton"; // change this in about:config if you don't want to disable the search settings button.
const skipOneOffsPref = "userChrome.urlbar.oneOffs.skipOneOffsOnArrowKey"; // 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 branch = "userChrome.urlbar.oneOffs";
let keyNav = true;
function init() {
let oneOffs = gURLBar.view.oneOffSearchButtons;
let handler = {
handleEvent(e) {
if (e.type === "unload") {
window.removeEventListener("unload", this, false);
prefsvc.removeObserver(branch, this);
gURLBar.inputField.removeEventListener("keydown", this, false);
return;
}
if (!gURLBar.view.isOpen || oneOffs.selectedButton || !keyNav) return;
if (
!oneOffs.input.value ||
oneOffs.input.getAttribute("pageproxystate") === "valid"
)
return;
if (e.keyCode === KeyboardEvent.DOM_VK_LEFT) {
oneOffs.advanceSelection(false, oneOffs.compact, true);
e.preventDefault();
e.stopImmediatePropagation();
}
if (e.keyCode === KeyboardEvent.DOM_VK_RIGHT) {
oneOffs.advanceSelection(true, oneOffs.compact, true);
e.preventDefault();
e.stopImmediatePropagation();
}
},
observe(sub, _top, pref) {
switch (pref) {
case keyNavPref:
if (sub.getBoolPref(pref)) {
oneOffs.disableOneOffsHorizontalKeyNavigation = false;
keyNav = true;
} else {
oneOffs.disableOneOffsHorizontalKeyNavigation = true;
keyNav = false;
}
break;
case hideSettingsPref:
toggleSettingsButton(sub.getBoolPref(pref));
break;
case skipOneOffsPref:
toggleKeyNavCallback(sub.getBoolPref(pref));
break;
}
},
attachListeners() {
window.addEventListener("unload", this, false);
prefsvc.addObserver(branch, this);
gURLBar.inputField.addEventListener("keydown", this, false);
this.observe(prefsvc, null, keyNavPref);
this.observe(prefsvc, null, hideSettingsPref);
this.observe(prefsvc, null, skipOneOffsPref);
},
};
function rectX(el) {
return el.getBoundingClientRect().x;
}
function toggleSettingsButton(hide) {
if (hide) {
oneOffs.getSelectableButtons = function () {
return [...this.buttons.querySelectorAll(".searchbar-engine-one-off-item")];
};
oneOffs.settingsButton.style.display = "none";
if (oneOffs.settingsButtonCompact)
oneOffs.settingsButtonCompact.style.display = "none";
} else {
delete oneOffs.getSelectableButtons;
oneOffs.settingsButton.style.removeProperty("display");
if (oneOffs.settingsButtonCompact)
oneOffs.settingsButtonCompact.style.removeProperty("display");
}
}
function toggleKeyNavCallback(disable) {
disable
? eval(
`gURLBar.view.controller.handleKeyNavigation = function ` +
gURLBar.view.controller.handleKeyNavigation
.toSource()
.replace(
/(this\.\_lastQueryContextWrapper)/,
`$1 && this.allowOneOffKeyNav`
)
)
: delete gURLBar.view.controller.handleKeyNavigation;
}
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.container.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.container.scrollTo(0, 0);
};
oneOffs.scrollToButton = function (el) {
let slider = el.parentElement;
let buttonX = rectX(el) - rectX(slider);
let midpoint = this.container.clientWidth / 2;
this.container.scrollTo({
left: buttonX - 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.container.scrollTo(0, 0);
};
oneOffs.onViewOpen = function onViewOpen() {
this._on_popupshowing();
if (this.canScroll && !gURLBar.searchMode && !this.window.gBrowser.userTypedValue)
this.container.scrollTo(0, 0);
};
oneOffs.onViewClose = function onViewClose() {
this._on_popuphidden();
if (this.canScroll && !gURLBar.searchMode) this.container.scrollTo(0, 0);
};
Object.defineProperty(oneOffs, "query", {
set: function (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.container.scrollTo(0, 0);
}
},
get: function () {
return this._query;
},
});
handler.attachListeners();
}
[
{ token: keyNavPref, default: true },
{ token: hideSettingsPref, default: true },
{ token: skipOneOffsPref, default: false },
].forEach((pref) => {
if (!prefsvc.prefHasUserValue(pref.token)) prefsvc.setBoolPref(pref.token, pref.default);
}); // create prefs early if they don't exist
// 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");
}
})();