Set Arrival Time

Set the desired arrival time in Tribal Wars and the script will automatically send the attack

目前為 2018-04-07 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name Set Arrival Time
// @description Set the desired arrival time in Tribal Wars and the script will automatically send the attack
// @author FunnyPocketBook
// @version 2
// @date 03-10-2017
// @namespace FunnyPocketBook
// @include https://*/game.php?village=*&screen=place&try=confirm
// ==/UserScript==

// CHANGE THESE NUMBERS TO GET THE CORRECT ARRIVAL TIME - higher number means more delay. 
// Change "windows.delayTime" before "intervalTime" and only change "intervalTime" if you 
// can't get it quite correct with only "delayTime"
delayTime = 11; // Set delay in ms
window.intervalTime = 10; // Set interval in ms



var arrival;
var reloadBefore = 1; // Reload page 1 minute before attack is supposed to be sent
/*
*  Save tab ID
*/
var tabID = sessionStorage.tabID ? sessionStorage.tabID : sessionStorage.tabID = Math.random(); // Generate ID for tab
console.log("tabID: " + tabID);
var storedID = JSON.parse(localStorage.getItem("storedID")); // Get tab ID
console.log("storedID: " + storedID);
if(storedID == null) { // See if any tab IDs already exist, if not, initialize the array
    storedID = [];
    console.log("storedID = " + storedID);
}
if(!storedID.includes(tabID) && !storedID.includes(parseFloat(tabID))) { // If the tab ID of the current tab is not stored yet, push the array
    storedID.push(tabID);
    console.log("storedID pushed: " + storedID);
}
localStorage.setItem("storedID", JSON.stringify(storedID));
console.log("localStorage id set to " + localStorage.getItem("storedID"));

/*
*  Reload boolean - set to true to reload, false to not reload
*/
var reload = JSON.parse(localStorage.getItem("reload")); // Get reload
if(reload == null) {
    reload = [];
}
console.log("Reload: " + reload);
localStorage.setItem("reload", JSON.stringify(reload));

/*
*  Get stored times
*/
var index = storedID.indexOf(tabID); // Get index of time of this tab
var storedTimes = JSON.parse(localStorage.getItem("storedTimes")); // Get stored times
if(storedTimes == null) { // If there are no times yet, initialize array
    storedTimes = [];
}
console.log("storedTimes = " + storedTimes);
localStorage.setItem("storedTimes", JSON.stringify(storedTimes));
console.log("localStorage times set to " + localStorage.getItem("storedTimes"));
var fullTime = storedTimes[index];
var inputTime;
var inputMs;
if(fullTime != null) {
    inputTime = fullTime.slice(0, 8);
    inputMs = fullTime.slice(9);
}
// Set arrival time button
var showArrTime = document.createElement("tr"); 
var showArrTimeTd = document.createElement("td");
var showArrTimeTd2 = document.createElement("td");
var pEle = document.getElementById("troop_confirm_go"); // Button comes after this element
var btn = document.createElement("a"); // Create button called btn as a link because any button causes the attack to launch
btn.setAttribute("id", "arrTime"); 
btn.setAttribute("class", "btn"); 
btn.setAttribute("style", "cursor:pointer;");
pEle.parentNode.insertBefore(btn, pEle.nextElementSibling); // Place btn after pEle
var t = document.createTextNode("Set arrival time"); 
btn.appendChild(t); 

// Create cell where set arrival time is shown
var dateArrival = document.getElementById("date_arrival"); // Cell of relative_time
showArrTime.appendChild(showArrTimeTd);
showArrTime.appendChild(showArrTimeTd2);
dateArrival.parentNode.parentNode.insertBefore(showArrTime, dateArrival.parentNode[1]); // Insert tablerow as last cell
showArrTimeTd.innerHTML = "Set arrival:";
showArrTimeTd2.setAttribute("id", "setArrTime");
// Display the set time if exist, else say "No time set"
if(inputTime != null || inputMs != null) {
    showArrTimeTd2.innerHTML = "" + inputTime + ":" + inputMs;
} else {
    showArrTimeTd2.innerHTML = "No time set";
    inputTime = "";
    inputMs = "";
}

showArrTimeTd.setAttribute("id", "showArrTime");
var inputTime;
var inputMs;
btn.onclick = function() {
	"use strict";
	var time = document.getElementsByClassName("relative_time")[0].textContent.slice(-8); // Get the time of arrival if sent now
	inputTime = prompt("Please enter desired arrival time", time); // Save desired time
	inputMs = parseInt(prompt("Please enter approximate milliseconds", "000")); // Save desired ms
    var setTime = inputTime + ":" + inputMs;
    showArrTimeTd2.innerHTML = inputTime + ":" + inputMs;
    storedTimes[index] = setTime; // Add new time to the array, same index as the tab ID
    console.log("SADFLKJASDFLKJSADF " + storedTimes[index]);
    localStorage.setItem("storedTimes", JSON.stringify(storedTimes));
    reload[index] = true;
    localStorage.setItem("reload", JSON.stringify(reload));
};

// Create delay input
var delayTime = localStorage.getItem("delayTime");
if(delayTime == null) {
    delayTime = 0;
}

// Create okay button to save delay
var delayRow = document.createElement("tr"); 
var delayC1 = document.createElement("td");
var delayC2 = document.createElement("td");
var parent1 = document.getElementById("date_arrival"); // Cell of relative_time
delayRow.appendChild(delayC1);
delayRow.appendChild(delayC2);
parent1.parentNode.parentNode.insertBefore(delayRow, parent1.parentNode[1]); // Insert tablerow as last cell
delayC1.innerHTML = "Offset";
delayC2.innerHTML = "<input id = 'delayInput' value = " + delayTime + " style = 'width: 50%'></input> <a id = 'delayButton' class = 'btn'>OK</a>";
$("#delayButton").click(function() {
	delayTime = $("#delayInput").val();
    localStorage.setItem("delayTime", JSON.stringify(delayTime));
    console.log("delayTime: " + localStorage.getItem("delayTime"));
})

console.log(inputTime + ":" + inputMs);
console.log(delayTime);
var delay = delayTime + parseInt(inputMs); // setTimeout time
var intervalCheck = setInterval(function retime() {
    "use strict";
    arrival = document.getElementsByClassName("relative_time")[0].textContent; // Get time of arrival if sent now for comparison
    delay = delayTime + parseInt(inputMs); // setTimeout time
    if(arrival.slice(-8) === inputTime) {
        console.log("inside");
        setTimeout(function() {
            console.log("sent");
            document.getElementById("troop_confirm_go").click();
            // Clear set time for this tab and save again in localStorage
            storedTimes[index] = null; 
            localStorage.setItem("storedTimes", JSON.stringify(storedTimes));
        }, delay);
        clearInterval(intervalCheck);
    }
}, window.intervalTime);

var minute = inputTime.substr(3,2) - reloadBefore; // Get minute of input and subtract reloadBefore from it
var arriveNow = document.getElementsByClassName("relative_time")[0].textContent.slice(-8); // Get relative_time to compare
var timeReload;
setInterval(function() {
    minute = inputTime.substr(3,2) - reloadBefore;
    arriveNow = document.getElementsByClassName("relative_time")[0].textContent.slice(-8);
    timeReload = inputTime.substr(0,3) + minute + inputTime.substr(5,3); // Time stitched together with the minute subtracted by reloadBefore
    if(timeReload != -1 && arriveNow === timeReload && reload[index]) {
        reload[index] = false;
        localStorage.setItem("reload", JSON.stringify(reload));
        window.location.reload();
    }
}, 1000);