Get access to the console once the console object reference was changed

不问归期 提交于 2019-12-02 05:00:52

问题


I like making userscripts. It's real fun to get some more control of your favorite page or just speed up it's loading.

Curently, I came across a problem that a page either defines console reference to a new dummy object:

window.console = {log: function() {}, info: function() {} ... };

Or it even destroys the functions:

window.console.log = function() {};
window.console.info = function() {};
...

I'm using window to make it obvious that I'm talking about the global scope. Also, I didn't use quick assigment to the same function on purpose, in the second example

Now how would you deal with this? Alerts work nice, but I've got used to Firebug and it's console quite a lot.
Can't express how graceful will I be for any help.

PNS.: Currently, the League of Legends forums is the site in question. Run the following code to see the problem:

window.console.log.toString(); //returns "function () {}"

回答1:


Well, I've got one nasty solution here. Create an iframe (which creates new window) and get the console object of that iframe:

function healConsole() {
  //<iframe> element
  var iframe = document.createElement("iframe");
  //Hide it somewhere
  iframe.style.position="fixed";
  iframe.style.height = iframe.style.width = "1px";
  iframe.style.top = iframe.style.left = "-5px";
  //No src to prevent loading some data
  iframe.src = "about: blank";
  //Needs append to work
  document.body.appendChild(iframe);
  //Get the inner console
  window.console = iframe.contentWindow.console;
}

Not sure how cross browser is this though. I'm looking for something better...



来源:https://stackoverflow.com/questions/21944457/get-access-to-the-console-once-the-console-object-reference-was-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!