I’m working on code that is injected on web pages (using a browser add-on or with a script tag).
The problem is that we want to use global objects and variables like JSON, window.location, String.split, etc. and the implementation of these may have been changed by the web page. This may make our code fail, and it is a security problem.
Example:
>>> String.prototype.split = function() { return 'foo'; };
function()
>>> 'a,b,c'.split(','); // gives unexpected result
"foo"
So, is there a way to get access to the browser’s default implementation of objects and functions as they were before they were changed? It does not have to be standard, I just want the functionality to exist.
Update
Perhaps a more viable way would be to create an empty
<iframe>dynamically.Here’s an example that contaminates
String.prototype.splitin the parent window but gets a clean one from<iframe>.Not in the ordinary sense; although there might be some exotic hacks out there.The only way I could think of, was to make sure your code gets loaded before any other script. If that requirement if fulfilled, necessary global variables can be cloned into a safe location.