VibratingVelociraptor / podawful-random-post-button

// ==UserScript==
// @name podawful-random-post-button
// @version 2.0.6
// @namespace http://tampermonkey.net/
// @description Add a 'Random Post' button to the Podawful website
// @author vv
// @license MIT
// @match https://podawful.com/*
// @match https://podawful.pizza/*
// @require https://cdn.jsdelivr.net/npm/@trim21/gm-fetch@0.1.13
// @grant GM.xmlHttpRequest
// ==/UserScript==

/******/ (() => { // webpackBootstrap
/******/ 	"use strict";
/******/ 	var __webpack_modules__ = ({

/***/ 607:
/***/ (function() {


var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
let maxPostId;
function getCurrentDomain() {
    const url = new URL(window.location.href);
    return url.origin;
}
function getMaxPostId() {
    return __awaiter(this, void 0, void 0, function* () {
        if (!maxPostId) {
            const result = yield fetch(`${getCurrentDomain()}/`);
            const data = yield result.text();
            const regex = /\/posts\/(\d+)/;
            const match = data.match(regex);
            if (match) {
                maxPostId = parseInt(match[1]);
            }
        }
        return maxPostId;
    });
}
function getValidPostId(maxPostId) {
    return __awaiter(this, void 0, void 0, function* () {
        let attempt = 0;
        while (attempt < 20) {
            const postId = Math.floor(Math.random() * (maxPostId || 2500));
            console.debug(`Checking post id ${postId}`);
            const result = yield fetch(`${getCurrentDomain()}/posts/${postId}`);
            if (result.status === 200) {
                console.debug(`Found valid post id ${postId}`);
                return postId;
            }
            attempt++;
        }
        return null;
    });
}
function navigateToPost(postId) {
    window.location.href = `${getCurrentDomain()}/posts/${postId}`;
}
function navigateToRandomPost() {
    return __awaiter(this, void 0, void 0, function* () {
        console.debug("Getting the latest max post");
        const maxPostId = yield getMaxPostId();
        console.debug(`Max post id is ${maxPostId}`);
        const validPostId = yield getValidPostId(maxPostId);
        if (validPostId) {
            navigateToPost(validPostId);
        }
        else {
            console.error("Could not find a valid post id");
        }
    });
}
function addMenuOption() {
    const li = document.createElement("li");
    li.className = "menu-item";
    const a = document.createElement("a");
    a.innerText = "Random Post";
    a.href = "#";
    a.addEventListener("click", (e) => {
        e.preventDefault();
        navigateToRandomPost();
    });
    li.appendChild(a);
    const ul = document.querySelector("#top-menu");
    if (!ul) {
        console.error("Could not find top-menu");
        return;
    }
    ul.appendChild(li);
}
function main() {
    return __awaiter(this, void 0, void 0, function* () {
        addMenuOption();
    });
}
main();


/***/ })

/******/ 	});
/************************************************************************/
/******/ 	
/******/ 	// startup
/******/ 	// Load entry module and return exports
/******/ 	// This entry module is referenced by other modules so it can't be inlined
/******/ 	var __webpack_exports__ = {};
/******/ 	__webpack_modules__[607]();
/******/ 	
/******/ })()
;