restore overridden window.JSON object

2019-06-17 07:37发布

Some code that I don't have control over is overriding the global JSON object without checking if it's already implemented:

var JSON = {
  org: "http://www.JSON.org",
  copyright: "(c)2005 JSON.org",
  license: "http://www.crockford.com/JSON/license.html",
  stringify: function(a, g) {
     ...

The problem is that this version of the JSON parser is very old and has a bug, which is fouling up my attempts at serialization. (Others have had a similar problem with this implementation.)

Can I get at the browser's native implementation? I thought delete would work, but it doesn't. I suspect that's because JSON is an object and not a method in the prototype. Is there some other way to get at it?

2条回答
甜甜的少女心
2楼-- · 2019-06-17 07:50

You can create an iframe element (which will load about:blank and hence create a new context) and get a JSON object from there.

function restoreJSON() {
  var f = document.createElement("iframe");
  f.style.display = "none";
  document.documentElement.appendChild(f);
  window.JSON = f.contentWindow.JSON;
  document.documentElement.removeChild(f);
}

about:blank is loaded synchronously, so no need to wait for the load event. While this isn't restoring the original JSON object, it is getting one black-box identical to it.

查看更多
▲ chillily
3楼-- · 2019-06-17 07:56

Since the code that you don't have control over is overriding the original before you come along in the page, you have two options:

Inject an iframe and grab the JSON off the contextWindow (as indicated in the other answer on this question at the time of this edit), or, alternately, just use the https://github.com/douglascrockford/JSON-js JSON library as your own insert. Note that using Crockford's does give cross-browser-guarantees of conformance, but the native implementations are often faster.

An alternative if you have the ability in the future to come along on the page before the offending code, is to inject something before that offending "code that helps" to grab the JSON object:

<html>
  <head>
    <script>
      window.myJson = window.JSON;
    </script>
 ....
查看更多
登录 后发表回答