您需要先安装一款用户样式管理器扩展(如 Stylus)后才能安装此样式。
您需要先安装一款用户样式管理器扩展(如 Stylus)后才能安装此样式。
您需要先安装一款用户样式管理器扩展(如 Stylus)后才能安装此样式。
您需要先安装一款用户样式管理器扩展后才能安装此样式。
您需要先安装一款用户样式管理器扩展后才能安装此样式。
您需要先安装一款用户样式管理器扩展后才能安装此样式。
(我已经安装了用户样式管理器,让我安装!)
// ==UserScript==
// @name Premium Exchange - Buy Resources
// @description Automatically buy resources up to a predefined amount of resources
// @author FunnyPocketBook
// @version 2.0.4.1
// @include https://*/game.php*screen=market*
// @namespace https://greasyfork.org/users/151096
// ==/UserScript==
const incoming = "Incoming";
const timeout = 9000;
let topUp, price, stack;
let start = false;
createInput();
function createInput() {
"use strict";
const userInputParent = _ID("premium_exchange_form"); // Parent element
// Create input for setting how much res should be bought
const divScript = document.createElement("div");
divScript.setAttribute("id", "divScript");
userInputParent.parentNode.insertBefore(divScript, userInputParent);
_ID("divScript").innerHTML = "<p>Top up warehouse to: <input id='topUpInput'> " +
"<button id='topUpOk' class='btn'>OK</button><span id='topUpText'></span></p><p>Buy when price above: <input id='priceInput'> " +
"<button id='priceOk' class='btn'>OK</button><span id='priceText'></span></p>" +
"<p>Buy max this much at once: <input id='stackInput'> <button id='stackOk' class='btn'>OK</button><span id='stackText'></span></p>" +
"<p>Buy the whole stock at one: <input type=\"checkbox\" name=\"buyStock\" id=\"buyStock\"></p><span style='color:red'>ATTENTION! This might deplete your Premium Points completely, as it buys everything that is available!</span>" +
"<p>Buy resources:</p><p><input type=\"checkbox\" name=\"wood\" id=\"woodCheck\"> Wood <input type=\"checkbox\" " +
"name=\"stone\" id=\"stoneCheck\"> Stone <input type=\"checkbox\" name=\"iron\" id=\"ironCheck\"> Iron</p>" +
"<p><button id='start' class='btn'></button></p>";
if (!start) {
_ID("start").innerHTML = "Start";
} else {
_ID("start").innerHTML = "Stop";
}
if (localStorage.topUp) {
_ID("topUpInput").value = localStorage.topUp;
topUp = localStorage.topUp;
}
if (localStorage.price) {
_ID("priceInput").value = localStorage.price;
price = localStorage.price;
}
if (localStorage.stack) {
_ID("stackInput").value = localStorage.stack;
stack = localStorage.stack;
}
}
_ID("topUpOk").addEventListener("click", function() {
topUp = _ID("topUpInput").value;
localStorage.topUp = topUp;
_ID("topUpText").innerHTML = "Top up to " + topUp;
});
_ID("priceOk").addEventListener("click", function() {
price = _ID("priceInput").value;
localStorage.price = price;
_ID("priceText").innerHTML = "Buy when price above " + price;
});
_ID("stackOk").addEventListener("click", function() {
stack = _ID("stackInput").value;
localStorage.stack = stack;
_ID("stackText").innerHTML = "Buy only " + stack + " resources at once";
});
_ID("start").addEventListener("click", function() {
if (start) {
start = false;
_ID("start").innerHTML = "Start";
} else {
start = true;
_ID("start").innerHTML = "Stop";
buyRes();
}
});
_ID("topUpInput").addEventListener("keydown", clickOnKeyPress.bind(this, 13, "#topUpOk"));
_ID("priceInput").addEventListener("keydown", clickOnKeyPress.bind(this, 13, "#priceOk"));
_ID("stackInput").addEventListener("keydown", clickOnKeyPress.bind(this, 13, "#stackOk"));
function Resource(wh, price, stock, inc, input) {
this.wh = wh;
this.price = price;
this.stock = stock;
this.inc = inc;
this.inputBuy = input;
this.buy = 0;
}
let wood = new Resource(game_data.village.wood, parseInt(__("#premium_exchange_rate_wood > div:nth-child(1)").innerText), parseInt(__("#premium_exchange_stock_wood").innerText), 0, __("#premium_exchange_buy_wood > div:nth-child(1) > input"));
let iron = new Resource(game_data.village.iron, parseInt(__("#premium_exchange_rate_iron > div:nth-child(1)").innerText), parseInt(__("#premium_exchange_stock_iron").innerText), 0, __("#premium_exchange_buy_iron > div:nth-child(1) > input"));
let stone = new Resource(game_data.village.stone, parseInt(__("#premium_exchange_rate_stone > div:nth-child(1)").innerText), parseInt(__("#premium_exchange_stock_stone").innerText), 0, __("#premium_exchange_buy_stone > div:nth-child(1) > input"));
let warehouse = game_data.village.res[6];
if (start) {
buyRes();
}
const interval = setInterval(function() {
if (start && (!document.querySelector("#fader") || document.querySelector("#fader").style.display === "none")) {
buyRes();
}
}, timeout);
function buyRes() {
getRes();
if (__("#buyStock").checked || wood.wh + wood.inc < topUp || stone.wh + stone.inc< topUp || iron.wh + iron.inc < topUp) {
if (__("#buyStock").checked && __("#woodCheck").checked || wood.price > price && wood.wh + wood.inc < topUp && __("#woodCheck").checked) {
// Buy wood
wood.buy = topUp - wood.wh - wood.inc;
if (wood.buy <= 0) {
wood.buy = wood.price - 2;
}
if(wood.buy > stack) {
wood.buy = stack;
}
if(__("#buyStock").checked && wood.stock > 0) {
wood.buy = wood.stock - 1;
}
stone.inputBuy.value = "";
iron.inputBuy.value = "";
wood.inputBuy.value = wood.buy;
clickBuy();
} else if (__("#buyStock").checked && __("#stoneCheck").checked || stone.price > price && stone.wh + stone.inc < topUp && __("#stoneCheck").checked) {
// Buy stone
stone.buy = topUp - stone.wh - stone.inc;
if (stone.buy <= 0) {
stone.buy = stone.price - 2;
}
if(stone.buy > stack) {
stone.buy = stack;
}
if(__("#buyStock").checked && stone.stock > 0) {
stone.buy = stone.stock - 1;
}
wood.inputBuy.value = "";
iron.inputBuy.value = "";
stone.inputBuy.value = stone.buy;
clickBuy();
} else if (__("#buyStock").checked && __("#ironCheck").checked || iron.price > price && iron.wh + iron.inc < topUp && __("#ironCheck").checked) {
// Buy iron
iron.buy = topUp - iron.wh - iron.inc;
if (iron.buy <= 0) {
iron.buy = iron.price - 2;
}
if(iron.buy > stack) {
iron.buy = stack;
}
if(__("#buyStock").checked && iron.stock > 0) {
iron.buy = iron.stock - 1;
}
wood.inputBuy.value = "";
stone.inputBuy.value = "";
iron.inputBuy.value = iron.buy;
clickBuy();
}
}
}
function clickBuy() {
__("#premium_exchange_form > input").click();
setTimeout(function() {
__("#premium_exchange > div > div > div.confirmation-buttons > button.btn.evt-confirm-btn.btn-confirm-yes").click();
}, 1000);
}
function _ID(selector) {
return document.getElementById(selector);
}
function __(selector) {
return document.querySelector(selector);
}
function getRes() {
let parentInc;
warehouse = game_data.village.res[6];
wood.wh = game_data.village.wood;
stone.wh = game_data.village.stone;
iron.wh = game_data.village.iron;
wood.price = parseInt(__("#premium_exchange_rate_wood > div:nth-child(1)").innerText);
stone.price = parseInt(__("#premium_exchange_rate_stone > div:nth-child(1)").innerText);
iron.price = parseInt(__("#premium_exchange_rate_iron > div:nth-child(1)").innerText);
try {
if (__("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(1)").innerHTML.split(" ")[0].replace(":", "") === incoming) {
parentInc = __("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(1)");
}
} catch(e) {}
try {
if (__("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(2)").innerHTML.split(" ")[0].replace(":", "") === incoming) {
parentInc = __("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(2)");
}
} catch(e) {}
try {
wood.inc = setZeroIfNaN(parseInt(parentInc.querySelector(".wood").parentElement.innerText.replace(".", "")));
} catch (e) {}
try {
stone.inc = setZeroIfNaN(parseInt(parentInc.querySelector(".stone").parentElement.innerText.replace(".", "")));
} catch (e) {}
try {
iron.inc = setZeroIfNaN(parseInt(parentInc.querySelector(".iron").parentElement.innerText.replace(".", "")));
} catch (e) {}
}
function clickOnKeyPress(key, selector) {
"use strict";
if (event.defaultPrevented) {
return; // Should do nothing if the default action has been cancelled
}
let handled = false;
if (event.key === key) {
document.querySelector(selector).click();
handled = true;
} else if (event.keyIdentifier === key) {
document.querySelector(selector).click();
handled = true;
} else if (event.keyCode === key) {
document.querySelector(selector).click();
handled = true;
}
if (handled) {
event.preventDefault();
}
}
function setZeroIfNaN(x) {
"use strict";
if ((typeof x === 'number') && (x % 1 === 0)) {
return x;
} else {
return 0;
};
}