我想知道是否可以使用 JavaScript
通过 URL 获取网站的 favicon。
例如,我有一个 URL:http://www.bbc.co.uk/
,我想获取在 <link rel="icon" .../>
meta 标签中描述的 favicon 路径 - http://www.bbc.co.uk/favicon.ico
。
我有很多 URL,所以不应该加载每个页面并搜索 link
标签。
有什么想法吗?
我想知道是否可以使用 JavaScript
通过 URL 获取网站的 favicon。
例如,我有一个 URL:http://www.bbc.co.uk/
,我想获取在 <link rel="icon" .../>
meta 标签中描述的 favicon 路径 - http://www.bbc.co.uk/favicon.ico
。
我有很多 URL,所以不应该加载每个页面并搜索 link
标签。
有什么想法吗?
这里有两个可行的选项,我测试了100多个URL并得到了每个选项不同的结果。请注意,这个解决方案并不使用JS
,但也许没有必要使用JS
。
<!-- Free -->
<img height="16" width="16" src='http://www.google.com/s2/favicons?domain=www.edocuments.co.uk' />
<!-- Paid -->
<img height="16" width="16" src='http://grabicon.com/edocuments.co.uk' />
突然发现了名为Google Shared Stuff
的东西,它能按主机名返回带有网站 favicon 的图片:
http://www.google.com/s2/favicons?domain=www.domain.com
但对于BBC网站,它返回的favicon有点小。请比较:
http://www.google.com/s2/favicons?domain=www.bbc.co.uk
http://www.bbc.co.uk/favicon.ico
http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D"http://bbc.co.uk/"and%20xpath%3D"/html/head/link[@rel%3D'icon']%20|%20/html/head/link[@rel%3D'ICON']%20|%20/html/head/link[@rel%3D'shortcut%20icon']%20|%20/html/head/link[@rel%3D'SHORTCUT%20ICON']"&format=json&callback=grab这个查询语法被 Display Feed Favicons Greasemonkey script 使用。
http://developer.yahoo.com/yql/console/#h=select%20*%20from%20html%20where%20url%3D%22http%3A//bbc.co.uk/%22and%20xpath%3D%22/html/head/link%5B@rel%3D%27icon%27%5D%20%7C%20/html/head/link%5B@rel%3D%27ICON%27%5D%20%7C%20/html/head/link%5B@rel%3D%27shortcut%20icon%27%5D%20%7C%20/html/head/link%5B@rel%3D%27SHORTCUT%20ICON%27%5D%22它比
http://www.google.com/s2/favicons?domain=www.domain.com
更好,因为在图标存在但不在 domain.com/favicon.ico 中时也可以正常工作。经过3万到4万次测试,我发现你真的会遇到许多不同的情况需要解决。
当然,起点只是查看其中的rel标签并提取它,但沿途你会发现越来越多的情况需要覆盖。
如果有人看到这个帖子并试图接近100%的完美,我在这里上传了我的(PHP)代码:https://plugins.svn.wordpress.org/wp-favicons/trunk/includes/server/class-http.php。这是一个(GPL)WordPress插件的一部分,可以根据请求检索Favicons,更多或更少地超出了标准Google的限制(如上所述)。该代码找到了比Google代码更多的图标。但也包括Google和其他作为图像提供者,以便缩短尝试检索图标的进一步迭代。
当你阅读代码时,你可能会遇到一些情况,例如base64数据URI、页面重定向到404页面或重定向多次、检索奇怪的HTTP状态码并需要检查每个可能的HTTP返回代码的有效性、图标本身具有错误的MIME类型、客户端刷新标记、在根文件夹中的图标以及在HTML代码中没有图标等等。最近我认为GitHub的服务比Google做得好多了:
https://favicons.githubusercontent.com/microsoft.com
虽然两者都不完美。
适用于GitHub:
这是我写的一篇文章,介绍了一个可以从多个来源获取网站图标的解决方案。
以下是源代码:
<!DOCTYPE html>
<html>
<body style="background-color:grey;">
<script type="text/javascript">
const KRequestFaviconGitHub = 'https://favicons.githubusercontent.com/';
const KRequestFaviconGoogle = 'https://www.google.com/s2/favicons?domain=';
const KDefaultUrl = KRequestFaviconGoogle;
// We rely on pre-defined hostname configurations
const hostnames = {
"stackoverflow.com": { url:KRequestFaviconGoogle+"stackoverflow.com", invert:0 },
"theregister.co.uk": { url:KRequestFaviconGoogle+"theregister.co.uk", invert:1 },
"github.com": { url:KRequestFaviconGitHub+"github.com", invert:1 },
"android.googlesource.com": { url:KRequestFaviconGoogle+"googlesource.com", invert:0 },
"developer.android.com": { url:KRequestFaviconGitHub+"developer.android.com", invert:0 }
};
document.addEventListener('DOMContentLoaded', function(event) {
addFavicon("stackoverflow.com");
addFavicon("bbc.co.uk");
addFavicon("github.com");
addFavicon("theregister.co.uk");
addFavicon("developer.android.com");
addFavicon("android-doc.github.io");
addFavicon("slions.net");
addFavicon("alternate.de");
addFavicon("amazon.de");
addFavicon("microsoft.com");
addFavicon("apple.com");
addFavicon("googlesource.com");
addFavicon("android.googlesource.com");
addFavicon("firebase.google.com");
addFavicon("play.google.com");
addFavicon("google.com");
addFavicon("team-mediaportal.com");
addFavicon("caseking.de");
addFavicon("developer.mozilla.org");
addFavicon("theguardian.com");
addFavicon("niche-beauty.com");
addFavicon("octobre-editions.com");
addFavicon("dw.com");
addFavicon("douglas.com");
addFavicon("douglas.de");
addFavicon("www.sncf.fr");
addFavicon("paris.fr");
addFavicon("bahn.de");
addFavicon("hopfully.that.domain.does.not.exists.nowaythisisavaliddomain.fart");
});
/**
*
*/
function addFavicon(aDomain)
{
var a = document.createElement("a");
a.href = "http://" + aDomain;
//a.style.display = "block";
var div = document.createElement("div");
div.innerText = aDomain;
div.style.verticalAlign = "middle";
div.style.display = "inline-block";
var img = document.createElement("img");
img.className = "link-favicon";
img.style.width = "16px";
img.style.height = "16px";
img.style.verticalAlign = "middle";
img.style.display = "inline-block";
img.style.marginRight = "4px";
a.prepend(img);
a.appendChild(div);
document.body.appendChild(a);
document.body.appendChild(document.createElement("p"));
const conf = hostnames[aDomain]
if (conf==null)
{
img.src = KDefaultUrl+aDomain;
}
else
{
img.src = conf.url;
img.style.filter = "invert(" + conf.invert + ")";
}
}
</script>
</body>
</html>