在浏览器控制台输出图片

今天无意中在百度知道页面打开了控制台,看到了控制台里居然有百度的logo

研究发现方法如下

if(!console){
    console = {}
    console.log = function(){}
}
 
 
function setConsoleImage(url){
    var img = new Image();
    img.style.display = "none";
    
    document.body.appendChild(img);
    img.src = url;
    img.onload = function(){
        
        var css = [
            'padding: ' + (img.height/2 - 8) + 'px ' + img.width/2 + 'px;',
            'line-height: ' + img.height +'px;',
            'background-image: url(' + url + ');'
        ]
        console.log('%c', css.join(''));
    }
}
 
window.onload = function(){
    setConsoleImage("xxx.jpg");
    setConsoleImage("xxx.gif")
}
原文地址:https://www.cnblogs.com/raoyunxiao/p/4768088.html