简体   繁体   English

从WKUserContentController删除单个特定的用户脚本

[英]Removing a single specific user script from WKUserContentController

I'm using addUserScript() to add a user script to my WKWebView's WKUserContentController. 我正在使用addUserScript()将用户脚本添加到WKWebView的WKUserContentController。 I noticed that even if call loadRequest() the script stays around. 我注意到,即使调用loadRequest(),脚本仍然存在。

For certain use cases I may need to remove certain scripts and add others. 对于某些用例,我可能需要删除某些脚本并添加其他脚本。 However, it seems that the only way to remove scripts is removeAllUserScripts(). 但是,似乎删除脚本的唯一方法是removeAllUserScripts()。 This is very inconvenient since it means I would have to remove all scripts and then re-add back the ones which I wanted to keep. 这非常不方便,因为这意味着我将必须删除所有脚本,然后重新添加要保留的脚本。

If anyone knows of any way to delete a specific script (by name, handle, etc.) please let me know. 如果有人知道删除特定脚本的任何方式(按名称,句柄等),请告诉我。

I don't think "removing a script" is something possible (or even meaningful if I understand correctly your question). 我认为“删除脚本”是不可能的(如果我正确理解您的问题,甚至是没有意义的)。

A "script" is just text that is used to provide the Javascript environment with definitions of elements such as variables, functions, etc... Two scripts could very well over-ride each other (eg define the same function foo()), so depending on the order you loaded them, you have a different end environment (you only have the foo() definition of the last loaded script). “脚本”只是用于为Javascript环境提供诸如变量,函数等元素的定义的文本。两个脚本可以很好地相互替代(例如,定义相同的函数foo()),因此,根据加载它们的顺序,您会拥有不同的最终环境(您只有最后加载的脚本的foo()定义)。 So what would "remove a script" mean in that case ? 那么,在这种情况下,“删除脚本”将意味着什么?

If you really want to get rid of elements of your Javascript environment, you can always re-define them to something that does not do anything (eg redefine foo() to {}). 如果您确实想摆脱Javascript环境中的元素,则始终可以将它们重新定义为什么也不做的事情(例如,将foo()重新定义为{})。 But I guess the simplest way is just not to use foo() any more. 但是我想最简单的方法就是不再使用foo()。

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

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