dev*_*v02 25 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.
Run Code Online (Sandbox Code Playgroud)
我的代码:(show_loader.js)
var dv = document.createElement('div');
dv.id = 'myid';
dv.innerHTML = 'test';
document.body.appendChild(dv);
Run Code Online (Sandbox Code Playgroud)
但是,当我把这个代码:
document.body.style.backgroundColor = 'green';
Run Code Online (Sandbox Code Playgroud)
它按预期工作,当前选项卡的背景颜色更改,除了show_loader.js文件中的代码之外没有其他任何更改,该文件从popup.js运行,如下所示:
chrome.tabs.executeScript(null, {file: "show_loader.js"});
Run Code Online (Sandbox Code Playgroud)
我的清单文件也有:
"permissions":
[
"tabs",
"notifications",
"http://*/",
"https://*/"
],
Run Code Online (Sandbox Code Playgroud)
所以我想知道为什么当我尝试做除设置背景颜色之外的任何其他事情时它会产生上述错误.即使在该页面上简单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"
}
}
Run Code Online (Sandbox Code Playgroud)
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();
});
};
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
show_loader.js
console.log($); // doesn't work
// document.body.style.backgroundColor = 'green'; // WORKS
Run Code Online (Sandbox Code Playgroud)
Sud*_*han 29
代码有效
{
"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://*/"
]
}
Run Code Online (Sandbox Code Playgroud)
<html>
<head>
<script src="back.js"></script>
</head>
<body>
<button id="tmp-clipboard">Click Me</button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
document.addEventListener("DOMContentLoaded", function () {
document.getElementById('tmp-clipboard').onclick = function () {
chrome.tabs.executeScript(null, {
file: "script.js"
});
}
});
Run Code Online (Sandbox Code Playgroud)
var dv = document.createElement('div');
dv.id = 'myid';
dv.innerHTML = 'test';
document.body.appendChild(dv);
Run Code Online (Sandbox Code Playgroud)
尝试chrome.tabs.getSelected从代码中删除已弃用的内容并使用chrome.tabs.query代替.
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
}
});
Run Code Online (Sandbox Code Playgroud)
如果您打算在他点击的当前窗口中捕获活动浏览选项卡,请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();
}
});
Run Code Online (Sandbox Code Playgroud)
清单 v3 使用不同的权限架构。这对我有用:
"host_permissions": [
"https://music.youtube.com/*"
],
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30322 次 |
| 最近记录: |