Last active
April 26, 2021 21:28
-
-
Save Mego/50c9a6caa53658c6a305facdb67b409f to your computer and use it in GitHub Desktop.
Rooster Teeth Cast Names Userscript
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 Rooster Teeth Cast Names | |
// @version 1.0 | |
// @description Adds cast names to Rooster Teeth video descriptions | |
// @author Mego | |
// @match https://roosterteeth.com/watch/* | |
// @icon https://roosterteeth.com/img/rt-favicon.png | |
// @grant none | |
// @homepage https://gist.github.com/Mego/50c9a6caa53658c6a305facdb67b409f | |
// @downloadURL https://gist.githubusercontent.com/Mego/50c9a6caa53658c6a305facdb67b409f/raw/d68000d5ba7cd9951e386de0b45835118a6809e6/rtcastnames.user.js | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const apiServer = window.rtConfig.REACT_APP_API_SERVER; | |
const path = /https:\/\/roosterteeth\.com(\/watch\/.+)$/.exec(window.location)[1]; | |
const url = `${apiServer}/api/v1/${path}`; | |
let attempts = 0; | |
function formatNamesList(names) { | |
if(!names) { | |
return ""; | |
} | |
if(names.length === 1) { | |
return names[0]; | |
} | |
if(names.length === 2) { | |
return `${names[0]} and ${names[1]}`; | |
} | |
return `${names.slice(0,-1).join(", ")}, and ${names.slice(-1)[0]}`; | |
} | |
function appendCastNames(names) { | |
if(names) { | |
const descriptionEl = document.getElementsByClassName("video-details__description")[0]; | |
if(!descriptionEl) { | |
attempts += 1; | |
if(attempts < 5) { | |
setTimeout(() => appendCastNames(names), 1000); | |
} | |
} | |
descriptionEl.insertAdjacentText('afterend', names); | |
} | |
} | |
fetch(url, { | |
"headers": { | |
"accept": "application/json", | |
"client-type": "web", | |
"content-type": "application/json", | |
}, | |
"referrerPolicy": "no-referrer", | |
"method": "GET", | |
"mode": "cors", | |
"credentials": "omit" | |
}).then(resp => resp.json()).then(episodeData => { | |
try { | |
const castMemberNames = episodeData.data[0].included.cast_members.map(castMember => castMember.attributes.name); | |
if(/https:\/\/roosterteeth\.com\/watch\/rooster-teeth-podcast.+$/.test(window.location) && castMemberNames.includes('Gus Sorola')) { | |
castMemberNames.sort((a,b) => { | |
if(a === "Gus Sorola") { | |
return -1; | |
} | |
if(b === "Gus Sorola") { | |
return 1; | |
} | |
return 0; | |
}); | |
castMemberNames.push("Gus Sorola"); | |
} | |
setTimeout(() => appendCastNames(formatNamesList(castMemberNames)), 1000); | |
} catch(error) { | |
console.debug(error); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment