Ok, I changed my mind on what I was going to write, and so I am writing a script that changes the clock from how much time is left in the turn, to when the turn will end. It's pretty useless, just a way to practice my coding.
Anyway, I can't seem to figure out a certain bug, so after many hours of trying, I thought I would post it here to see if anyone with more experience could spot it. I don't have a way to host the script right now, so I will just put it here.
- Code: Select all
// ==UserScript==
// @name Conquer Club Clock Modifier
// @include http://www.conquerclub.com/game.php?game=*
// ==/UserScript==
function extractTime (timeLeft, hoursLeft, minutesLeft, secondsLeft)
{
var tempTimeLeft = timeLeft;
var subTimeLeft = "";
var spaceLocation = 0;
var i = 0;
/*****
I believe I have identified the problem as two parts:
1. Replacing the line feeds (its line feed because the ASCII value is 10 when I use
tempTimeLeft.charCodeAt();) in the clock string.
2. Seperating the string so that I can use parseInt() on them to get the clock values. (This
might be solved with #1.
*****/
tempTimeLeft.replace("\s","_");
alert(tempTimeLeft);
spaceLocation = tempTimeLeft.indexOf("_");
alert(tempTimeLeft);
for(var i=0;i<spaceLocation;i++)
{
subTimeLeft.concat(tempTimeLeft[i]);
}
hoursLeft = parseInt(subTimeLeft,10);
tempTimeLeft.replace(subTimeLeft, "");
spaceLocation = tempTimeLeft.indexOf("_");
for(var i=0;i<spaceLocation;i++)
{
subTimeLeft.concat(tempTimeLeft[i]);
}
minutesLeft = parseInt(subTimeLeft,10);
tempTimeLeft.replace(subTimeLeft, "");
spaceLocation = tempTimeLeft.indexOf("_");
for(var i=0;i<spaceLocation;i++)
{
subTimeLeft.concat(tempTimeLeft[i]);
}
secondsLeft = parseInt(subTimeLeft,10);
tempTimeLeft.replace(subTimeLeft, "");
alert(hoursLeft + " " + minutesLeft + " " + secondsLeft);
}
function carryMinOrSec(time)
{
time=time-60;
}
function carryHr(time)
{
time=time-24;
}
function setTime (endTime, hoursLeft, minutesLeft, secondsLeft)
{
if(secondsLeft + endTime.getSeconds() >= 60)
{
carryMinOrSec(secondsLeft);
minutesLeft += (endTime.getminutes()+1);
}
endTime.setSeconds(secondsLeft + endTime.getSeconds());
if(minutesLeft + endTime.getMinutes() >= 60)
{
carryMinOrSec(minutesLeft);
hoursLeft += (endTime.getHours()+1);
}
endTime.setMinutes(minutesLeft + endTime.getMinutes());
if(hoursLeft + endTime.getHours() >= 24)
{
carryHr(hoursLeft);
if(endTime.getDay() == 6)
{
endTime.setDay(0);
}
else
{
endTime.setDay(endTime.getDay+1);
}
}
endTime.setHours(hoursLeft + endTime.getHours());
}
var timeLeft = document.getElementById('clock').innerHTML;
var hoursLeft = 0;
var minutesLeft = 0;
var secondsLeft = 0;
extractTime(timeLeft, hoursLeft, minutesLeft, secondsLeft);
var endTime = new Date();
setTime(endTime, hoursLeft, minutesLeft, secondsLeft);
var Day = "";
switch(endTime.getDay())
{
case 0:
Day="Sunday";
break;
case 1:
Day="Monday";
break;
case 2:
Day="Tuesday";
break;
case 3:
Day="Wednesday";
break;
case 4:
Day="Thursday";
break;
case 5:
Day="Friday";
break;
case 6:
Day="Saturday";
break;
}
newTime="Turn will end at " + endTime.getHours() + ":" + endTime.getMinutes() + ":" + endTime.getSeconds() + " " + Day;
document.getElementById('clock').innerHTML=timeLeft + "<br />" + newTime;
The problem is getting the time into variables from the clock's reading. I have put in comments in the function extractTime() showing where I think the problem is. If there is another way to go about extracting the time, I am open to that as well, this was just the only way I found.
Obviously, I will answer any questions about the code so that you have a better shot at fixing it, and thanks in advance for any help.