Skip to content

Instantly share code, notes, and snippets.

@melchor629
Last active July 17, 2024 00:59
Show Gist options
  • Save melchor629/a58ca6b9ad2c8711b0cb7a6fdc094409 to your computer and use it in GitHub Desktop.
Save melchor629/a58ca6b9ad2c8711b0cb7a6fdc094409 to your computer and use it in GitHub Desktop.
old reddit improver
// ==UserScript==
// @name old reddit image opener
// @description Opens stuff when page loads
// @namespace melchor9000.me
// @version 1.1.0
// @downloadURL https://gist.github.com/melchor629/a58ca6b9ad2c8711b0cb7a6fdc094409/raw/a213129b41d2de839cca11fdc254c783f398c987/old-reddit-image-opener.js
// @match https://www.reddit.com/*
// ==/UserScript==
window.addEventListener('load', () => {
const buttons = [...document.querySelectorAll('.expando-button.collapsed.video')]
const intersectionObserver = new IntersectionObserver((entries) => {
entries
.filter((entry) => entry.isIntersecting)
.forEach((entry) => {
entry.target.click()
intersectionObserver.unobserve(entry.target)
})
}, { root: null, rootMargin: '-10px' })
buttons.forEach((button, i) => {
const container = button.parentElement.parentElement.parentElement
const kind = (container.attributes.getNamedItem('data-kind') || {}).value || 'unknown'
const nsfw = (container.attributes.getNamedItem('data-nsfw') || {}).value === 'true'
const elementId = ((container.attributes.getNamedItem('data-fullname') || {}).value || 'unknown').slice(3)
if (!nsfw) {
intersectionObserver.observe(button)
}
})
window.addEventListener('keyup', (e) => {
if (e.shiftKey) {
if (e.key === 'ArrowLeft') {
e.preventDefault()
document.querySelector('#siteTable > div.nav-buttons > span > span.prev-button a')?.click()
} else if (e.key === 'ArrowRight') {
e.preventDefault()
document.querySelector('#siteTable > div.nav-buttons > span > span.next-button a')?.click()
}
}
}, false)
})
/* ==UserStyle==
@name old reddit changes
@description This is your new file, start writing code
@namespace melchor9000.me
@version 1.0.0
@downloadURL https://gist.github.com/melchor629/a58ca6b9ad2c8711b0cb7a6fdc094409/raw/a213129b41d2de839cca11fdc254c783f398c987/old-reddit-ui-changes.css
@match https://www.reddit.com/*
==/UserStyle== */
.content[role="main"] > .sitetable.linklisting > .link {
margin-bottom: 1rem;
}
.content[role="main"] > .sitetable.linklisting > .link > .entry .title {
font-size: 1.125rem;
}
/* --- */
#header {
position: sticky;
top: 0;
}
#redesign-beta-optin-btn {
display: none;
}
#sr-header-area .srdrop {
transition: all 125ms ease-in-out;
user-select: none;
}
#sr-header-area .dropdown.srdrop {
margin-left: 0.25rem;
margin-right: 0.75rem;
}
#sr-header-area .dropdown.srdrop > span {
margin-right: 0;
}
#sr-header-area .dropdown.srdrop:hover {
background-color: rgb(10 10 10 / 0.15);
}
#sr-header-area .drop-choices.srdrop {
transform: translateY(4px);
border-radius: 4px;
box-shadow: 0 0 18px 1px rgb(10 10 10 / 0.15);
background-color: rgb(250 250 250 / 0.75);
-webkit-backdrop-filter: blur(3px) brightness(125%);
backdrop-filter: blur(3px) brightness(125%);
}
#sr-header-area .drop-choices.srdrop.inuse {
animation: menu-entrance 250ms ease-out;
}
#sr-header-area .drop-choices.srdrop > a {
transition: all 75ms ease-in-out;
}
/* --- */
body .listing-chooser.initialized {
position: fixed;
}
/* --- */
.tabmenu li a {
border-radius: 3px 3px 0 0;
transition: all 75ms ease-in-out;
}
.tabmenu li a:hover {
opacity: 0.75;
}
.tabmenu li.selected a:hover {
opacity: 0.95;
}
/* --- */
.premium-banner-outer {
display: none;
}
@keyframes menu-entrance {
from { opacity: 0.05; transform: translateY(16px); }
to { opacity: 1; transform: translateY(4px); }
}
// ==UserScript==
// @name Download media
// @namespace http://tampermonkey.net/
// @version 0.3
// @description try to take over the world!
// @author You
// @match https://twitter.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=twitter.com
// @grant unsafeWindow
// @sandbox MAIN_WORLD
// ==/UserScript==
(function() {
'use strict';
function downloadThing(url, name) {
const a = document.createElement('a')
a.href = url
a.download = name
a.click()
}
async function downloadPhoto(photoElement) {
if (!document.querySelector('[data-testid="swipe-to-dismiss"] img')) {
photoElement.click()
await new Promise((resolve) => setTimeout(resolve, 500))
}
const imageUrls = [...document.querySelectorAll('[data-testid="swipe-to-dismiss"] img')]
.map(img => img.src)
const [, user, id] = /\/([^/]+?)\/status\/(\d+)/.exec(location.pathname)
for (const imageUrl of imageUrls) {
const i = imageUrls.indexOf(imageUrl)
const fileName = imageUrls.length > 1 ? `@${user} ${id} (${i + 1}).jpg` : `@${user} ${id}.jpg`
const res = await unsafeWindow.fetch(imageUrl)
if (!res.ok) {
throw new Error('image request failed')
}
const blob = await res.blob()
downloadThing(
URL.createObjectURL(blob),
fileName,
)
}
}
async function downloadVideo(videoElement) {
debugger
const video = videoElement.querySelector('video')
const videoUrl = video.src
const [, user, id] = /\/([^/]+?)\/status\/(\d+)/.exec(location.pathname)
if (videoUrl.startsWith('blob:')) {
downloadThing(videoUrl, `@${user} ${id}.mp4`)
}
}
async function download() {
const tweet = [...document.querySelectorAll('[data-testid="tweet"][tabindex="-1"]')].at(-1)
const isVideo = tweet?.querySelector('[data-testid="videoPlayer"]')
const isPhoto = tweet?.querySelector('[data-testid="tweetPhoto"]')
debugger
try {
if (isPhoto && !isVideo) {
await downloadPhoto(isPhoto)
} else if (isVideo) {
await downloadVideo(isVideo)
}
} catch (e) {
console.log('Failed trying to download the thing', e)
}
}
const button = document.createElement('button')
Object.assign(button.style, {
position: 'fixed',
bottom: '1rem',
right: '2.5rem',
border: 'none',
borderRadius: '9px',
padding: '0.25rem 0.5rem',
backgroundColor: 'rgb(29, 155, 240)',
cursor: 'pointer',
})
button.innerText = 'di'
button.onclick = download
document.body.appendChild(button)
const hideShit = () => {
let clean = false
for(const node of document.querySelectorAll('[data-testid="sidebarColumn"] > div > div > div > div > div > div:not(:first-child)')) {
node.style.display = 'none'
clean = true
}
return clean
}
let oldHref = ''
const body = document.querySelector("body")
const observer = new MutationObserver(mutations => {
if (oldHref !== document.location.href) {
if(hideShit()) {
oldHref = document.location.href
requestAnimationFrame(hideShit)
}
}
})
observer.observe(body, { childList: true, subtree: true })
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment