javascript - 如何在 chrome 扩展中添加“读取和更改您访问的网站上的所有数据”权限?
问题描述
我需要Read and change all your data on the websites you visit
在我的 chrome 扩展中添加此权限,以便我可以读取DOM
页面的元素。
清单.json
{
"name": "BBox Counter",
"version": "2.0",
"manifest_version": 2,
"content_scripts": [
{
"match_about_blank": true,
"matches": ["<all_urls>"],
"js": ["content.js"]
}
],
"browser_action": {
"default_icon": "16.png",
"default_popup": "popup.html",
"default_title": "BBox Counter"
},
"permissions": [
"activeTab",
"contextMenus",
"storage"
],
"content_security_policy":"script-src 'self' https://www.gstatic.com/ https://*.firebaseio.com https://www.googleapis.com; object-src 'self'",
"background": {
"page": "background.html"
}
}
更新 content.js
console.log("context started...");
chrome.runtime.onMessage.addListener(gotMessage);
function gotMessage(message, sender, sendResponse) {
console.log("Message received");
const element_id = "event-capturer";
if (message == 'count') {
if (document.getElementById(element_id)) {
const cnt = document.getElementById(element_id).getElementsByTagName("rect").length;
alert("Count is: "+cnt);
} else {
alert("No element with '"+element_id+"' tag.");
}
}
}
该网站有一个div
带有 idevent-capturer
但我不知道为什么它会成为其他部分"No element with '"+element_id+"' tag."
。
请帮帮我。谢谢
解决方案
你做对了。
当您像现在所做的那样注入内容脚本时,您将获得正确的权限。
"content_scripts": [
{
"match_about_blank": true,
"matches": ["<all_urls>"],
"js": ["content.js"]
}
]
这意味着您注入content.js
到每个网页。尝试在 content.js 中编写一些代码alert("I'm here")
,然后重新加载扩展,然后打开任何页面,您都会收到警报。
如何查看权限状态?
- 右键单击工具栏中的扩展图标
- 悬停在
this can read and change site date
- 如果它
on all sites
默认选择了,那么它可以访问所有站点。
推荐阅读
- java - Maven 的“没有主要清单属性”错误
- chart.js - Chart.js - 使条形图元素闪烁
- python-3.x - Python 3:如何匹配 2 个声波信号
- json - 在 React 中递归渲染深层嵌套数据
- node.js - 赛普拉斯检索 JWToken 并设置为进一步请求的标头
- excel - 如何打开 Web 链接并将文件保存在同一文件夹中?
- android - 如何在android中成功加密和解密tensorflow模型
- html - 单击下拉导航栏时文本移动
- android - wix react-native-navigation 更改选项卡动画
- python - 无法从 Django 视图返回响应到前端