Load and Save maps from haxmaps just using an ID.
La data de
// ==UserScript==
// @name HaxBall Map Picker
// @namespace https://greasyfork.org/en/users/165127-hydrosaur
// @version 2.1
// @icon https://www.haxball.com/favicon.ico
// @description Load and Save maps from haxmaps just using an ID.
// @author -Electron-
// @include https://www.haxball.com/FFuuq69i/__cache_static__/g/game.html
// @supportURL https://www.reddit.com/message/compose/?to=-Electron-
// @website https://redd.it/no-post-yet
// @require https://code.jquery.com/jquery-latest.min.js
// @grant none
// ==/UserScript==
(function() {
'use strict';
let pickerPopupIsShown = false;
let gamePageIsShown = false;
let haxMap = 0;
let favoritesListShown = false;
if(!localStorage.getItem("saved_maps")){
localStorage.setItem("saved_maps", "{}");
}
setInterval(function(){
if($(".dialog.pick-stadium-view").length){
if(!pickerPopupIsShown) showPicker();
pickerPopupIsShown = true;
} else {
if(pickerPopupIsShown) hidePicker();
pickerPopupIsShown = false;
}
if($("button[data-hook='leave-btn']").length){
if(!gamePageIsShown) gamePageShown();
gamePageIsShown = true;
} else {
if(gamePageIsShown) gamePageHidden();
gamePageIsShown = false;
}
}, 250);
function showPicker(){
$(`<button data-hook="favorites">Favorites</button>`).insertAfter(".dialog.pick-stadium-view .buttons .file-btn");
$(`<button data-hook="searchmaps">HaxMaps</button>`).insertAfter(".dialog.pick-stadium-view .buttons .file-btn");
$("button[data-hook='searchmaps']").click(function(){
let mapSearch = prompt("Map ID:");
if(!isNaN(Number(mapSearch)) && mapSearch){
changeStadium(mapSearch);
} else {
alert("Invalid Map ID")
}
});
$("button[data-hook='favorites']").click(function(){
favoritesListShown = true;
$(".dialog.pick-stadium-view .list.ps").empty();
let savedMaps = JSON.parse(localStorage.getItem("saved_maps"));
Object.keys(savedMaps).forEach((item, idx) => {
$(".dialog.pick-stadium-view .list.ps").append(`<div class="elem" data-id="${savedMaps[item]}">${item}</div>`);
});
$(".dialog.pick-stadium-view .list.ps .elem").click(function(){
$(".selected").removeClass("selected");
$(this).addClass("selected");
$("button[data-hook='pick']").prop("disabled", false);
});
$("button[data-hook='pick']").click(function(){
console.log("cliq");
if($(".dialog.pick-stadium-view .list.ps .selected").length){
if(!isNaN(Number($(".dialog.pick-stadium-view .list.ps .selected").attr("data-id")))){
changeStadium($(".dialog.pick-stadium-view .list.ps .selected").attr("data-id"));
}
}
});
});
}
function hidePicker(){
favoritesListShown = false;
}
function gamePageShown(){
if(!$("button[data-hook='addfavorites']").length){
$(".settings").append("<button class='admin-only' data-hook='addfavorites' style='width: 100%'>Add Map To Favorites</button>");
$("button[data-hook='addfavorites']").click(function(){
if(haxMap){
let saved_maps = JSON.parse(localStorage.getItem("saved_maps"));
let savedMapIDs = Object.values(saved_maps);
if(savedMapIDs.includes(Number(haxMap))){
alert("You have already saved this map.");
} else {
saved_maps[$("label[data-hook='stadium-name']").text()] = Number(haxMap);
localStorage.setItem("saved_maps", JSON.stringify(saved_maps));
alert("Saved " + $("label[data-hook='stadium-name']").text());
}
} else {
alert("You can only favorite maps that are uploaded to haxmaps.com");
}
});
}
}
function gamePageHidden(){
}
$("button[data-hook='pick']").click(function(){
haxMap = 0;
})
function searchForMaps(query){
fetch("https://streamlyne.stream:88/proxy?link=http://haxmaps.com/hb/rpc").then(a => a.text()).then(text => {
let listElems = $(text);
console.log(Array.from(listElems))
});
}
function linkToBlob(link){
return new Promise(function(resolve, reject) {
let xhr = new XMLHttpRequest();
xhr.open("GET", link);
xhr.responseType = "blob";//force the HTTP response, response-type header to be blob
xhr.onload = function(){
resolve(xhr.response);
}
xhr.send();
});
}
function getHaxBallMapText(id){
return new Promise(function(resolve, reject) {
fetch("https://streamlyne.stream:88/proxy?link=http://haxmaps.com/dl/" + id).then(a => a.text()).then(function(text){
resolve(text);
});
});
}
function changeStadium(id){
getHaxBallMapText(id).then(text => {
if(text.length > 2){
changeStadiumFromText(text);
haxMap = Number(id);
} else {
alert("Map ID out of range");
}
});
}
function changeStadiumFromText(text){
const dT = new ClipboardEvent('').clipboardData || // Firefox < 62 workaround exploiting https://bugzilla.mozilla.org/show_bug.cgi?id=1422655
new DataTransfer(); // specs compliant (as of March 2018 only Chrome)
dT.items.add(new File([text], 'programmatically_created.hbs'));
console.log(text);
document.getElementById("stadfile").files = dT.files;
}
// Your code here...
})();