سؤال

I have written the following Greasemonkey script to add or remove cookies in the popular game Cookie Clicker.

function addCookies()
{
var cookiesString = prompt("How many cookies would you like to gain?");
var cookiesInt = Math.floor(cookiesString);
Game.Earn(cookiesInt);
alert("Your cookies have been added!");
}
function removeCookies()
{
var cookiesString2 = prompt("How many cookies would you like to remove?");
var cookiesInt2 = Math.floor(cookiesString2);
Game.cookies -= cookiesInt2;
alert("Your cookies have been removed.");
}
window.onkeydown = function(event) {
   if (event.keyCode === 81) {
      addCookies();
   }
   if (event.keyCode === 87) {
      removeCookies();
   }
   if (event.keyCode === 18) {
       alert('To add cookies, press "Q".  To remove cookies, press "W".');
};

For some reason, they code doesn't run when I press the "Q" key (or any other key, for that matter). Does anyone have any idea why this may be?

I ran it through JSLint, but all I got was just about the fact that the "Game" function wasn't defined. This is not the problem, because the code will be running on the webpage, which has defined the "Game" function. Just to make sure, I removed those lines of code, and I still couldn't get the prompt to pop up with any key.

Any help would be greatly appreciated.

هل كانت مفيدة؟

المحلول

You're missing an end bracket on your final if statement. Corrected:

/* ...snip */
window.onkeydown = function(event) {
   if (event.keyCode === 81) {
      addCookies();
   }
   if (event.keyCode === 87) {
      removeCookies();
   }
   if (event.keyCode === 18) {
       alert('To add cookies, press "Q".  To remove cookies, press "W".');
   }
};
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top