-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfandom-press-esc-for-search.user.js
87 lines (69 loc) · 2.7 KB
/
fandom-press-esc-for-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
// ==UserScript==
// @name Fandom press ESC for search + tweaks
// @namespace https://github.com/Alistair1231/my-userscripts/
// @version 0.2.5
// @description when you press esc on a fandom wiki it will now open and select the search bar, also makes the page wider and disables highlight actions
// @author Alistair1231
// @match *://*.fandom.com/wiki/*
// @grant GM.addStyle
// @license MIT
// ==/UserScript==
// https://greasyfork.org/en/scripts/454358-fandom-press-esc-for-search-widescreen
// https://openuserjs.org/scripts/Alistair1231/Fandom_press_ESC_for_search_+_widescreen
// https://github.com/Alistair1231/my-userscripts/raw/master/fandom-press-esc-for-search.user.js
(function () {
'use strict';
GM.addStyle(`
#highlight__main-container {
display: none;
}
`);
const qS = (x) => document.querySelector(x);
// const qSA = (x) => document.querySelectorAll(x);
function resizeContainer() {
var resizableContainer = qS(".resizable-container");
if (resizableContainer && resizableContainer.offsetLeft > 200) {
resizableContainer.style.maxWidth = "80%";
}else{
resizableContainer.style.maxWidth = "100%";
}
}
// observe for changes in the document body (this is for when a new page is clicked)
var observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
// check if the mutation is a node insertion
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
for (var i = 0; i < mutation.addedNodes.length; i++) {
resizeContainer();
}
}
});
});
// resize the container on window resize
window.addEventListener('resize', function () {
resizeContainer();
});
// resize the container if already present on the page
resizeContainer();
observer.observe(document.body, { childList: true, subtree: true });
var active = false;
window.addEventListener('keydown', function (e) {
if (e.key === "Escape") {
e.preventDefault();
console.log(active);
// close search if it is open
if (active) {
document.getElementById('firstHeading').click();
active = false;
}
// open search
else {
const searchButton = document.querySelector("header.fandom-community-header a.wiki-tools__search[title='Search']");
if (searchButton) {
searchButton.click();
active = true;
}
}
}
});
})();