Last active
May 29, 2017 15:03
-
-
Save lukashavrlant/2ef8211c95d9f741f98255e5799d56af to your computer and use it in GitHub Desktop.
Travian Obchodní Cesty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript: (function () { | |
var confirmationInterval = null; | |
var autoSubmitEnabledMsg = "Po kliknutí automaticky potvrdit formulář s obchodní cestou."; | |
var autoSubmitDisabledMsg = "Po kliknutí nepotvrzovat formulář s obchodní cestou, nutné ruční odeslání."; | |
var helperId = "tbc-trade-route-helper"; | |
var ids = [ | |
"tbc-lumber", "tbc-clay", "tbc-iron", "tbc-crop", "tbc-villages", "tbc-number-of-trips", "tbc-village-id" | |
]; | |
var helper = document.getElementById(helperId); | |
var body = document.getElementsByTagName("body")[0]; | |
var idsMap = { | |
"tbc-lumber": "r1", | |
"tbc-clay": "r2", | |
"tbc-iron": "r3", | |
"tbc-crop": "r4", | |
"tbc-villages": "did_dest", | |
"tbc-number-of-trips": "repeat", | |
"tbc-village-id": "did_dest" | |
}; | |
if (shouldFillTradeRoute()) { | |
fillTradeRoute(); | |
} else { | |
if (helper) { | |
hideHelper(); | |
} else { | |
showSettingsBox(); | |
} | |
} | |
return; | |
function hideHelper() { | |
helper = helper || document.getElementById(helperId); | |
body.removeChild(helper); | |
} | |
function fillTradeRoute() { | |
ids.forEach(function(id) { | |
var value = localStorage.getItem(id); | |
if (value) { | |
document.getElementById(idsMap[id]).value = value; | |
} | |
}); | |
if (shouldAutosubmit()) { | |
document.getElementById("tradeRouteEdit").submit(); | |
} | |
} | |
function shouldFillTradeRoute() { | |
return !!document.getElementById("tradeRouteEdit"); | |
} | |
function showSettingsBox() { | |
var form = document.createElement("div"); | |
var villageNames = getVillageNames(); | |
var villigesSelectBox = getVillagesSelectBox(villageNames); | |
form.innerHTML = ` | |
<a style="font-size: 250%; position: absolute; right: 20px; top: 20px; color: #00bc00" href="#" id="tbc-hide-helper">×</a> | |
<form> | |
<table style="border-collapse: collapse"> | |
<tr> | |
<td>Cílová stanice:</td> | |
<td> | |
${villigesSelectBox} nebo ID vesnice: | |
<input id="tbc-village-id" style="width: 100px;"> | |
</td> | |
</tr> | |
<tr> | |
<td>Suroviny:</td> | |
<td> | |
<img class="r1" src="img/x.gif"><input id="tbc-lumber" style="width: 100px;"> | |
<img class="r2" src="img/x.gif"><input id="tbc-clay" style="width: 100px;"> | |
<img class="r3" src="img/x.gif"><input id="tbc-iron" style="width: 100px;"> | |
<img class="r4" src="img/x.gif"><input id="tbc-crop" style="width: 100px;"> | |
</td> | |
</tr> | |
<tr> | |
<td>Dodávky:</td> | |
<td><select id="tbc-number-of-trips"><option value="1">1</option><option value="2">2</option><option value="3">3</option></select></td> | |
</tr> | |
<tr> | |
<td>Hned potvrdit?</td> | |
<td> | |
<input type="checkbox" id="tbc-autosubmit"> | |
<span id="tbc-autosubmit-msg"></span> | |
</td> | |
</tr> | |
</table> | |
<input type="button" value="Uložit" id="tbc-save-settings"> | |
<span id="tbc-confirmation-msg"></span> | |
</form> | |
`; | |
form.style["background-color"] = "white"; | |
form.style["border-radius"] = "20px"; | |
form.style.border = "2px solid #00bc00"; | |
form.style.padding = "20px"; | |
form.style.position = "fixed"; | |
form.style.top = "50px"; | |
form.style.left = "50px"; | |
form.style["z-index"] = "1111"; | |
form.id = helperId; | |
body.appendChild(form); | |
restoreSettings(); | |
showAutosubmitHelpMessage(); | |
document.getElementById("tbc-save-settings").onclick = saveSettings; | |
document.getElementById("tbc-autosubmit").onclick = showAutosubmitHelpMessage; | |
document.getElementById("tbc-hide-helper").onclick = hideHelper; | |
} | |
function showAutosubmitHelpMessage() { | |
var msgEl = document.getElementById("tbc-autosubmit-msg"); | |
if (document.getElementById("tbc-autosubmit").checked) { | |
msgEl.innerText = autoSubmitEnabledMsg; | |
} else { | |
msgEl.innerText = autoSubmitDisabledMsg; | |
} | |
} | |
function getVillagesSelectBox(villagesMap) { | |
var options = []; | |
for (var vil in villagesMap) { | |
if (villagesMap.hasOwnProperty(vil)) { | |
options.push(`<option value="${villagesMap[vil]}">${vil}</option>`) | |
} | |
} | |
return `<select id="tbc-villages">${options.join("")}</select>`; | |
} | |
function getVillageNames() { | |
var villages = document.getElements("#sidebarBoxVillagelist ul li a"); | |
var villagesMap = {}; | |
for (var i = 0; i < villages.length; i++) { | |
let vil = villages[i]; | |
villagesMap[vil.innerText.trim()] = extractId(vil.href); | |
} | |
return villagesMap; | |
} | |
function extractId(villageUrl) { | |
return villageUrl.match(/newdid=([0-9]+)/)[1]; | |
} | |
function saveSettings() { | |
clearInterval(confirmationInterval); | |
var msgEl = document.getElementById("tbc-confirmation-msg"); | |
ids.forEach(function(id) { | |
localStorage.setItem(id, document.getElementById(id).value); | |
}); | |
localStorage.setItem("tbc-autosubmit", document.getElementById("tbc-autosubmit").checked); | |
document.getElementById("tbc-confirmation-msg").innerText = "Nastavení bylo uloženo!"; | |
confirmationInterval = setInterval(function() { | |
msgEl.innerText = ""; | |
}, 2000); | |
} | |
function restoreSettings() { | |
ids.forEach(function(id) { | |
var value = localStorage.getItem(id); | |
if (value) { | |
document.getElementById(id).value = value; | |
} | |
}); | |
document.getElementById("tbc-autosubmit").checked = shouldAutosubmit(); | |
} | |
function shouldAutosubmit() { | |
return localStorage.getItem("tbc-autosubmit") === "true"; | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment