不涉及脚本引用的控制台日志记录

8

可能是一个没什么用的问题,但是很有趣,想知道facebook如何在不引用脚本的情况下将内容打印到浏览器控制台。在facebook.com上打开控制台,你会看到文本,但不会看到对javascript的引用...

enter image description here


1
您可以通过重新定义它们来轻松地覆盖JS函数。 console.log = function(){ /* 您想要的任何魔法 */ } - Lix
1
@ProstoTrader 这不是一个无用的问题。实际上,我也对答案很感兴趣。他们是如何做到的,这非常有趣。 - Ani
1
@ProstoTrader - 你需要做的就是确保开发者工具在一个独立的窗口中,这样你就可以检查检查器了:) 查看此帖子以获取更多信息: https://dev59.com/c2ct5IYBdhLWcg3wKqe9 - Lix
1
哇!我不知道我们可以检查检查器)))只需更改CSS,我们就可以隐藏参考)))#console-messages .link,#console-messages a { 颜色:透明; 光标:指针; } - Prosto Trader
1
@ProllyGeek 你不需要登录。只需转到 Facebook 网址并检查即可。 - Ani
显示剩余12条评论
2个回答

7

我的朋友的朋友找到了答案。

如果想在没有引用的情况下使用console.log,我们应该使用setTimout和bind。

setTimeout(console.log.bind(console, 'test'));

这里是完整的Facebook代码片段:

    var i = "Stop!",
        j = "This is a browser feature intended for developers. If someone told you to copy-paste something here to enable a Facebook feature or \"hack\" someone's account, it is a scam and will give them access to your Facebook account.";

if ((window.chrome || window.safari)) {
var l = 'font-family:helvetica; font-size:20px; ';
[
   [i, l + 'font-size:50px; font-weight:bold; ' + 'color:red; -webkit-text-stroke:1px black;'],
   [j, l],
   ['', '']
].map(function(r) {
    setTimeout(console.log.bind(console, '\n%c' + r[0], r[1]));
});
}

哇!这真是出乎意料,不知道Facebook的开发者为什么会这样做? - ProllyGeek
1
我能理解为什么,但我真的不明白他们是如何得到它的。可能其中一个之前在WebInspector上工作过,并知道那个错误。 - Prosto Trader
if ((window.chrome || window.safari)) 是无用的,因为它在 Firefox 上也能工作。 - Zibri

0
更通用的函数。
如果使用()而不是("在此输入您的标题","在此输入您的文本")运行
它将显示默认消息。
((title,message)=>{
    var i = title || "Stop!"
      , j = message || "This is a browser feature intended for developers. If someone told you to copy-paste something here to enable a Facebook feature or \"hack\" someone's account, it is a scam and will give them access to your Facebook account.";

    var l = 'font-family:helvetica; font-size:20px; ';
    [[i, l + 'font-size:50px; font-weight:bold; ' + 'color:red; -webkit-text-stroke:1px black;'], [j, l], ['', '']].map(function(r) {
        setTimeout(console.log.bind(console, '\n%c' + r[0], r[1]));
    });
})("Your title here","Your text here")

或者直接:

console.alert = function(title, message) {
  var i = title || "Stop!",
    j = message || "This is a browser feature intended for developers. If someone told you to copy-paste something here to enable a Facebook feature or \"hack\" someone's account, it is a scam and will give them access to your Facebook account.";

  var l = 'font-family:helvetica; font-size:20px; ';
  [
    [i, l + 'font-size:50px; font-weight:bold; ' + 'color:red; -webkit-text-stroke:1px black;'],
    [j, l],
    ['', '']
  ].map(function(r) {
    setTimeout(console.log.bind(console, '\n%c' + r[0], r[1]));
  });
}

并且

console.alert("Hello", "You can write what you like here!")

在 Angular 10 中无法工作,可能会在后续版本中修复。 - Zain Ul Abidin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接