简体   繁体   English

如何使用 JavaScript 获取网站的图标?

[英]How do I Get a website's favicon using JavaScript?

I am wondering if it is possible to get a website's favicon by a URL with JavaScript .我想知道是否有可能通过 URL 和JavaScript获得网站的图标。

For example, I have the URL http://www.bbc.co.uk/ and I would like to get the path to the favicon described in <link rel="icon".../> meta tag - http://www.bbc.co.uk/favicon.ico .例如,我有 URL http://www.bbc.co.uk/我想获取<link rel="icon".../>元标记 - http://www.bbc.co.uk/favicon.ico

I have many URLs so that should not load every page and search for link tag I think.我有很多 URL,所以不应该加载每个页面并搜索我认为的link标记。

Any ideas?有任何想法吗?

Here are 2 working options, I tested over 100 urls and got different results which each option.这里有 2 个工作选项,我测试了 100 多个 url,每个选项都得到了不同的结果。 Please note, this solution is not JS , but JS may not be necessary.请注意,此解决方案不是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' />

Suddenly I found something called Google Shared Stuff that returns image with website's favicon by hostname:突然我发现了一个叫做Google Shared Stuff ,它通过主机名返回带有网站图标的图像:

http://www.google.com/s2/favicons?domain=www.domain.com

But fot BBC site it returns favicon a bit small.但是对于 BBC 网站,它返回的 favicon 有点小。 Compare:比较:

http://www.google.com/s2/favicons?domain=www.bbc.co.uk http://www.google.com/s2/favicons?domain=www.bbc.co.uk
http://www.bbc.co.uk/favicon.ico http://www.bbc.co.uk/favicon.ico

You could use YQL for that你可以使用 YQL

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

This query used by Display Feed Favicons Greasemonkey script .此查询由Display Feed Favicon Greasemonkey 脚本使用

You can write queries in YQL console, but it requires to login (btw, using queries don't):您可以在 YQL 控制台中编写查询,但它需要登录(顺便说一句,使用查询不需要):

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

It is better than http://www.google.com/s2/favicons?domain=www.domain.com , in case favicon exists, but doesn't located in domain.com/favicon.ico它比http://www.google.com/s2/favicons?domain=www.domain.com更好,以防 favicon 存在,但不位于 domain.com/favicon.ico

After 30.000 to 40.000 tests I noticed that you really encounter lots of different situations which have to be worked against.经过 30.000 到 40.000 次测试后,我注意到您确实遇到了许多必须应对的不同情况。

The starting point is ofcourse somewhere to only look at the rel tag in there and fetch this, but along the way you will find more and more situations you will have to cover.起点当然是在某个地方只查看其中的 rel 标签并获取它,但是在此过程中,您会发现越来越多的情况需要涵盖。

In case anyone will look at this thread and tries to come closer to 100% perfection I uploaded my (PHP) code here: https://plugins.svn.wordpress.org/wp-favicons/trunk/includes/server/class-http.php .万一有人会看这个帖子并试图接近 100% 完美,我在这里上传了我的(PHP)代码: https : //plugins.svn.wordpress.org/wp-favicons/trunk/includes/server/class- http.php This is part of a (GPL) WordPress Plugin that retrieves Favicons, more or less on request back then, out of limitations of the standard Google one (as mentioned above).这是 (GPL) WordPress 插件的一部分,该插件或多或少地根据当时的要求检索 Favicon,不受标准 Google 插件的限制(如上所述)。 The code finds a substantially amount more icons that the code of Google.该代码找到的图标数量比 Google 的代码多得多。 But also includes google and others as image providers to shortcut further iterations on trying to retrieve the icon.但也包括 google 和其他作为图像提供者的图像提供者,以便在尝试检索图标时进一步迭代。

When you read through the code you will probably see some situations that you will encounter eg base64 data uris, pages redirecting to 404 pages or redirecting a gazillion times, retrieving weird HTTP status codes and having to check every possible HTTP return code for validness, the icons themselves that have a wrong mime type, client side refresh tags, icons in the root folder and none in the html code, etc... etc... etc...当你通读代码时,你可能会看到一些你会遇到的情况,例如 base64 数据 uri、页面重定向到 404 页面或重定向无数次、检索奇怪的 HTTP 状态代码以及必须检查每个可能的 HTTP 返回代码的有效性,图标本身具有错误的 MIME 类型、客户端刷新标签、根文件夹中的图标以及 html 代码中没有的图标等等...等等...等等...

If you go up a directory you will find other classes that then are ment to store the actual icons against their url (and ofcourse you will then need to find out which "branches" use the same favicon and which not, and find out if they belong to the same "owner" or are really different parts but under the same domain.如果你上一个目录,你会发现其他类然后将实际图标存储在它们的 url 中(当然,你需要找出哪些“分支”使用相同的图标,哪些不使用,并找出它们是否属于同一个“所有者”或者是真正不同的部分但在同一个域下。

These days I thought that GitHub's service did a much better job than Google's:这些天我认为 GitHub 的服务比 Google 的服务做得好得多:

https://favicons.githubusercontent.com/microsoft.com https://favicons.githubusercontent.com/microsoft.com

Though neither are perfect it seems.虽然两者都不完美,但看起来。 For stackoverflow:对于堆栈溢出:

For GitHub:对于 GitHub:

Here is an article I wrote about a solution that can fetch favicons from multiple source.这是我写的一篇关于可以从多个来源获取网站图标的解决方案的文章

Here is the source code:这是源代码:

<!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>

Disclaimer: I built this service myself, but you can try http://grabicon.com .免责声明:我自己构建了此服务,但您可以尝试http://grabicon.com It's a very simple API for favicons that handles resizing and reformatting on the fly.这是一个非常简单的网站图标 API,可以即时处理调整大小和重新格式化。 It uses a lot of proactive caching for speed, as well.它也使用大量主动缓存来提高速度。 Like most other services like this, you can insert this directly into your HTML and it works great.像大多数其他类似的服务一样,您可以将其直接插入到您的 HTML 中,并且效果很好。 I don't have online documentation just yet, but here are some examples using everybody's favorite web destination:我还没有在线文档,但这里有一些使用每个人最喜欢的网络目的地的示例:

Get the icon in PNG format:获取PNG格式的图标:

http://grabicon.com/icon?domain=microsoft.com

Resize to 64 pixels square:调整大小为 64 像素正方形:

http://grabicon.com/icon?domain=microsoft.com&size=64 http://grabicon.com/icon?domain=microsoft.com&size=64

This hunts down the icon using the various methods people can implement them, and uses the highest resolution version available, since favicon.ico files actually contain a collection of icons.使用各种方法的人这查获的图标可以实现它们,并使用可用的最高分辨率版本,因为favicon.ico的文件实际上包含的图标集合 It uses that version to resize to your preference, unless a perfect match is already available.它使用该版本根据您的喜好调整大小,除非已经有完美匹配。

Let me know what you guys think, and what other features you might like!让我知道你们的想法,以及你可能喜欢的其他功能!

UPDATE:更新:

Grabicon is now back in operation, and it's not going anywhere. Grabicon 现在已恢复运行,而且不会有任何进展。 It was originally built for a customer who later decided not to support it.它最初是为后来决定不支持它的客户构建的。 But now it's under my direct care, with much better speed and resizing capability.但现在它在我的直接照顾下,具有更好的速度和调整大小的能力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM