简体   繁体   English

如何运行javascript以还原嵌入式脚本资源中的某些代码?

[英]How do I run javascript to revert what some code in an embedded script resource just did?

So, I'm working on a mapping application. 因此,我正在开发一个映射应用程序。 In the app there are these toolbars and based on certain circumstances I would like to disable specific tools. 在应用程序中有这些工具栏,根据某些情况,我想禁用特定的工具。 When a tool is disabled it's image changes. 禁用工具后,其图像会更改。 This part works fine. 这部分工作正常。

Now, there is all this Javascript that is in embedded script resources that is ran when certain actions occur, like selecting a tool. 现在,当某些动作(例如选择工具)发生时,嵌入式脚本资源中就会包含所有这些Javascript。 I have no power over this Javascript. 我对这个Javascript无能为力。

What happens is some Javascript gets ran that essentially changes the image back from it's disabled one to it's default one. 发生的事情是运行了一些Javascript,从本质上将图像从禁用状态更改为默认状态。 This is bad. 这不好。

I'd like to know if there is any way to tell if the code from the script resource has ran or any way to see when the image has been changed so I can immediately change it back. 我想知道是否有任何方法可以判断脚本资源中的代码是否已运行,或者有什么方法可以查看何时更改了图像,因此我可以立即将其更改。 Essentially over-riding the code in the script resource. 本质上是覆盖脚本资源中的代码。

To word it differently, whenever this icons image changes from the disabled one to the enabled one via this specific javascript code, can I immediately change it back to the disabled image? 换句话说,只要此图标图像通过此特定的javascript代码从禁用的图像变为启用的图像,我是否可以立即将其更改回禁用的图像?

Although I think this is some ugly workaround, you can do this: 尽管我认为这是一个丑陋的解决方法,但是您可以执行以下操作:

Let's say the id of the image that you want to change back to disabled image when enabled by third part javascripts is "image1" then: 假设您要在由第三方javascripts启用后要更改回禁用图像的图像ID为“ image1”,然后:

disable = function() {
   $("image1").src = "lalala"; // lala being you disabled image URI
   $("image1").observe("load", function(e) {
       if(this.src != "lalala") this.src = "lalala";
   });
}

What you will be doing is observing the load of the image, and checking its src attribute. 您将要做的是观察图像的负载,并检查其src属性。 If it was changed, change it back to your desired value. 如果已更改,请将其更改回所需的值。

Oh, this snippet is using PrototypeJS methods. 哦,此代码段正在使用PrototypeJS方法。 Sure you can translate that to your preferred JS framework or pure javascript. 当然,您可以将其转换为您首选的JS框架或纯JavaScript。

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

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