Javascript 扩展清单必须请求访问此主机的权限
我正在尝试将div附加到当前活动选项卡的页面。然而,我得到了这个错误:Javascript 扩展清单必须请求访问此主机的权限,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,我正在尝试将div附加到当前活动选项卡的页面。然而,我得到了这个错误: Error during tabs.executeScript: Cannot access contents of url .... Extension manifest must request permission to access this host. 我的代码:(show_loader.js) 然而,当我把这个代码: document.body.style.backgroundColor = 'green'
Error during tabs.executeScript: Cannot access contents of url ....
Extension manifest must request permission to access this host.
我的代码:(show_loader.js)
然而,当我把这个代码:
document.body.style.backgroundColor = 'green';
它按预期工作,当前选项卡的背景颜色已更改,除show_loader.js文件中的代码外,没有其他更改,该文件从popup.js运行,如下所示:
chrome.tabs.executeScript(null, {file: "show_loader.js"});
我的清单文件也有:
"permissions":
[
"tabs",
"notifications",
"http://*/",
"https://*/"
],
所以我想知道,除了设置背景色之外,当我尝试做任何其他事情时,为什么会出现上述错误。即使是简单的alert
或console.log
页面也会出现上述错误
如何解决这个问题
更新:填写相关代码
舱单:
{
... name and description ...
"icons":
{
"16" : "img/icon.png",
"48" : "img/48.png",
"128" : "img/128.png"
},
"permissions":
[
"tabs",
"notifications",
"http://*/*",
"https://*/*"
],
"browser_action":
{
"default_icon": "img/icon.png",
"default_title": "Page title",
"default_popup": "popup.html"
}
}
popup.js
// send data to server for saving
$('#btn').click(function(){
chrome.tabs.executeScript(null, {file: "show_loader.js"});
$loader.show();
var data = $(this).closest('form').serialize();
$.ajax({.....});
});
window.onload = function(){
var $loader = $('#loader');
$loader.show();
chrome.tabs.getSelected(null, function(tab) {
//console.log(tab);
$('#url').val(tab.url);
$('#title').val(tab.title);
$loader.hide();
});
};
popup.html
<html>
<head>
<link rel="stylesheet" href="css/style.css" type="text/css" />
</head>
<body>
<form action="" method="post" name="frm" id="frm">
<table border="0" cellpadding="3" cellspecing="0" width="370">
......
</table>
</form>
<script src='js/jquery.js'></script>
<script src='popup.js?v=014423432423'></script>
</body>
</html>
有效的代码
manifest.json
popup.html
script.js
尝试从代码中删除不推荐的chrome.tabs.getSelected
,然后改用
样本使用
编辑1
如果您打算在当前窗口中捕获活动浏览选项卡,在该窗口中他单击了browser action
,请使用此代码
chrome.tabs.query({
"currentWindow": true,//Filters tabs in current window
"status": "complete", //The Page is completely loaded
"active": true // The tab or web page is browsed at this state,
"windowType": "normal" // Filters normal web pages, eliminates g-talk notifications etc
}, function (tabs) {//It returns an array
for (tab in tabs) {
$('#url').val(tabs[tab].url);
$('#title').val(tabs[tab].title);
$loader.hide();
}
});
manifest v3使用不同的方法。这就是我的工作原理:
“主机权限”:[
"https://music.youtube.com/*"
],
尝试将您的权限更改为“http://*/*”
和“https://*/*”
(注意尾随的星号)@rgthree:不幸的是,仍然是相同的错误:(我认为问题可能是其他的,因为背景颜色更改代码使用相同的清单权限。@dev02:在什么时间点,这个代码chrome.tabs.executeScript
在popup.js
中被调用?@Sudarshan:它是在popup.html文件中存在的一个按钮点击下被调用的。如果我更改背景,它会在同一位置工作。)我什么都不愿意else@dev02:我刚刚尝试了一个示例,它工作正常,您正在执行的页面是否处于加载状态?请共享您完整的相关代码,似乎该问题似乎是与此无关的另一个问题感谢您花时间提供帮助和回答。我将此标记为已接受并尝试,但只有一个问题。您将如何替换cchrome.tabs.getSelected(null,函数(tab){$('#url').val(tab.url);})
与选项卡。查询版本?@dev02:检查我的回答的编辑1谢谢我的提问,但我认为迭代选项卡将影响所有选项卡。再次感谢你的帮助:)看起来像是chrome说没有网络的页面,无论你在manifest.js中说了什么权限,都不能在上面运行代码
console.log($); // doesn't work
// document.body.style.backgroundColor = 'green'; // WORKS
{
"name": "Manifest Permissions",
"description": "http://stackoverflow.com/questions/14361061/extension-manifest-must-request-permission-to-access-this-host",
"version": "1",
"manifest_version": 2,
"browser_action": {
"default_popup": "popup.html"
},
"permissions": [
"tabs",
"notifications",
"http://*/",
"https://*/"
]
}
<html>
<head>
<script src="back.js"></script>
</head>
<body>
<button id="tmp-clipboard">Click Me</button>
</body>
</html>
document.addEventListener("DOMContentLoaded", function () {
document.getElementById('tmp-clipboard').onclick = function () {
chrome.tabs.executeScript(null, {
file: "script.js"
});
}
});
var dv = document.createElement('div');
dv.id = 'myid';
dv.innerHTML = 'test';
document.body.appendChild(dv);
chrome.tabs.query({
"currentWindow": true,
"status": true,
"active": true //Add any parameters you want
}, function (tabs) {//It returns an array
for (tab in tabs) {
//Do your stuff here
}
});
chrome.tabs.query({
"currentWindow": true,//Filters tabs in current window
"status": "complete", //The Page is completely loaded
"active": true // The tab or web page is browsed at this state,
"windowType": "normal" // Filters normal web pages, eliminates g-talk notifications etc
}, function (tabs) {//It returns an array
for (tab in tabs) {
$('#url').val(tabs[tab].url);
$('#title').val(tabs[tab].title);
$loader.hide();
}
});