【发布时间】:2015-05-11 09:02:21
【问题描述】:
我正在尝试创建我的第一个 Chrome 扩展程序,但我的 JS 无法正常工作。 我想要做的是,当我点击“Activer”时,它会显示一个弹出窗口,上面写着“你好”。
这是我在 github 中找到的一段代码,我试图适应我的代码。 当我检查我的扩展时,我收到如下错误:
Unchecked runtime.lastError while running tabs.executeScript: Cannot access contents of url "http://stackoverflow.com/questions/ask". Extension manifest must request permission to access this host.
at HTMLDivElement.hello (chrome-extension://clolnlfhhjonbfknjgebnmnfanpmcono/popup.js:4:15)
这是我的 manifest.json
{
"name": "e-kootsa",
"version": "1.0",
"manifest_version": 2,
"description": "Ce plugin vous permet d'écouter le texte d'une page",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"options_page": "options.html"
}
这是我的 popup.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<style type="text/css">
body{
margin: 0px;
padding: 0px;
font-family: Arial, Sans-serif;
font-size: 20px;
width: 200px;
}
.selection{
text-align: center;
margin-bottom: 5px;
}
.global{
padding-top: 5px;
}
a{
text-decoration: none;
color: #000;
}
</style>
</head>
<body>
<div class="global">
<div class="selection"><a href="options.html" target="_blank">Paramètres</a></div>
<hr />
<div class="selection" id="clickme"><a href="#">Activer</a></div>
<hr />
<div class="selection"><a href="about.html" target="_blank">À propos</a> </div>
</div>
<script type="text/javascript" src="popup.js"></script>
</body>
</html>
这里是 popup.js
// var app = chrome.runtime.getBackgroundPage();
function hello() {
chrome.tabs.executeScript({
file: 'alert.js'
});
}
document.getElementById('clickme').addEventListener('click', hello);
这是我的 alert.js
alert('hello ' + document.location.href);
console.log('Tryhard');
我知道我一定犯了一些错误,但我仍然很难理解如何让事情发挥作用......
提前谢谢你!
【问题讨论】:
标签: javascript google-chrome google-chrome-extension popup