最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Chrome Extension: using document.querySelector in background script - Stack Overflow

programmeradmin2浏览0评论

I'm trying to set a variable equal to the favicon url of the current page. Are you not able to do this in the background script? How else could I get the current page's favicon url that the user is on?

background.js

var currentFaviconURL = document.querySelector("link[rel*='shortcut icon']").href;
console.log(currentFaviconURL);

console

Error in response to tabs.query: TypeError: Cannot read property 'href' of null

I'm trying to set a variable equal to the favicon url of the current page. Are you not able to do this in the background script? How else could I get the current page's favicon url that the user is on?

background.js

var currentFaviconURL = document.querySelector("link[rel*='shortcut icon']").href;
console.log(currentFaviconURL);

console

Error in response to tabs.query: TypeError: Cannot read property 'href' of null
Share asked Aug 16, 2018 at 22:00 Marcus MartinMarcus Martin 1191 gold badge1 silver badge9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

For reading and manipulating DOM, you must use Content Scripts. https://developer.chrome./extensions/content_scripts

发布评论

评论列表(0)

  1. 暂无评论