//
// Author: Simon Amor
//
// ==UserScript==
// @name Shartak Rifle Reload
// @namespace https://www.shartak.com/
// @description Reload rifles until all rifles loaded or out of bullets (or AP)
// @include https://www.shartak.com/game.cgi
// ==/UserScript==
var bullet_li_tag = document.evaluate("//form//input[@value='rifle bullet']//parent::form//parent::li", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
var bullets = 0;
if (bullet_li_tag.snapshotLength) {
bullets = bullet_li_tag.snapshotItem(0).textContent.replace(" x ","");
}
var rifle_li_tag = document.evaluate("//form//input[@value='rifle']//parent::form//parent::li", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
// find out if we have any rifles that aren't full already
var rifles = 0;
if (rifle_li_tag.snapshotLength) {
for (var i = 0; i < rifle_li_tag.snapshotLength; i++) {
var rifle_item = rifle_li_tag.snapshotItem(i).textContent;
if (rifle_item.match(/\(0\)/)) {
rifles ++;
} else if (rifle_item.match(/\(1\)/)) {
rifles ++;
}
}
}
// Only if we have bullets and empty rifles..
if (bullets > 0 && rifles > 0) {
var bullet_li = bullet_li_tag.snapshotItem(0);
var refresh_form = document.evaluate("//form//input[@value='Refresh']//parent::form", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
// Find the 'Refresh' button and stick the 'Max Load' button after it.
if (refresh_form.snapshotLength) {
newform = document.createElement('form');
newform.innerHTML = "";
newform.setAttribute("method", "POST");
newform.setAttribute("class", "formblock");
newform.setAttribute("action", "/game.cgi");
newform.setAttribute("id", "gm_max_load");
bullet_li.parentNode.parentNode.insertBefore(newform,refresh_form.snapshotItem(0).nextSibling);
document.getElementById("gm_max_load").addEventListener('click', function(event) {
GM_setValue("shartak_rifle_reload", 1);
}, true);
if (GM_getValue("shartak_rifle_reload")) {
var bullet_form = document.evaluate("//form//input[@value='rifle bullet']//parent::form", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
if (bullet_form.snapshotLength) {
bullet_form.snapshotItem(0).setAttribute("id", "rifle_bullet_load");
document.getElementById("rifle_bullet_load").submit();
}
}
}
} else if (GM_getValue("shartak_rifle_reload")) {
GM_setValue("shartak_rifle_reload", 0);
var info_boxes = document.evaluate(
"//p[@class='info']",
document,
null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null);
if (info_boxes.snapshotLength) {
info_boxes.snapshotItem(0).innerHTML = info_boxes.snapshotItem(0).innerHTML + " Auto-reload complete";
}
}