我正在尝试构建一个Chrome扩展,它将在Deezer的相册页面上添加一个按钮。
我的舱单如下:
{
"manifest_version": 2,
"name": "Deezeet",
"description": ".",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["https://www.deezer.com/*", "http://www.deezer.com/*"],
"js": ["jquery.js", "popup.js"],
"run_at": "document_end"
}
],
"web_accessible_resources": ["script.js"],
"permissions": [
"tabs", "http://*/*", "https://*/*"
],
"content_security_policy": "script-src 'self' https://api.deezer.com; object-src 'self' https://api.deezer.com; script-src 'self'"
}
我的popup.js (来自here):
var s = document.createElement('script');
s.src = chrome.extension.getURL('script.js');
s.onload = function() {
this.parentNode.removeChild(this);
};
(document.head||document.documentElement).appendChild(s);
还有我的script.js (现在只用于测试):
$('header').hide();
但不起作用。我所有的JS都在弹出窗口里工作。我能做什么?
发布于 2014-06-25 14:04:27
你打开一个弹出窗口,看到弹出窗口中的效果:
..。即使我使用警告(‘bam’)或其他简单的东西,它都运行在我的弹出窗口中。
当您打开它时,它与任何其他页面无关。您已经将脚本设置为内容脚本。当您导航到页或刷新现有页时,它将执行。
如果您需要在单击“扩展”按钮时触发脚本,则应该从清单中删除default_popup
属性,添加一个后台脚本,并在其中添加一个单击处理程序:
chrome.browserAction.onClicked.addListener(function(tab){
chrome.tabs.executeScript(tab.id, {file: "popup.js"});
// Or, alternatively, send a message to an already-injected script
});
请注意:将脚本注入为<script>
元素是一种非常困难的解决方案,只需要摆脱孤立的上下文。在测试用例中,您只访问页面的DOM,而不需要popup.js
包装器。
https://stackoverflow.com/questions/24402731
复制相似问题