-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcfqol.user.js
265 lines (238 loc) · 10.9 KB
/
cfqol.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// ==UserScript==
// @name Curseforge QOL Fixes
// @version 0.27
// @description Various Quality of Life improvements to the Curseforge website
// @author comp500
// @namespace https://infra.link/
// @match https://www.curseforge.com/*
// @match https://legacy.curseforge.com/*
// @updateURL https://github.com/comp500/Curseforge-Userscripts/raw/master/cfqol.user.js
// @downloadURL https://github.com/comp500/Curseforge-Userscripts/raw/master/cfqol.user.js
// @homepageURL https://github.com/comp500/Curseforge-Userscripts/
// @supportURL https://github.com/comp500/Curseforge-Userscripts/issues/
// @source https://github.com/comp500/Curseforge-Userscripts/
// @run-at document-end
// @grant none
// ==/UserScript==
(function () {
"use strict";
// Add a search box
let searchBoxContainer = document.createElement("div");
searchBoxContainer.className = "flex mr-4 items-center";
// Get the current assets path
let styleSheet = Array.from(document.styleSheets).find((s) => /\/Content\/([\d\-]+)\//.test(s.href));
let assetsPath = styleSheet == null ? "2-0-7179-35052" : /\/Content\/([\d\-]+)\//.exec(styleSheet.href)[1];
searchBoxContainer.innerHTML = `<form action="/minecraft/mc-mods/search" method="get" novalidate="novalidate" autocomplete="false" style="width:100%">
<div class="flex flex-col h-full justify-between">
<div class="input input--icon" style="color: #000">
<i class="search textgray-900 flex items-center justify-center">
<svg class="icon" viewBox="0 0 20 20" width="16" height="16"><use xlink:href="/Content/${assetsPath}/Skins/CurseForge/images/twitch/Object/Search.svg#Object/Search"></use></svg>
</i>
<input type="text" name="search" id="cfqolTopbarSearch" placeholder="Search Mods">
</div>
</div></form>`;
let insertLocation = document.querySelector(".curseforge-header .ml-auto > div");
if (insertLocation != null && insertLocation.firstChild != null) {
// @Inject(method = "the navbar", at = @At("HEAD"))
insertLocation.insertBefore(searchBoxContainer, insertLocation.firstChild);
// Make the search box magically grow
let searchBox = searchBoxContainer.querySelector("#cfqolTopbarSearch");
if (searchBox != null) {
// Fix stupid flexboxes - set to flex-grow 1 flex-shrink 0
searchBoxContainer.style.flex = "1 0";
searchBoxContainer.parentNode.parentNode.style.flex = "1 0";
let navLinksContainer = document.querySelector(".top-nav__nav-link").parentNode;
navLinksContainer.style.transition = "opacity 0.4s, max-width 0.3s";
searchBox.addEventListener("focus", (e) => {
navLinksContainer.style.opacity = 0;
navLinksContainer.style.maxWidth = "0";
});
searchBox.addEventListener("blur", (e) => {
navLinksContainer.style.opacity = 1;
navLinksContainer.style.maxWidth = "2000px";
});
// Make the search icon focus the search box
let searchIcon = searchBoxContainer.querySelector(".search");
if (searchIcon != null) {
searchIcon.addEventListener("click", (e) => {
searchBox.focus();
});
}
}
}
// Add an "All Files" tab for all curseforge projects
let projectPathMatches = /^\/([\w-]+)\/([\w-]+)\/([a-z][\da-z-_]{0,127})/.exec(document.location.pathname);
let filesTab = document.getElementById("nav-files");
if (projectPathMatches != null && projectPathMatches.length == 4 && filesTab != null) {
let gameName = projectPathMatches[1];
let projectCategory = projectPathMatches[2];
let projectSlug = projectPathMatches[3];
let projectAllFiles = document.createElement("li");
let isAllFilesPage = /\/files\/all$/.test(document.location.pathname);
if (isAllFilesPage) {
projectAllFiles.className =
"border-b-2 border-primary-500 b-list-item p-nav-item px-2 pb-1/10 -mb-1/10 text-gray-500";
filesTab.className = "b-list-item p-nav-item px-2 pb-1/10 -mb-1/10 text-gray-500";
filesTab.getElementsByTagName("a")[0].className = "text-gray-500 hover:no-underline";
} else {
projectAllFiles.className = "b-list-item p-nav-item px-2 pb-1/10 -mb-1/10 text-gray-500";
}
projectAllFiles.innerHTML = `<a href="/${gameName}/${projectCategory}/${projectSlug}/files/all" class="text-${
isAllFilesPage ? "primary" : "gray"
}-500 hover:no-underline">
<span class="b-list-label">
All Files
</span>
</a>`;
filesTab.parentNode.insertBefore(projectAllFiles, filesTab.nextSibling);
}
// Add pagination to the bottom of the page in dependency lists
let dependenciesPage = document.querySelector(".project-dependencies-page > div");
if (dependenciesPage != null) {
let paginationTop = document.querySelector(".project-dependencies-page > div .pagination-top");
if (paginationTop != null) {
dependenciesPage.appendChild(paginationTop.parentNode.cloneNode(true)).classList.remove("mb-4");
}
}
// Skip download countdowns
let downloadScript = Array.from(document.scripts).find(
(s) => s.innerText != null && s.innerText.includes("PublicProjectDownload.countdown")
);
if (downloadScript != null && downloadScript.innerText != null) {
let matches = downloadScript.innerText.match(/countdown\("(.+)"\)/);
if (matches != null && matches[1] != null) {
// Break the existing script
let countdownEl = document.querySelector("span[data-countdown-seconds]");
if (countdownEl != null) {
// UNSAFE if grant != none! For some reason jQuery stores data in itself rather than attrs?!
jQuery.removeData(countdownEl, "countdown-seconds");
}
let downloadText = document.querySelector("p[data-countdown-timer]");
if (downloadText != null) {
downloadText.innerText = "Downloading now...";
}
window.location.href = matches[1];
}
}
/**
* Link redirections
*/
const linkList = Array.from(document.getElementsByTagName("a"));
const regexDownloadLink = /^https:\/\/(www|legacy).curseforge.com\/.*\/download\/\d+$/;
const redirections = [
// Better method for skipping, if links contain file ID already
[regexDownloadLink, a => {
a.href = a.href + "/file";
}],
// Change the default Minecraft tab (from other links) to /minecraft/mc-mods
[/^http:\/\/bit.ly\/2Lzpfsl|https:\/\/(www|legacy).curseforge.com\/minecraft\/?$/, a => {
a.path = "/minecraft/mc-mods";
}],
// Change the default member tab to projects
[/^https:\/\/(www|legacy).curseforge.com\/members\/[^\/]+\/?$/, a => {
a.href = a.href + (a.href.endsWith("/") ? "" : "/") + "projects";
}],
// Redirect linkout URLs
[/^https:\/\/(www|legacy).curseforge.com\/linkout/, a => {
let url = new URL(a.href);
a.href = decodeURIComponent(url.searchParams.get("remoteUrl"));
}],
// Change the default dependency type to "Include" for Minecraft modpacks
[/^https:\/\/(www|legacy).curseforge.com\/minecraft\/modpacks\/([a-z][\da-z-_]{0,127})\/relations\/dependencies\/?$/, a => {
a.href = a.href + "?filter-related-dependencies=6";
}],
// Change the default dependency type to "Required Dependency"
[/^https:\/\/(www|legacy).curseforge.com\/([\w-]+)\/([\w-]+)\/([a-z][\da-z-_]{0,127})\/relations\/dependencies\/?$/, a => {
a.href = a.href + "?filter-related-dependencies=3";
}],
// Change the default dependent type to "Required Dependency"
[/^https:\/\/(www|legacy).curseforge.com\/([\w-]+)\/([\w-]+)\/([a-z][\da-z-_]{0,127})\/relations\/dependents\/?$/, a => {
a.href = a.href + "?filter-related-dependents=3";
}]
];
for (let link of linkList) {
for (let redir of redirections) {
if (redir[0].test(link.href)) {
redir[1](link);
break;
}
}
}
/**
* Readd download buttons for modpacks
*/
// All Files list
Array.from(document.querySelectorAll("table.listing a.button"))
.filter(l => l.pathname.startsWith("/minecraft/modpacks") && l.href.endsWith("?client=y"))
.map(link => {
let newHref = link.href.slice(0, -9);
if (regexDownloadLink.test(newHref)) {
newHref = newHref + "/file";
}
let newLink = link.cloneNode(true);
newLink.href = newHref;
newLink.classList.add("button--icon-only");
newLink.classList.add("mr-2");
newLink.innerHTML = `<span class="button__text">
<svg class="icon icon-fixed-width icon-margin" viewBox="0 0 20 20" width="18" height="18">
<use xlink:href="/Content/${assetsPath}/Skins/CurseForge/images/twitch/Action/Download.svg#Action/Download"></use>
</svg>
</span>`;
if (link.parentNode != null) {
link.parentNode.insertBefore(newLink, link);
}
});
// Main File button, Page header button
Array.from(document.querySelectorAll("article a.button, header a.button"))
.filter((l) => l.pathname.startsWith("/minecraft/modpacks") && l.href.endsWith("?client=y"))
.map((link) => {
let newHref = link.href.slice(0, -9);
if (regexDownloadLink.test(newHref)) {
newHref = newHref + "/file";
}
if (link.parentNode.parentNode.childElementCount >= 2) {
// For some reason, direct file pages now have it, but not the main files page?
return;
}
let newButton = link.parentNode.cloneNode(true);
newButton.classList.remove("ml-2");
let newLink = newButton.querySelector("a.button");
newLink.classList.add("button--hollow");
newLink.href = newHref;
newLink.innerHTML = `<span class="button__text">
<svg class="icon icon-margin" viewBox="0 0 20 20" width="18" height="18">
<use xlink:href="/Content/${assetsPath}/Skins/CurseForge/images/twitch/Action/Download.svg#Action/Download"></use>
</svg> Download
</span>`;
link.parentNode.parentNode.insertBefore(newButton, link.parentNode);
});
// Minecraft version-specific files list
Array.from(document.querySelectorAll(".cf-recentfiles-credits-wrapper"))
.filter((w) => w.firstChild == null || (w.childNodes.length == 1 && w.firstChild.nodeType != Node.ELEMENT_NODE))
.forEach((wrapper) => {
let link = wrapper.parentNode.querySelector("a");
if (link != null) {
let newHref = link.href.replace("files", "download");
if (regexDownloadLink.test(newHref)) {
newHref = newHref + "/file";
}
wrapper.innerHTML = `<a href="${newHref}" class="button button--icon-only button--sidebar">
<span class="button__text">
<svg class="icon icon-fixed-width icon-margin" viewBox="0 0 20 20" width="16" height="16"><use xlink:href="/Content/${assetsPath}/Skins/CurseForge/images/twitch/Action/Download.svg#Action/Download"></use></svg>
</span>
</a>`;
}
});
// Sort file dependency lists
let sortNodeList = (list, sortBy) => {
let pairs = Array.from(list).map(el => [el, el.parentNode]);
pairs.sort((a, b) => sortBy(a[0], b[0]));
pairs.forEach(pair => pair[1].removeChild(pair[0]));
pairs.forEach(pair => pair[1].appendChild(pair[0]));
};
let relatedProjectsHeading = Array.from(document.querySelectorAll("section > h3")).find(heading => heading.innerText == "Related Projects");
if (relatedProjectsHeading != undefined) {
sortNodeList(relatedProjectsHeading.parentNode.querySelectorAll("section > div.flex > div"), (a, b) =>
a.querySelector("p.font-bold > a").innerText.localeCompare(b.querySelector("p.font-bold > a").innerText));
}
})();