A maneira mais fácil de obter o ícone do site é obtê -lo no site/favicon.ico. No entanto, como muitos sites configuram Favicon na página, esse método não está disponível em muitas situações.
Uma maneira melhor é conseguir isso através dos serviços prestados pelo Google:
http://www.google.com/s2/favicons?domain=http://www.baidu.com
Código:
A cópia do código é a seguinte:
<! doctype html>
<html>
<head>
<meta charset = "utf-8">
<style type = "text/css">
#entrada {
Altura: 300px;
preenchimento: 10px 5px;
altura de linha: 20px;
Largura: 1000px;
}
#enviar {
Altura: 30px;
Alinhamento de texto: centro;
Cor: #ffffff;
altura de linha: 30px;
Largura: 80px;
Background-Color: Blue;
Margin-top: 20px;
}
#result {
Margin-top: 20px;
}
#Result Li {
Altura: 40px;
altura da linha: 40px;
flutuar: esquerda;
margem: 10px 14px;
}
</style>
</head>
<Body>
<textarea id = "input" placeholder = "insira vários URLs com intervalos de espaço"> </sexttarea>
<div id = "submeter"> obtenha ícone </div>
<ul id = "resultado">
</ul>
<script type = "text/javascript">
var input = document.getElementById ("input");
var envia = document.getElementById ("submeter");
var resultado = document.getElementById ("resultado");
var val;
Função Trim (str) {
var whitespace = '/n/r/t/f/x0b/xa0/u2000/u2001/u2002/u2003/u2004/u2005/u2006/u2007/u2008/u2009/u200a/u200b/u2028/u2029/u2009/u200a/u200b/u2028/u2029/u310';
for (var i = 0, len = str.length; i <len; i ++) {
if (whiteSpace.indexof (str.charat (i)) === -1) {
str = str.substring (i);
quebrar;
}
}
for (i = str.Length-1; i> = 0; i--) {
if (whiteSpace.indexof (str.charat (i)) === -1) {
str = str.substring (0, i + 1);
quebrar;
}
}
Retornar WhiteSpace.IndexOf (str.Charat (0)) === -1? str: '';
}
função getfaviconurl (url) {
var Prohost;
prohost = url.match (/([^: //?#]+: ////)? ([^//@:]+)/i);
prohost = prohost? Prohost: [true, "http: //", document.location.hostname];
// complete o URL
if (! Prohost [1]) {
prohost [1] = "http: //";
}
// CRAWL OCO
retornar "http://www.google.com/s2/favicons?domain=" + prohost [1] + prohost [2];
}
submmit.OnClick = function () {
val = input.value;
if (! val) alerta ("a entrada está vazia!");
val = val.split ("");
val.foreach (função (item) {
Item = Trim (item);
if (! item) retornar;
resultado.innerhtml + = "<li>" + item + "<img src = '" + getfaviconurl (item) + "'> </li>";
});
};
</script>
</body>
</html>
Download do código -fonte