Skip to content

Instantly share code, notes, and snippets.

@Cyxo
Last active December 10, 2020 01:28
Show Gist options
  • Save Cyxo/49799cd796d7e3c97967804c72c89b21 to your computer and use it in GitHub Desktop.
Save Cyxo/49799cd796d7e3c97967804c72c89b21 to your computer and use it in GitHub Desktop.
A Userscript to add a random animal ASCII art to CodinGame's IDE
// ==UserScript==
// @name Codingame Header
// @namespace http://tampermonkey.net/
// @version 0.3
// @description Places a random animal ASCII art in the header of CodingGame's IDE
// @author Cyxo
// @include http*://*codingame.com/*
// @grant GM_xmlhttpRequest
// ==/UserScript==
//////////////////////////////////////////////////////////////////////////////////
// This is a userscript. You need the Tampermonkey browser extension to run it. //
// Then click on the [Raw] button on this Gist to install the script. //
// The first time you go to the IDE, it won't work. The second time, it will //
// for permissions. The third time it will work ;) //
//////////////////////////////////////////////////////////////////////////////////
let editor, text, lang;
(function() {
'use strict';
(function (old) {
console.log("overriden");
window.history.pushState = function () {
old.apply(window.history, arguments);
if (window.location.href.match(/https?:\/\/.*codingame.com\/ide\/.*/g))
fetchArt();
}
})(window.history.pushState);
if (window.location.href.match(/https?:\/\/.*codingame.com\/ide\/.*/g))
fetchArt();
})();
function tm(){
if (document.getElementsByClassName("code-editor").length === 0) setTimeout(tm, 1000);
else onEditor();
}
function onEditor(){
editor = monaco.editor.getModels()[0];
console.log("Heyyy", editor);
new MutationObserver(function (mutationsList){
for(let mutation of mutationsList){
let newLang = $('[ng-if="ngModel"]').text();
if (newLang != lang){
editor = monaco.editor.getModels()[0];
setTimeout(addText, 500);
}
}
}).observe($('[ng-if="ngModel"]')[0], {childList: true});
setTimeout(addText, 500);
}
function addText(){
lang = $('[ng-if="ngModel"]').text();
let comment = "// ";
let cmend = "";
if (["Bash","Perl","Python 3","Ruby"].indexOf(lang) > -1) comment = "# ";
else if (["Haskell","Lua"].indexOf(lang) > -1) comment = "-- ";
else if (lang == "Clojure") comment = "; ";
else if (lang == "OCaml") {
comment = "(* ";
cmend = " *)";
}
let base = editor.getValue();
if (base.match(/Random ASCII Header Plugin by Cyxo/)) return;
let art = text.split("\n");
for (let i = 0; i < art.length; i++){
if (art[i].length > 0)
art[i] = comment + art[i] + cmend;
}
art = art.join("\n") + "\n";
art += comment + "Random ASCII Header Plugin by Cyxo" + cmend + "\n";
art += comment + "To install: https://gist.github.com/Cyxo/49799cd796d7e3c97967804c72c89b21" + cmend + "\n\n";
editor.setValue(art + base);
console.log(editor.getValue());
}
function fetchArt(){
let animals = ["Aardvarks", "Bats", "Bears (Teddybears)", "Bears", "Beavers", "Birds (Land)", "Birds (Water)", "Bison", "Camels", "Cats", "Cows", "Deer", "Dogs", "Dolphins", "Elephants", "Fish", "Frogs", "Gorillas", "Horses"];
let url = "https://asciiart.website/index.php?art=animals/" + animals[Math.floor(Math.random()*animals.length)].toLowerCase();
GM_xmlhttpRequest({
url: url,
onload: function(res){
let html = $(res.response);
let art = html.find("pre pre")
let rnd = Math.floor(Math.random()*Math.floor(art.length/2+1));
text = html.find("#p"+rnd).text();
if (text.length == 0) fetchArt();
else tm();
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment