Event without event

So, I recently decided to start learning JavaScript. I only get knowledge of VB.NET for programming knowledge and HTML and CSS for design. Anyway, scrap.tf is a TF2 banking website that makes things automatic. I plan to write a basic Chrome plugin, and I want this function to happen if a button is clicked. Everything is set up for me, but when the button is pressed, only scrap.tf/hats, EnQueueHatBank () takes me away; This is the JS command that they use to join the queue. It never even works if I don’t type it after I’m on the site. Do I have to wait until it lights up?

if (location.href === 'http://scrap.tf/hats') { EnQueueHatBank(); } else { window.location.href='http://scrap.tf/hats'; EnQueueHatBank(); } 
+4
source share
3 answers

I think that in this conversation = vs == no point. You change window.location before calling EnQueueHatBank , so before the function is called, you go to a new page. This is what prevents him from running. So the first thing you need to do is:

  • First call EnQueueHatBank.

     if (location.href === 'http://www.scrap.tf/hats') { EnQueueHatBank(); } else { EnQueueHatBank(); window.location.href='http://www.scrap.tf/hats'; } 
  • Clear the code a bit, because the structure is a little inconvenient. You call EnQueueHatBank anyway, so there is no need for it to be in the if statement:

     EnQueueHatBank(); if (window.location.href !== 'http://www.scrap.tf/hats') { window.location.href = 'http://www.scrap.tf/hats'; } 
  • Finally, remember that http://www.scrap.tf/hats/ probably refers to the same place as http://www.scrap.tf/hats , not to mention https://www.scrap.tf/hats?foo=bar etc. You would be better off doing a less rigorous test:

     EnQueueHatBank(); if (window.location.href.indexOf('://www.scrap.tf/hats') > -1) { window.location.href = 'http://www.scrap.tf/hats'; } 

EDIT: Based on your comment, you will need to do this:

 if (window.location.href.indexOf('://www.scrap.tf/hats') > -1) { EnQueueHatBank(); } else { window.location.href = 'http://www.scrap.tf/hats'; } 

This will only work if your program starts up again after going to scrap.tf/hats , so make sure it starts every time you load a new page.

For security reasons, you cannot initiate code on one page and continue after you have moved to another place. You will need to call EnQueueHatBank from the page on which it was supposed to work.

+2
source

You need to fix the comparison in order to use two equal signs.

 if (location.href == 'http://www.scrap.tf/hats') { EnQueueHatBank(); } 

Once you are done using tools like jslint, javascript even offers the === operator, which also checks the type (checks whether both sides are strings, as in this example).

+3
source

You must use the comparison operation === , but you did assign the value = .

 if (location.href === 'http://www.scrap.tf/hats') { EnQueueHatBank(); } else { window.location.href='http://www.scrap.tf/hats'; EnQueueHatBank(); } 
+1
source

Source: https://habr.com/ru/post/1491185/


All Articles