restore overridden window.JSON object

后端 未结 2 582
夕颜
夕颜 2021-01-18 06:27

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://ww         


        
2条回答
  •  一向
    一向 (楼主)
    2021-01-18 07:29

    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.

提交回复
热议问题