-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreddit.user.js
executable file
·247 lines (236 loc) · 6.83 KB
/
reddit.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
// ==UserScript==
// @name REDDIT
// @namespace http://github.com/steventheworker
// @version 0.1
// @description T N O C B - top new old controversial best, auto disable safesearch, auto enable dark mode, remove mobile/incognito popups
// @author steventheworker
// @grant none
// @icon https://www.redditstatic.com/desktop2x/img/favicon/android-icon-192x192.png
// @match https://www.reddit.com/*
// ==/UserScript==
let cache$isLoggedIn;
const isLoggedIn = () =>
cache$isLoggedIn
? cache$isLoggedIn
: (cache$isLoggedIn = doc.getElementById(
"email-collection-tooltip-id"
));
function openThread(el, newTab) {
if (!el) return;
if (el.nodeName !== "A")
return openThread(
Array.from(el.querySelectorAll("a")).filter(
(el, i) => el.children[0]?.children[0]?.nodeName === "H3"
)[0],
newTab
);
// win.open(el.href, newTab ? "_blank" : undefined);
el.focus();
}
function addEventListeners() {
win.addEventListener("keydown", function (e) {
if ($isInput(doc.activeElement)) return;
//sort
const threadSortPicker = doc.querySelector(
"button#CommentSort--SortPicker"
);
if (threadSortPicker) {
// thread / comment mode
const sortThreadMap = { B: 0, T: 1, N: 2, O: 4, C: 3, Q: 5 };
const selectIndex = sortThreadMap[e.key];
if (selectIndex !== undefined) {
//open menu, then click menu-item
threadSortPicker.click();
const container = Array.from(
doc.querySelectorAll('div[role="menu"]')
)
.filter((el, i) => el.parentNode.parentNode === bod)
.pop();
container.querySelectorAll("button")[selectIndex].click();
}
} else {
//subreddit mode
if (e.shiftKey) {
if (e.key === "Enter") openThread(doc.activeElement);
} else if (e.metaKey) {
if (e.key === "Enter") openThread(doc.activeElement, true);
}
const sortRedditdMap = {
B: "Best" /* sometimes DNE */,
H: "Hot",
N: "New",
T: "Top",
R: "Rising",
};
if (!sortRedditdMap[e.key]) return;
let links = doc.getElementsByClassName(
"ListingLayout-outerContainer"
);
if (links[0]) {
links = links[0].children[1];
links = links.children[links.children.length - 1].children[0];
if (
links.children[0].children.length > 3 &&
!links.children[0].children[0]
.getAttribute("href")
?.startsWith("/user/")
// &&
// !win.location.pathname.startsWith("/r/news")
)
links = links.children[0].children[1];
else links = links.children[1].children[1];
const linkLabel = sortRedditdMap[e.key];
const selectIndex = linkLabel
? (function getSelectIndex() {
let ret;
Array.from(links.children).forEach((el, i) => {
if (
el.href.endsWith(
linkLabel.toLowerCase() + "/"
)
)
ret = i;
});
return ret;
})()
: null;
if (selectIndex !== null) links.children[selectIndex].click();
} else {
let path = win.location.pathname.split("/"); // /r/aldi/comments/10q5yqc/is_this_the_red_bag_chicken_everyone_raves_about/
if (path[3] == "comments") {
const url = new URL(window.location.href);
url.searchParams.set(
"sort",
sortRedditdMap[e.key].toLowerCase()
);
window.location.href = url.toString();
}
}
}
});
// let downState = { e: {}, scrollY: win.scrollY };
win.downState = {};
win.addEventListener("mousedown", function (e) {
if (e.target.textContent === "Continue this thread") {
downState = { e, scrollY: win.scrollY };
}
});
win.addEventListener("mouseup", function (e) {
if (
downState &&
downState.e.target === e.target &&
e.target.textContent === "Continue this thread"
) {
setTimeout(() => win.scrollTo(win.scrollX, downState.scrollY), 167);
}
});
}
//init
(function () {
//reddit thread shortcut.user.js
addGlobalStyle(`
/* make content use more width */
._1OVBBWLtHoSPfGCRaPzpTf._3nSp9cdBpqL13CqjdMr2L_ { width: 90% !important;}
div { max-width: none !important; }
/* Join Reddit box */
.BtYn3oMRXzNwmNMkolecQ {display: none;}
`);
addEventListeners();
//reddit.user.js
addGlobalStyle(`
.m-slideIn {display: none;}
/* remove cat sliding popup (open in app) */
shreddit-drawer,
shreddit-drawer .content,
shreddit-experience-tree {
display: none !important;
}
`);
// auto-Disable safe search (when performing a search)
waitForEl(`#safe-search-toggle[aria-checked=true]`).then((el) =>
el.click()
);
/*
incognito mode helpers
*/
// remove iframe "Use your google account to sign in to Reddit"(single sign on google)
waitForEl("#SHORTCUT_FOCUSABLE_DIV iframe").then((sso) =>
isLoggedIn() ? 0 /* do nothing */ : sso.parentNode.remove()
);
//Automatic Dark mode
function openDropDown(dropDown) {
//open user settings dropdown
if (isLoggedIn()) return; //don't run if logged in
dropDown.click();
if (dropDown.getAttribute("aria-expanded") === "false")
setTimeout(() => openDropDown(dropDown), 84);
else {
//open Settings submenu
const carets = doc.getElementsByClassName("icon-caret_down");
const settingsCaret = carets[10] || carets[9];
if (
settingsCaret.parentNode.children[1] &&
settingsCaret.parentNode.children[1].textContent === "Settings"
) {
settingsCaret.click();
//click Dark Mode Btn if it's off
if (
settingsCaret.parentNode.parentNode.nextSibling.children[0].children[1].getAttribute(
"aria-checked"
) === "false"
) {
settingsCaret.parentNode.parentNode.nextSibling.children[0].click();
}
}
//auto close dropDown
dropDown.click();
}
}
waitForEl("#USER_DROPDOWN_ID").then((dropDown) => openDropDown(dropDown));
//Auto-Click "View Entire Discussion" - Spoiler button
const clickDiscussionSpoiler = () => {
if (isLoggedIn()) return;
const ray = Array.prototype.slice.call(
doc.querySelectorAll('button[tabindex="0"]')
);
ray.forEach((curBtn, i) => {
if (curBtn.innerHTML.startsWith("View Entire Discussion"))
curBtn.click();
});
if (!ray.length) setTimeout(clickDiscussionSpoiler, 1500);
};
onPageLoaded(() => {
clickDiscussionSpoiler();
});
})();
// wait for element to exist
// https://stackoverflow.com/a/61511955
function waitForEl(selector) {
return new Promise((resolve) => {
if (doc.querySelector(selector))
return resolve(doc.querySelector(selector));
const observer = new MutationObserver((mutations) => {
if (doc.querySelector(selector)) {
resolve(doc.querySelector(selector));
observer.disconnect();
}
});
observer.observe(bod, {
childList: true,
subtree: true,
});
});
}
//other helpers
function addGlobalStyle(css) {
//https://greasyfork.org/en/scripts/405073-wide-new-reddit-userscript
var head, style;
head = doc.getElementsByTagName("head")[0];
if (!head) {
return;
}
style = doc.createElement("style");
style.type = "text/css";
style.innerHTML = css;
head.appendChild(style);
}