-
Notifications
You must be signed in to change notification settings - Fork 2
/
youtube-middle-click-search.user.js
96 lines (87 loc) · 2.79 KB
/
youtube-middle-click-search.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
// ==UserScript==
// @name Youtube Middle Click Search
// @namespace https://greasyfork.org/users/649
// @version 2.1.7
// @description Middle clicking the search on youtube opens the results in a new tab
// @author Adrien Pyke
// @match *://www.youtube.com/*
// @require https://cdn.jsdelivr.net/gh/fuzetsu/userscripts@ec863aa92cea78a20431f92e80ac0e93262136df/wait-for-elements/wait-for-elements.js
// @grant GM_openInTab
// ==/UserScript==
(function () {
'use strict';
const SCRIPT_NAME = 'YMCS';
const Util = {
log(...args) {
args.unshift(`%c${SCRIPT_NAME}:`, 'font-weight: bold;color: #233c7b;');
console.log(...args);
},
q(query, context = document) {
return context.querySelector(query);
},
qq(query, context = document) {
return Array.from(context.querySelectorAll(query));
},
getQueryParameter(name, url = window.location.href) {
name = name.replace(/[[\]]/gu, '\\$&');
const regex = new RegExp(`[?&]${name}(=([^&#]*)|&|#|$)`, 'u'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/gu, ' '));
},
encodeURIWithPlus(string) {
return encodeURIComponent(string).replace(/%20/gu, '+');
}
};
waitForElems({
sel: '#search-icon-legacy',
stop: true,
onmatch(btn) {
btn.onmousedown = function (e) {
if (e.button === 1) {
e.preventDefault();
}
};
btn.onclick = function (e) {
e.preventDefault();
e.stopImmediatePropagation();
const input = Util.q('input#search').value.trim();
if (!input) return false;
const url = `${
location.origin
}/results?search_query=${Util.encodeURIWithPlus(input)}`;
if (e.button === 1) {
GM_openInTab(url, true);
} else if (e.button === 0) {
window.location.href = url;
}
return false;
};
btn.onauxclick = btn.onclick;
}
});
waitForElems({
sel: '.sbsb_c',
onmatch(result) {
result.onclick = function (e) {
if (!e.target.classList.contains('sbsb_i')) {
const search = Util.q('.sbpqs_a, .sbqs_c', result).textContent;
const url = `${
location.origin
}/results?search_query=${Util.encodeURIWithPlus(search)}`;
if (e.button === 1) {
GM_openInTab(url, true);
} else if (e.button === 0) {
window.location.href = url;
}
} else if (e.button === 1) {
// prevent opening in new tab if they middle click the remove button
e.preventDefault();
e.stopImmediatePropagation();
}
};
result.onauxclick = result.onclick;
}
});
})();