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 () {}"
有帮助吗?

解决方案

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...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top