Instantly share code, notes, and snippets.
Created
October 19, 2026 05:11
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
-
Save Excedrin/2ea7d0122a7ed303d9536bb5bd84b34e to your computer and use it in GitHub Desktop.
slophits share links
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==UserScript== | |
| // @name SlopHits duel share links | |
| // @namespace local.slophits | |
| // @version 1.3.0 | |
| // @description Adds copy and open buttons for each clip in SlopHits duels. | |
| // @match https://slophits.com/* | |
| // @run-at document-start | |
| // @grant none | |
| // ==/UserScript== | |
| (function () { | |
| 'use strict'; | |
| const clipsById = new Map(); | |
| let renderQueued = false; | |
| function rememberPair(pair) { | |
| for (const clip of [pair?.a, pair?.b]) { | |
| if (clip?.id && clip?.url) clipsById.set(String(clip.id), clip); | |
| } | |
| } | |
| // Capture duel data so each card points to its original X post. | |
| const pageFetch = window.fetch; | |
| window.fetch = async function (...args) { | |
| const response = await pageFetch.apply(this, args); | |
| try { | |
| const requestUrl = args[0] instanceof URL ? args[0].href : typeof args[0] === 'string' ? args[0] : args[0]?.url; | |
| const url = requestUrl ? new URL(requestUrl, location.href) : null; | |
| if (url?.origin === location.origin && ['/api/duel', '/api/vote'].includes(url.pathname) && | |
| response.ok) { | |
| const data = await response.clone().json(); | |
| rememberPair(data); | |
| rememberPair(data?.next); | |
| queueRender(); | |
| } | |
| } catch (_) { /* Ignore unexpected responses without affecting the site. */ } | |
| return response; | |
| }; | |
| function queueRender() { | |
| if (renderQueued) return; | |
| renderQueued = true; | |
| requestAnimationFrame(() => { renderQueued = false; addButtons(); }); | |
| } | |
| function clipFor(card) { | |
| const clips = [...clipsById.values()]; | |
| // Prefer identifiers in the rendered card. A/B position alone is unsafe: | |
| // the API response can arrive before Vue has replaced the previous pair. | |
| const recId = card.querySelector('[data-rec-slot]')?.getAttribute('data-rec-slot'); | |
| if (recId) { | |
| const match = clips.find(clip => String(clip.id) === recId); | |
| if (match) return match; | |
| } | |
| // src is the requested thumbnail; currentSrc can still refer to the last | |
| // loaded image during a transition. Never guess from A/B or song titles. | |
| const image = card.querySelector('img.fg, img.bg'); | |
| const thumb = image?.getAttribute('src'); | |
| if (thumb) { | |
| const matches = clips.filter(clip => clip.thumb === thumb); | |
| return matches.length === 1 ? matches[0] : null; | |
| } | |
| const videoUrl = card.querySelector('video')?.getAttribute('src'); | |
| if (videoUrl) { | |
| const matches = clips.filter(clip => [clip.sd, clip.hd].includes(videoUrl)); | |
| return matches.length === 1 ? matches[0] : null; | |
| } | |
| return null; | |
| } | |
| function clipPageUrl(clip) { | |
| return new URL(`/clip/${encodeURIComponent(clip.id)}`, location.origin).href; | |
| } | |
| function addButtons() { | |
| document.querySelectorAll('#duel .scrw').forEach(card => { | |
| const clip = clipFor(card); | |
| const existing = card.querySelector('.sh-share-actions'); | |
| if (existing) { | |
| if (!clip?.url || !clip.id) { | |
| existing.hidden = true; | |
| return; | |
| } | |
| const xLink = existing.querySelector('.sh-x-link'); | |
| const clipLink = existing.querySelector('.sh-clip-link'); | |
| if (xLink) xLink.href = clip.url; | |
| if (clipLink) clipLink.href = clipPageUrl(clip); | |
| existing.dataset.clipUrl = clip.url; | |
| existing.dataset.clipId = String(clip.id); | |
| existing.hidden = false; | |
| return; | |
| } | |
| if (!clip?.url || !clip.id) return; | |
| const actions = document.createElement('div'); | |
| actions.className = 'sh-share-actions'; | |
| actions.dataset.clipUrl = clip.url; | |
| actions.dataset.clipId = String(clip.id); | |
| // Refresh before the browser follows or copies an anchor's destination. | |
| for (const eventName of ['pointerdown', 'click', 'auxclick', 'contextmenu']) { | |
| actions.addEventListener(eventName, event => { | |
| addButtons(); | |
| if (!clipFor(card)) event.preventDefault(); | |
| }, true); | |
| } | |
| actions.addEventListener('click', event => event.stopPropagation()); | |
| const copy = document.createElement('button'); | |
| copy.type = 'button'; | |
| copy.textContent = 'Copy link'; | |
| copy.title = 'Copy the original X post link'; | |
| copy.addEventListener('click', async event => { | |
| event.stopPropagation(); | |
| const liveUrl = clipFor(card)?.url; | |
| if (!liveUrl) { queueRender(); return; } | |
| try { | |
| await navigator.clipboard.writeText(liveUrl); | |
| copy.textContent = 'Copied!'; | |
| setTimeout(() => { copy.textContent = 'Copy link'; }, 1400); | |
| } catch (_) { window.prompt('Copy this link:', liveUrl); } | |
| }); | |
| const open = document.createElement('a'); | |
| open.className = 'sh-x-link'; | |
| open.textContent = 'Open X ↗'; | |
| open.href = clip.url; | |
| open.target = '_blank'; | |
| open.rel = 'noopener noreferrer'; | |
| open.title = 'Open the original X post in a new tab'; | |
| open.addEventListener('click', event => event.stopPropagation()); | |
| const clipPage = document.createElement('a'); | |
| clipPage.className = 'sh-clip-link'; | |
| clipPage.textContent = 'SlopHits page ↗'; | |
| clipPage.href = clipPageUrl(clip); | |
| clipPage.target = '_blank'; | |
| clipPage.rel = 'noopener noreferrer'; | |
| clipPage.title = 'Open this clip on SlopHits in a new tab'; | |
| clipPage.addEventListener('click', event => event.stopPropagation()); | |
| actions.append(copy, open, clipPage); | |
| card.append(actions); | |
| }); | |
| } | |
| const style = document.createElement('style'); | |
| style.textContent = ` | |
| #duel .scrw { position: relative; } | |
| #duel .sh-share-actions { position:absolute; z-index:8; top:10px; right:10px; max-width:calc(100% - 20px); display:flex; flex-wrap:wrap; justify-content:flex-end; gap:6px; align-items:center; | |
| } | |
| #duel .sh-share-actions[hidden] { display:none; } | |
| #duel .sh-share-actions button, #duel .sh-share-actions a { border:2px solid #101010; padding:7px 9px; background:#ffe600; color:#101010; font-family:inherit; font-size:13px; font-weight:700; | |
| line-height:1.1; text-decoration:none; cursor:pointer; box-shadow:2px 2px 0 #101010; } | |
| #duel .sh-share-actions button:hover, #duel .sh-share-actions a:hover { background:#fff; } | |
| @media (max-width:520px) { #duel .sh-share-actions { top:6px; right:6px; gap:4px; } #duel .sh-share-actions button, #duel .sh-share-actions a { padding:6px; font-size:11px; } } | |
| `; | |
| (document.head || document.documentElement).append(style); | |
| new MutationObserver(queueRender).observe(document, { | |
| childList:true, subtree:true, attributes:true, | |
| attributeFilter:['src', 'data-rec-slot'] | |
| }); | |
| // Repair removed controls even if a DOM update was missed. This only reads | |
| // the page; it never requests another pair or submits a vote. | |
| setInterval(queueRender, 1000); | |
| window.addEventListener('pageshow', queueRender); | |
| queueRender(); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment