我正在使用Twitter网站进行一些Javascript研究。在任何网站中,我都可以打开Google Devtools并使用控制台操作页面上的任何内容,例如获取DOM节点,编辑元素等。
例如,当我打开“www.google.com”时,请转到Devtools并输入上面的命令:
的console.log( '试验');
我在控制台中显示'测试'字符串。
然而,当我打开'www.twitter.com'并做同样的事情时,没有发生。控制台中没有显示任何内容,只是一个'undefined'字符串,如下所示:
为什么这种行为只发生在Twitter网站上?
编辑:尝试了提出的解决方案 "delete" - restore native function not working for changed prototype, how then?
但没有奏效:
答案 0 :(得分:6)
在Javascript中,您可以修改全局对象,因此您可以执行类似
的操作Array.prototype.push = function(element) {
this[this.length] = 'blah'
}
现在,每次向任何数组添加元素时,都会添加'blah';
const myArray = [];
myArray.push(1);
myArray.push(2);
myArray.push(3);
console.log(myArray);
// output ['blah', 'blah', 'blah']
在Twitter网站上,他们做了同样的事情,虽然代码缩小了,你可以在这里看到它:
, Y = ["log", "warn", "debug", "info"]
, K = function() {}
, Q = ""
第1414行https://abs.twimg.com/k/en/init.en.caa653749241467e7dbb.js
要使其再次运行,请复制每一行并在控制台上运行(将此解决方案归功于Rob W):
var frame = document.createElement('iframe');
document.body.appendChild(frame);
console = frame.contentWindow.console
console.log('it works')
答案 1 :(得分:1)
如果您只输入console.log
(没有任何括号),则可以列出该网站上日志功能的代码。在Twitter上执行此操作会为您提供
function (){}
这确实是一个空函数,表明他们已经覆盖了默认值。相比之下,google.com上的相同内容
function log() { [native code] }
这是默认设置。