redactle

// ==UserScript==
// @name Custom Redactles
// @version 0.2
// @description Custom and Random Redactles
// @author Kkevsterrr, smrq
// @match https://www.redactle.com/
// @icon https://www.google.com/s2/favicons?sz=64&domain=redactle.com
// @grant none
// ==/UserScript==
/* globals jQuery, $, waitForKeyElements, smallList */

(function() {
‘use strict’;
/* global baffled:writable, guessedWords:writable, guessCounter:writable, hitCounter:writable, currentAccuracy:writable, pageRevealed:writable, ansStr, RevealPage, fetchData */

async function LoadCustom(game) {
if (!game || ”) return;

var winText = document.getElementById(“winText”);
var userGuess = document.getElementById(‘userGuess’);
var guessLogBody = document.getElementById(‘guessLogBody’);

window.SaveProgress = function SaveProgress() {}
window.WinRound = function WinRound(){
document.getElementById(“userGuess”).disabled = true;
if(!pageRevealed){
RevealPage();
}
winText.innerHTML = `

Congratulations, you solved this custom Redactle!

  • The answer was: ${ansStr}
  • You solved it in ${guessedWords.length} guesses
  • Your accuracy was ${currentAccuracy}%

`;
winText.style.display = ‘block’;
winText.style.height = ‘auto’;
}

baffled = [];
guessedWords = [];
guessCounter = 0;
hitCounter = 0;
currentAccuracy = -1;
pageRevealed = false;

userGuess.disabled = false;
winText.innerHTML = ”;
// Keep hiding win text when vic.php is slow and returns after loading a custom game
winText.style.height = 0;
winText.style.overflow = ‘hidden’;
guessLogBody.innerHTML = ”;

await fetchData(false, game);
}
async function generateGame() {
var arts = “https://en.wikipedia.org/w/api.php?action=parse&format=json&page=Wikipedia:Vital%20articles&prop=text&formatversion=2&origin=*”;
//console.log(“Generating new Redactle…”)
await fetch(arts).then(resp => {
if (!resp.ok) {
throw `Server error: [${resp.status}] [${resp.statusText}] [${resp.url}]`;
}
return resp.json();
}).then(a => {
//console.log(“Pulled Wikipedia top 1,000…”)
var options = [];
var el = document.createElement( ‘html’ );
el.innerHTML = a.parse.text
$(‘a’, el).each(function( idx) {
var data = $(this).attr(“href”);
if (data != undefined && data.indexOf(“wiki”) != -1 && data.indexOf(“.svg”) == -1 && data.indexOf(“https://”) == -1 && data.indexOf(“:”) == -1) {
options.push(data.replace(“/wiki/”, “”));
}
});
const randomElement = options[Math.floor(Math.random() * options.length)];
//console.log(randomElement)
var article = btoa(randomElement);
LoadCustom(article);
});
}
window.genGame = function genGame() {
generateGame();
}
window.LoadCustomPopup = function LoadCustomPopup() {
var game = prompt(‘Enter a game code. Leave it blank for a random Redactle-approved article, or enter a base64 article slug for a specific article.’);
if (!game) {
game = smallList[Math.floor(Math.random() * smallList.length)];
//console.log(game);
}
LoadCustom(game);
}
document.querySelector(‘ul.navbar-nav’).innerHTML += ‘

‘;
})();