我正在尝试将 div 附加到当前活动选项卡的页面。但是我收到此错误:
Error during tabs.executeScript: Cannot access contents of url ....
Extension manifest must request permission to access this host.
我的代码:(show_loader.js)
var dv = document.createElement('div');
dv.id = 'myid';
dv.innerHTML = 'test';
document.body.appendChild(dv);
但是,当我输入此代码时:
document.body.style.backgroundColor = 'green';
它按预期工作,当前选项卡的背景颜色已更改,除了从 popup.js 运行的 show_loader.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>
show_loader.js
console.log($); // doesn't work
// document.body.style.backgroundColor = 'green'; // WORKS