简体   繁体   English

如何在不看到 CORS 错误的情况下从网站 URL 获取图标?

[英]How to get a favicon from a website URL without seeing a CORS error?

Does anyone know how to get a favicon from a website URL without seeing a CORS error?有谁知道如何在不看到 CORS 错误的情况下从网站 URL 获取图标?

I'm implementing a simple chrome extension that shows a list of my bookmarks.我正在实现一个简单的 chrome 扩展,显示我的书签列表。 As I wanted to display a bookmark favicon in my extension popup, I wrote a code below.因为我想在我的扩展弹出窗口中显示一个书签图标,所以我在下面写了一个代码。

const httpRequest = new XMLHttpRequest();
httpRequest.open(
  "GET",
  "http://www.google.com/s2/favicons?domain=stackoverflow.com",
  false
);
httpRequest.send();

But I got a CORS error:但我收到了一个 CORS 错误:

popup.js:55 Access to XMLHttpRequest at 'https://www.google.com/s2/favicons?
domain=stackoverflow.com' (redirected from 'http://www.google.com/s2/favicons?
domain=stackoverflow.com') from origin 'chrome-extension://jompmceilagkakejfedancdallnhmfog' has 
been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested 
resource.

Thanks to @AD7six who gave me a comment, my issue is resolved!感谢@AD7six 给了我评论,我的问题已经解决了!

Based on the comment, I changed to "https", and used the tag as below.根据评论,我改为“https”,并使用如下标签。

const img = $("<img>");
img.attr("src", `https://www.google.com/s2/favicons?domain=stackoverflow.com`);

Now my extension can show the favicons!现在我的扩展程序可以显示网站图标了! Thank you so much.太感谢了。

Screenshot of the extension扩展程序的屏幕截图

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

相关问题 如何从具有 AJAX 请求的网站获取 HTML 页面而不出现 CORS 错误? - How do I get HTML page from a website with AJAX request without getting CORS error? 如何在不分析页面代码的情况下获取网站的收藏夹? - How to get favicon of a website without analyzing page code? 如何从这些站点正确获取简单数据而没有CORS错误 - How to GET simple data from these sites properly without CORS error 流星:使用HTTP.get从客户端访问网站(CORS错误) - Meteor: Accessing a website from the client with HTTP.get (CORS error) 从外部URL获取URL标题,而无需加载整个网站 - Get URL Title from External URL without loading whole Website 如何使用 JavaScript 获取网站的图标? - How do I Get a website's favicon using JavaScript? 如何从动态网站获取完整网址 - How to get the fully url from a dynamic website 如何在新标签页中打开链接而不在新标签页中看到 URL? - How to open a link in a new tab without seeing the URL in new tab? 如何在 javascript 或 jquery 中获取当前选项卡的图标 url - How to get favicon url of current tab in javascript or jquery 通过XMLHttpRequest从文件:// URL发送GET到本地主机时出现CORS错误 - CORS error when sending GET to localhost from file:// URL via XMLHttpRequest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM