简体   繁体   中英

Removing a single specific user script from WKUserContentController

I'm using addUserScript() to add a user script to my WKWebView's WKUserContentController. I noticed that even if call loadRequest() the script stays around.

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(). 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). 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 {}). But I guess the simplest way is just not to use foo() any more.

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