Last active
March 5, 2019 09:33
-
-
Save alyti/3b94a627f797baf03f0e25e6b2d221d3 to your computer and use it in GitHub Desktop.
A little helper userscript to make downloading NSPs from Switch Pirates a bit more tolerable
This file contains 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 Discord Switch Pirates Helper | |
// @namespace http://tampermonkey.net/ | |
// @version 2.1.1 | |
// @description Arr!! | |
// @author AltiCodes | |
// @match *://discordapp.com/channels/* | |
// @grant GM_setClipboard | |
// @inject-into auto | |
// @downloadURL https://gist.github.com/AltiCodes/3b94a627f797baf03f0e25e6b2d221d3/raw/DiscordSwitchPiratesHelper.user.js | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const is_switch_pirates_embed = (message) => { | |
const embedAuthor = message.getElementsByClassName('embedAuthor-3l5luH')[0] | |
if (!embedAuthor) | |
return false | |
return embedAuthor.textContent == 'Switch Pirates' | |
} | |
const magic_atob = (content) => { | |
try { | |
return window.atob(content) | |
} catch(e) { | |
return content | |
} | |
} | |
const update_messages = () => { | |
Array.from(document.querySelectorAll('.containerCozyBounded-1rKFAn .markup-2BOw-j')).forEach(message => { | |
if (!is_switch_pirates_embed(message)) | |
return | |
if (message.querySelector(".copyAllTheLinks") != null) | |
return | |
const p = message.getElementsByClassName('embedDescription-1Cuq9a')[0] | |
let allLinks = [] | |
const nodes = [...p.childNodes.entries()] | |
nodes.slice(Math.max(nodes.length - 3, 1)).map(a => a[1].remove()) | |
nodes.map(node => node[1]).filter(node => node.nodeName === "#text").map(text => { | |
const links = text.textContent.trim().split("\n").map(magic_atob).filter(a => a.startsWith("http")) | |
if (links.length == 0) | |
return | |
if (!text.parentNode) | |
return | |
const awoo = document.createElement("div") | |
awoo.style.cssText = "color:cyan;margin-bottom:1em;" | |
links.map((link, index) => { | |
let awau = document.createElement("a") | |
awau.href = link | |
awau.appendChild(document.createTextNode(`[Link #${index+1}] `)) | |
awoo.appendChild(awau) | |
}) | |
allLinks.push(...links) | |
text.parentNode.replaceChild(awoo, text) | |
}) | |
const saveall = document.createElement("button") | |
saveall.classList.add(..."button-38aScr lookFilled-1Gx00P colorBrand-3pXr91 sizeSmall-2cSMqn grow-q77ONN copyAllTheLinks".split(' ')) | |
saveall.style.marginTop = "10px" | |
const saveallText = document.createElement("div") | |
saveallText.classList.add("contents-18-Yxp") | |
saveallText.appendChild(document.createTextNode("Copy all")) | |
saveall.appendChild(saveallText) | |
saveall.onclick = (e) => { | |
GM_setClipboard(allLinks.join(' ')) | |
const copied = document.createElement("div") | |
copied.style.cssText = 'color:cyan' | |
copied.appendChild(document.createTextNode(`Arr! ${allLinks.length} link${allLinks.length != 1 ? 's' : ''} copied.`)) | |
p.parentNode.insertBefore(copied, p.nextSibling) | |
} | |
message.appendChild(saveall) | |
}) | |
} | |
const checkChannel = (changes) => { | |
for(let change of changes){ | |
if(change.addedNodes.length){ | |
setTimeout(update_messages, 1000) | |
} | |
} | |
} | |
const channel = new MutationObserver(checkChannel) | |
window.onload = () => { | |
setTimeout(function(){ | |
let elem = document.getElementsByClassName('content-yTz4x3')[0].firstChild | |
channel.observe(elem, {childList: true}) | |
update_messages() | |
console.log('%c[Discord Switch Pirates Helper]'+'%c has loaded', 'background:black; color:cyan', 'color:black') | |
}, 2000) | |
} | |
})() |
For some reason Tampermonkey in Chrome wouldn't run this for me until I enclosed the if blocks in { brackets }. After that, it works great. @Raicuparta 's edit works great too!
On Firefox I also needed to add brackets for every if statement.
It also does not work on new messages, until I refresh the page manually.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works great! Personally I'd change line 53 to
awau.appendChild(document.createTextNode(`${link}\n`))
so the links are easier to copy & paste as plain text.