Created
June 11, 2020 22:57
-
-
Save AviDuda/bc448c79bf7f0b2e91607a79ac1ebbca to your computer and use it in GitHub Desktop.
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 itch.io play a random bundle game | |
// @namespace https://twitter.com/tomasduda | |
// @version 1.0 | |
// @description Shows which game to play next from an itch.io bundle download page. | |
// @author Tomáš Duda | |
// @match https://itch.io/bundle/download/* | |
// @grant GM.setValue | |
// @grant GM.getValue | |
// @grant GM.deleteValue | |
// ==/UserScript== | |
(async () => { | |
'use strict'; | |
const VAL_REDIRECTED = 'redirected'; | |
const filterOptions = document.querySelector('.filter_options'); | |
const pager = filterOptions.querySelector('.pager'); | |
if (await GM.getValue(VAL_REDIRECTED) === true) { | |
selectRandomItem(); | |
} | |
addRandomButton(); | |
function addRandomButton() { | |
// Add a button next to the search box | |
const randomButton = document.createElement('a'); | |
randomButton.classList.add('button'); | |
randomButton.innerText = 'Random'; | |
filterOptions.appendChild(randomButton); | |
randomButton.addEventListener('click', randomButtonClick); | |
} | |
async function randomButtonClick() { | |
if (await GM.getValue(VAL_REDIRECTED) !== true) { | |
if (pager) { | |
goToRandomPage(); | |
return; | |
} | |
} | |
selectRandomItem(); | |
} | |
function goToRandomPage() { | |
const numPages = Number(pager.querySelector('.pager_label a').innerText); | |
const randPage = Math.ceil(Math.random() * numPages); | |
GM.setValue(VAL_REDIRECTED, true); | |
window.location.search = "?page=" + randPage; | |
} | |
function selectRandomItem() { | |
GM.deleteValue(VAL_REDIRECTED); | |
const gameList = document.querySelector('.game_list'); | |
// Reset styles just in case | |
gameList.querySelectorAll('.game_row').forEach(e => { e.style = '' }); | |
const randomGame = gameList.children[Math.floor(Math.random() * gameList.childElementCount)]; | |
randomGame.style = 'border: 5px solid var(--itchio_button_color, #FF2449);'; | |
randomGame.scrollIntoView({behavior: 'smooth', block: 'end', inline: 'nearest'}); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment