Skip to content

Instantly share code, notes, and snippets.

@towerofnix
Last active August 2, 2020 23:15
Show Gist options
  • Save towerofnix/ca54c2ba584f5b7cec96 to your computer and use it in GitHub Desktop.
Save towerofnix/ca54c2ba584f5b7cec96 to your computer and use it in GitHub Desktop.
Wikia trivia on anything! Just go to your favourite wiki and run this JavaScript code to get free trivia!
var what = "/Special:Random";
$.get(what).done(function(res) {
try {
res = (new DOMParser()).parseFromString(res, "text/html");
window.res = res;
what = res.querySelector("#WikiaPageHeader .header-title").textContent.trim();
var trivHeader = res.querySelector("*[id*=rivia]");
var article = trivHeader.parentNode.parentNode;
var index = Array.from(article.childNodes).indexOf(trivHeader.parentNode);
var trivUl = article.childNodes[index + 2];
var arr = ["-- Trivia on " + what + ": --"];
for (var i of trivUl.children) {
arr.push(i.innerText.trim());
}
alert(arr.join("\n\n"));
} catch(e) {
alert("Oops! I couldn't get you any trivia on the page " + what + ". Sorry!\n\n" + e);
}
}).error(function(e) {
alert("Oops! I got a network error.");
});
// Defaults are designed for the Wings of Fire wiki.
// http://wingsoffire.wikia.com
var what = "/wiki/" + prompt("What do you want trivia about?", ["Tsunami","Umber","Moonwatcher"][Math.floor(Math.random() * 3)]);
$.get(what).done(function(res) {
try {
res = (new DOMParser()).parseFromString(res, "text/html");
window.res = res;
what = res.querySelector("#WikiaPageHeader .header-title").textContent.trim();
var trivHeader = res.querySelector("*[id*=rivia]");
var article = trivHeader.parentNode.parentNode;
var index = Array.from(article.childNodes).indexOf(trivHeader.parentNode);
var trivUl = article.childNodes[index + 2];
var arr = ["-- Trivia on " + what + ": --"];
for (var i of trivUl.children) {
arr.push(i.innerText.trim());
}
alert(arr.join("\n\n"));
} catch(e) {
alert("Oops! I couldn't get you any trivia on the page " + what + ". Sorry!\n\n" + e);
}
}).error(function(e) {
alert("Oops! I got a network error.");
});
// old code from nov 14, 2015 7:26 am ast!
// You only need to run this program once! Just run it, then, if
// you want to see more trivia, click on "OK". If you don't want
// to see any more trivia, click on "Cancel". :)
(function f() {
var what = "/Special:Random";
$.get(what).done(function(res) {
try {
res = (new DOMParser()).parseFromString(res, "text/html");
window.res = res;
what = res.querySelector("#PageHeader .page-header__title").textContent.trim();
var trivHeader = res.querySelector("*[id*=rivia]");
if (!trivHeader) return f();
var article = trivHeader.parentNode.parentNode;
var index = Array.from(article.childNodes).indexOf(trivHeader.parentNode);
var trivUl = Array.from(article.childNodes).slice(index).find(el => el.tagName === 'UL');
var arr = ["-- Trivia on " + what + ": --"];
/*
for (var i of trivUl.children) {
arr.push(i.innerText.trim());
}
*/
arr.push(trivUl.children[Math.floor(Math.random() * trivUl.children.length)].innerText.trim().replace(/\s{2,}/g, ' '));
if (confirm(arr.join("\n"))) f();
} catch(e) {
if (confirm("Oops! I couldn't get you any trivia on the page " + what + ". Sorry!\n\n" + e)) f();
}
}).error(function(e) {
alert("Oops! I got a network error.");
});
}());
// You only need to run this program once! Just run it, then, if
// you want to see more trivia, click on "OK". If you don't want
// to see any more trivia, click on "Cancel". :)
(function f() {
var what = "/Special:Random";
$.get(what).done(function(res) {
try {
res = (new DOMParser()).parseFromString(res, "text/html");
window.res = res;
what = res.querySelector("#WikiaPageHeader .header-title").textContent.trim();
var trivHeader = res.querySelector("*[id*=rivia]");
var article = trivHeader.parentNode.parentNode;
var index = Array.from(article.childNodes).indexOf(trivHeader.parentNode);
var trivUl = article.childNodes[index + 2];
var arr = ["-- Trivia on " + what + ": --"];
for (var i of trivUl.children) {
arr.push(i.innerText.trim());
}
if (confirm(arr.join("\n\n"))) f();
} catch(e) {
if (confirm("Oops! I couldn't get you any trivia on the page " + what + ". Sorry!\n\n" + e)) f();
}
}).error(function(e) {
alert("Oops! I got a network error.");
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment