查找收藏夹图标 - 当不在默认位置时

发布于 2024-12-21 14:44:30 字数 712 浏览 2 评论 0 原文

我在我的页面上显示其他网站的图标。

大约一半的时间他们在这里:

hostname.com/favicon.ico

但另一半他们不在这里。例如,在我自己的网站中,我链接到我的 .ico 文件,如下所示。 FAVICON 只是路径的 PHP 定义。

<link rel="SHORTCUT ICON" href="<?php echo FAVICON ?>" />

如何使用 html 中的链接获取网站图标的 URL?

这是网站,你可以像这样进行谷歌搜索在其中输入您需要其网站图标的域。

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

这是一种解决方案,但似乎比从路径读取 html 效率低。

我认为谷歌将“所有”图标缓存为.png格式并使它们可搜索 -

根据此 网站

I display favicon's from other sites on my page.

About half the time they are here:

hostname.com/favicon.ico

But the other half they are not. For ecample in my own site I link to my .ico file like this. FAVICON is just a PHP definition of the path.

<link rel="SHORTCUT ICON" href="<?php echo FAVICON ?>" />

How do I get the URL of a site's favicon using the the link in the html?

This is site sais you can do a google search like this where you enter the domain you need the favicon for.

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

Which is one solution but seems less efficient than just reading the html from the path.

I think google cached "ALL" icons into .png format and made them searchable -

per this site

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放血 2024-12-28 14:44:30

使用 Ajax 和代理页面加载页面。对于 Ajax:

// Create a request object:
var rq = new XMLHttpRequest(); // Not IE6-compatible, by the way.

// Set up the request:
rq.open('GET', 'proxy.php?url=' + encodeURIComponent(thePageURL), true);

// Handle when it's loaded:
rq.onreadystatechange = function() {
    if(rq.readyState === 4) {
        // The request is complete:
        if(rq.status < 400) {
            // The HTML is stored in rq.responseText; you could use a regular expression to extract the favicon, like /shortcut icon.+?href="(.+?)"/i.
        } else {
            // There was an error fetching the page; fall back?
        }
    }
};

以及代理页面(您可能需要添加一些安全性):

<?php
echo file_get_contents($_REQUEST['url']);
?>

Google“Ajax”,您会发现很多有关如何执行此类操作的信息。

您需要代理页面的原因是,浏览器不允许来自 JavaScript 的 Ajax 请求跨域,除非目标允许,而目标必须明确这样做。这是出于安全原因,因为 JavaScript 可能会恶意冒充用户。因此,您可以使用服务器端脚本代理内容并避免此类问题。

Load the page using Ajax and a proxy page. For the Ajax:

// Create a request object:
var rq = new XMLHttpRequest(); // Not IE6-compatible, by the way.

// Set up the request:
rq.open('GET', 'proxy.php?url=' + encodeURIComponent(thePageURL), true);

// Handle when it's loaded:
rq.onreadystatechange = function() {
    if(rq.readyState === 4) {
        // The request is complete:
        if(rq.status < 400) {
            // The HTML is stored in rq.responseText; you could use a regular expression to extract the favicon, like /shortcut icon.+?href="(.+?)"/i.
        } else {
            // There was an error fetching the page; fall back?
        }
    }
};

And the proxy page (you'll probably want to add some security):

<?php
echo file_get_contents($_REQUEST['url']);
?>

Google "Ajax" and you'll find lots of information on how to do that sort of thing.

The reason you need to proxy the page is that browsers don't allow Ajax requests from JavaScript to go across domains unless the target allows it, which it must do explicitly. This is for security reasons, since the JavaScript could be maliciously impersonating the user. So instead, you proxy the content using a server-side script and avoid such problems.

天荒地未老 2024-12-28 14:44:30

解析 HTML 很麻烦 - 你可能想使用这样的库: http:// www.controlstyle.com/articles/programming/text/php-favicon/ 或让 google 为您完成:http://www.google.com/s2/favicons?domain=domain(效率更高 - 您不必解析服务器上的所有 HTML,它只是其中一个标签)。如果您想在您的服务器上使用类似谷歌的功能,请查看上面的链接。

Parsing HTML is nasty - you probably want to use a library like: http://www.controlstyle.com/articles/programming/text/php-favicon/ or let google do it for you: http://www.google.com/s2/favicons?domain=domain (much more efficient - you don't have to parse all the HTML on your server, and it's just one tag). If you want something like google's functionality on your server, check out the link above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文