A Web Page's Javascript Causing an Error in My WKUserScript Loaded in a Custom Content World

I have a global variable in WKUserScript declared as a const in Javascript.

I load this script in a custom WKContentWorld I create with my own name (not the pageWorld or default world).

There some nasty javascript out there on most popular sites. I noticed that unhandled errors on the page can break stop my WKUserScript from working. In one particular case there is this nasty script on page for ads that uses document.write like so:

b.contentWindow.document.write(e),
b.contentWindow.document.close()

And the web inspector shows an error here "Can't create duplicate variable: 'MyGlobalVariableNameInsMyUserScriptHere'"

This script being used on the web page is going to end up getting blocked. But my question is this, isn't using my own content world supposed to protect me against this type of thing? This global javascript variable (an array) is global in my javascript file and holds some data in it but it is not inserted into the DOM. Shouldn't document.write/document.close calls from the page not cause this type of error in my user script?

And if the entire DOM is being rewritten shouldn't a new script (representing my user script) be created from scratch? I think I can avoid the error by changing the variable from const to var (haven't tried yet). But is this how it's supposed to be?

Changing the Global variable from let to var fixed the error but I am still curious if this behavior is intended or would be considered a bug.

A Web Page's Javascript Causing an Error in My WKUserScript Loaded in a Custom Content World
 
 
Q