Chrome扩展程序:在后台脚本中使用document.querySelector [英] Chrome Extension: using document.querySelector in background script

查看:426
本文介绍了Chrome扩展程序:在后台脚本中使用document.querySelector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个变量设置为等于当前页面的收藏夹网址.您无法在后台脚本中执行此操作吗?我还能如何获取用户所在的当前页面的收藏夹网址?

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?

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

控制台

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

推荐答案

要阅读和操作DOM,必须使用内容脚本. https://developer.chrome.com/extensions/content_scripts

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

这篇关于Chrome扩展程序:在后台脚本中使用document.querySelector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆