Repository navigation
Expand file tree
/
Copy pathReddit link swapper.user.js
More file actions
287 lines (249 loc) · 10.4 KB
/
Copy pathReddit link swapper.user.js
File metadata and controls
287 lines (249 loc) · 10.4 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// ==UserScript==
// @name Reddit link swapper
// @namespace https://github.com/Deses/userscripts
// @version 2.4
// @description Hijack share button to copy an embed link directly, strip params; add rapidsave download button
// @author Deses
// @match https://www.reddit.com/*
// @icon https://www.redditstatic.com/shreddit/assets/favicon/192x192.png
// @license BSD-3-Clause
// @run-at document-start
// @updateURL https://raw.githubusercontent.com/Deses/userscripts/refs/heads/main/Reddit%20link%20swapper.user.js
// @downloadURL https://raw.githubusercontent.com/Deses/userscripts/refs/heads/main/Reddit%20link%20swapper.user.js
// ==/UserScript==
(function () {
"use strict";
// -- Config --------------------------------------------------------------
const TARGET_DOMAIN = "fxreddit.seria.moe"; // e.g. "rxddit.com", "vxreddit.com", "fxreddit.seria.moe"
// -- Toast ---------------------------------------------------------------
let toastEl = null;
let toastTimer = null;
function injectStyles() {
const style = document.createElement("style");
style.textContent = `
#ls-toast {
position: fixed;
bottom: 28px;
left: 50%;
transform: translateX(-50%) translateY(12px);
background: #ff4500;
color: #fff;
font-family: -apple-system, "Segoe UI", Roboto, sans-serif;
font-size: 14px;
font-weight: 500;
line-height: 1;
padding: 10px 16px;
border-radius: 9999px;
box-shadow: 0 4px 16px rgba(0,0,0,0.25);
pointer-events: none;
opacity: 0;
transition: opacity 180ms ease, transform 180ms ease;
z-index: 999999;
white-space: nowrap;
}
#ls-toast.ls-visible {
opacity: 1;
transform: translateX(-50%) translateY(0);
}
/* Fallback styling, only used when Reddit's own share button markup
couldn't be found/cloned for a given post. */
.ls-download-btn-fallback {
display: inline-flex;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
margin-right: 4px;
border: none;
border-radius: 9999px;
background: transparent;
color: inherit;
cursor: pointer;
}
.ls-download-btn-fallback:hover {
background: rgba(128, 128, 128, 0.2);
}
`;
document.head.appendChild(style);
}
function showToast(message, durationMs = 2200) {
if (!toastEl) {
toastEl = document.createElement("div");
toastEl.id = "ls-toast";
document.body.appendChild(toastEl);
}
toastEl.textContent = message;
toastEl.classList.remove("ls-visible");
void toastEl.offsetWidth;
toastEl.classList.add("ls-visible");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toastEl.classList.remove("ls-visible"), durationMs);
}
// -- Helpers -------------------------------------------------------------
function buildEmbedUrl(href) {
try {
const url = new URL(href, "https://www.reddit.com");
url.hostname = TARGET_DOMAIN;
url.search = "";
url.hash = "";
return url.origin + url.pathname;
} catch {
return href;
}
}
function buildRapidsaveUrl(href) {
try {
const url = new URL(href, "https://www.reddit.com");
url.hostname = "www.reddit.com";
url.search = "";
url.hash = "";
return "https://rapidsave.com/info?url=" + encodeURIComponent(url.origin + url.pathname);
} catch {
return null;
}
}
function getPostUrl(composedPath) {
const shareEl = composedPath.find(el => el instanceof Element && el.matches("shreddit-post-share-button"));
if (shareEl) {
const href = shareEl.getAttribute("share-href");
if (href) return href;
}
const postEl = composedPath.find(el => el instanceof Element && el.matches("shreddit-post"));
if (postEl) {
const permalink = postEl.getAttribute("permalink");
if (permalink) return permalink;
}
return window.location.href;
}
function copyAndToast(url) {
navigator.clipboard
.writeText(url)
.then(() => showToast(`🔗 Copied as ${TARGET_DOMAIN}`))
.catch((err) => {
console.error("[LinkSwapper] Clipboard write failed:", err);
showToast("⚠️ Copy failed");
});
}
// -- Share button interception -------------------------------------------
// Capture phase so we fire before Reddit's own listener opens the menu
window.addEventListener(
"click",
(event) => {
const path = event.composedPath();
const btn = path.find(
el =>
el instanceof Element &&
el.tagName === "BUTTON" &&
(el.hasAttribute("aria-haspopup") || el.getAttribute("aria-label") === "Share")
);
const inShareHost = path.some(el => el instanceof Element && el.matches("shreddit-post-share-button"));
// console.log("[reddit-swapper] path tags:", path.map(el => el.tagName || el).join(", "));
// console.log("[reddit-swapper] btn:", btn, "inShareHost:", inShareHost);
if (!btn || !inShareHost) return;
const href = getPostUrl(path);
// console.log("[reddit-swapper] href:", href, "-> embed:", buildEmbedUrl(href));
if (!href) return;
event.preventDefault();
event.stopImmediatePropagation();
// console.log("[reddit-swapper] stopped propagation, copying");
copyAndToast(buildEmbedUrl(href));
},
true
);
// -- Fallback: clipboard.writeText interception (old share menu / other paths) --
const originalWriteText = navigator.clipboard.writeText.bind(navigator.clipboard);
navigator.clipboard.writeText = function (text) {
if (typeof text !== "string") {
return originalWriteText(text);
}
try {
const url = new URL(text);
if (url.hostname === "www.reddit.com") {
url.hostname = TARGET_DOMAIN;
url.search = "";
url.hash = "";
return originalWriteText(url.origin + url.pathname);
}
} catch {}
return originalWriteText(text);
};
// -- Download button (rapidsave, original url) ---------------------------
// Lucide "download" icon (MIT licensed), inlined so nothing is hotlinked.
const DOWNLOAD_ICON_SVG =
'<svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" ' +
'stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">' +
'<path d="M12 3v12"/><path d="m7 10 5 5 5-5"/><path d="M5 21h14"/></svg>';
function resolvePostHref(shareEl) {
const shareHref = shareEl.getAttribute("share-href");
if (shareHref) return shareHref;
const postEl = shareEl.closest("shreddit-post");
const permalink = postEl && postEl.getAttribute("permalink");
if (permalink) return permalink;
return null;
}
function findRealShareButton(shareEl) {
return (shareEl.shadowRoot && shareEl.shadowRoot.querySelector("button")) || shareEl.querySelector("button");
}
function makeDownloadButton(shareEl) {
const sourceBtn = findRealShareButton(shareEl);
let btn;
if (sourceBtn) {
// Clone Reddit's own share button so classes/layout match the native UI,
// then swap the icon and label for our download action.
btn = sourceBtn.cloneNode(true);
btn.removeAttribute("id");
const svg = btn.querySelector("svg");
if (svg) svg.outerHTML = DOWNLOAD_ICON_SVG;
btn.querySelectorAll("*").forEach((node) => {
node.childNodes.forEach((child) => {
if (child.nodeType === Node.TEXT_NODE && child.textContent.trim() === "Share") {
child.textContent = child.textContent.replace("Share", "Download");
}
});
});
} else {
btn = document.createElement("button");
btn.type = "button";
btn.className = "ls-download-btn-fallback";
btn.innerHTML = DOWNLOAD_ICON_SVG;
}
if (btn.hasAttribute("aria-label")) btn.setAttribute("aria-label", "Download");
btn.title = "Download original on rapidsave.com";
btn.addEventListener("click", (event) => {
event.preventDefault();
event.stopPropagation();
const href = resolvePostHref(shareEl);
const rapidsaveUrl = href && buildRapidsaveUrl(href);
if (!rapidsaveUrl) {
showToast("⚠️ Could not resolve url");
return;
}
window.open(rapidsaveUrl, "_blank", "noopener");
});
return btn;
}
function insertDownloadButtons(root = document) {
const shareEls = root.querySelectorAll("shreddit-post-share-button:not([data-ls-download])");
shareEls.forEach((shareEl) => {
shareEl.setAttribute("data-ls-download", "1");
shareEl.insertAdjacentElement("beforebegin", makeDownloadButton(shareEl));
});
}
function observeForShareButtons() {
insertDownloadButtons();
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.addedNodes.length) {
insertDownloadButtons(document);
break;
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
}
// -- Init ----------------------------------------------------------------
if (document.head) injectStyles();
else document.addEventListener("DOMContentLoaded", injectStyles);
if (document.body) observeForShareButtons();
else document.addEventListener("DOMContentLoaded", observeForShareButtons);
})();