2015-05-11 318 views
4

我试图创建我的第一个Chrome扩展程序,但我遇到了问题以使我的JS正常工作。 我想要做的就是当我点击“Activer”时,它会显示一个弹出窗口,显示“hello”。无法通过Chrome扩展程序弹出窗口创建JS弹出窗口

这是一个代码,我发现在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'); 

我知道我一定做了一些错误,但我仍然有困难,了解如何使事情工作...

预先感谢您!

回答

2

对于比较保守的解决方案看Xan's answer

======

它与激活的标签,所以你需要在你的清单中添加的许可,您想要的工作一切可能的URL =所有网址:"permissions": ["<all_urls>"]。所以你的清单看起来是这样的:

... 
"browser_action": { 
    "default_icon": "icon.png", 
    "default_popup": "popup.html" 
}, 
"permissions": ["<all_urls>"], 
"options_page": "options.html" 

只要添加它应该可以正常工作。

+2

看到我的答案是更保守的解决方案,万一你不知道这件事。 – Xan

+0

谢谢,不知道我是不是:) – Samurai

+0

该死的我喜欢这个社区,它现在就像一个魅力! – Jaeger

4

Samurai's answer有效,但达不到最佳效果。

如果您正在使用这样的活动选项卡,则不需要全部权限。声明<all_urls>将导致安装用户的可怕警告。

有一个special permission, "activeTab",专门为此目的。当用户调用扩展名时(例如按下按钮),它授予当前选项卡的权限。

"permissions": ["activeTab"], 
相关问题