简体   繁体   中英

Using GreaseMonkey to change Javascript variable

I've got a question regarding a website that I use quite frequently. The website is essentially a browser-based air traffic control simulator. The page uses a php script to load the JavaScript directly into the browser when the game is initiated.

So, here is my question. In the JavaScript code, there is a function that is called when a new aircraft is generated on the screen.

function fnNewOne() { /* New aircraft generation code }

In the code, there is a command that sets the altitude of the plane to a certain height and then stores this value in a global variable. I wanted to know if it would be possible to alter this variable externally using a GreaseMonkey script in order to generate a different altitude. For example, I want flights to only enter the screen at a minimum altitude of 13,000 ft. How could I use GreaseMonkey to target the altitude variable? And when would be most appropriate to change the altitude that has initially been set by the game? What kind of event listener could I use?

If anyone could assist me in this, I would be very grateful.

The answer is that you just write your code in a GreaseMonkey script... There's nothing special about it.

It will be executed after the page loads, but there is a chance that your code will execute before the game initializes, so maybe you should set an interval to 100ms and watch for window.hasOwnProperty('altitude') to make sure the game is initialized (or whatever variable you want to fiddle with) and when that property is found, call whatever code you need and then clear your interval.

This is the best answer I have for you, given how poor your question is. Maybe if you gave us a few more details we'd be able to provide you with some better answers. What exactly are you trying to do? (what variable do you want to change, how and when?) What did you try? Do you have a JS question or a GM question? Have you checked out the GM documentation for examples? (there are very few, very succinct examples from which you can learn 95% of GM in a couple of hours)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM