更改控制台对象引用后,可以访问控制台 [英] Get access to the console once the console object reference was changed

查看:113
本文介绍了更改控制台对象引用后,可以访问控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢制作用户脚本。获得对你最喜欢的页面的更多控制或加速它的加载真的很有趣。

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

现在你将如何处理这个问题?提醒工作很好,但我已经习惯了Firebug和它的控制台了很多。

无法表达我对任何帮助的优雅程度。

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。:目前,英雄联盟论坛是相关网站。运行以下代码以查看问题:

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 () {}"


推荐答案

,我在这里有一个令人讨厌的解决方案。创建一个iframe(创建新窗口)并获取该iframe的控制台对象:

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

这篇关于更改控制台对象引用后,可以访问控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆