简体   繁体   English

使用GreaseMonkey更改Javascript变量

[英]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. 启动游戏时,该页面使用php脚本将JavaScript直接加载到浏览器中。

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. 在JavaScript代码中,有一个函数在屏幕上生成新飞机时调用。

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. 我想知道是否可以使用GreaseMonkey脚本从外部更改此变量以生成不同的高度。 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? 例如,我希望航班仅在最低高度13,000英尺处进入屏幕。如何使用GreaseMonkey定位高度变量? 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. 答案是,您只需要使用GreaseMonkey脚本编写代码即可...没什么特别的。

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. 它会在页面加载后执行,但是您的代码有可能在游戏初始化之前执行,因此也许您应该将时间间隔设置为100ms并注意window.hasOwnProperty('altitude')以确保游戏是初始化(或要使用的任何变量),并在找到该属性后,调用所需的任何代码,然后清除间隔。

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? 您有JS问题还是GM问题? Have you checked out the GM documentation for examples? 您是否已查看了GM文档中的示例? (there are very few, very succinct examples from which you can learn 95% of GM in a couple of hours) (很少有非常简洁的示例,您可以在几个小时内从中学习95%的GM)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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