-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtampermonkey.user.js
More file actions
74 lines (64 loc) · 2.39 KB
/
tampermonkey.user.js
File metadata and controls
74 lines (64 loc) · 2.39 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
// ==UserScript==
// @name Reddit saver
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Quick download button for reddit posts
// @author Feridinha
// @icon https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @updateURL https://raw.githubusercontent.com/Feridinha/reddit-save/main/tampermonkey.user.js
// @domain raw.githubusercontent.com
// @grant none
// @match https://*/*
// ==/UserScript==
const apiUrl = "https://reddit-save.feridinha.com?url="
const handleClick = (redditUrl) => async () => {
const response = await fetch(apiUrl + redditUrl, { method: "POST" })
const json = await response.json()
if (!json.success) return alert(json.message)
window.open(json.data, "_blank")
}
const getDownloadButton = (link) => {
const wrapper = document.createElement("div")
const button = document.createElement("button")
const { href } = link
button.style.background = "#2222"
button.style.margin = "0 .1rem"
button.style.padding = ".5rem"
button.style.height = "100%"
button.innerText = "Download"
button.onclick = handleClick(href)
button.onmouseenter = (e) => {
e.target.style.background = "#5555"
}
button.onmouseleave = (e) => {
e.target.style.background = "#2222"
}
wrapper.appendChild(button)
return wrapper
}
const handleArea = (area, link) => {
if (area.style.background == "none") return
area.style.background = "none"
const button = getDownloadButton(link)
area.insertBefore(button, area.lastChild)
}
const postsSelector = `#AppRouter-main-content > div > div > div:last-child > div:last-child > div:first-child > div:nth-child(5) > div > div > div > div:last-child > div:last-child`
const handleButtonAreas = () => {
const postsLinks = document.querySelectorAll(
`${postsSelector} > div:last-child > a:first-child`
)
const buttonsAreas = document.querySelectorAll(
`${postsSelector} > div:last-child`
)
if (!postsLinks?.length) return
buttonsAreas.forEach((post, index) => {
const link = postsLinks[index]
if (!link) return
handleArea(post, link)
})
}
;(function () {
"use strict"
const matches = /\b(?:www\.)?reddit\.com/.test(location)
setInterval(handleButtonAreas, 500)
})()