让我解释我的问题。我目前正在开发一款谷歌浏览器扩展程序,它在每个网页中都将工具栏作为iframe插入。内容脚本中的监听器
问题是,我需要在某些情况下隐藏工具栏,重新显示它和类似的东西。基本上我想把我的听众放在我的背景页面上,但它没用,因为这个页面不能操纵对象的图形。所以我的计划是把这个听众放在一个content_script上(他可以操纵对象的图形)。但第二个问题是,与背景页面相反的内容脚本不是一直执行,而是只执行一次。
所以我问自己,如果可能的话,使内容脚本听起来像一个背景页,通过把一个环上或类似的东西...提前
感谢。
我已经试过这样:
的manifest.json
{
"background_page" : "background.html",
"browser_action" :
{
"default_icon" : "images/extension.png"
//"popup" : "activateToolbar.html"
},
"content_scripts":
[ {
"all_frames": true,
"css": ["css/yourtoolbar.css"],
"js": ["js/jquery.js", "js/yourtoolbar.js", "js/listener.js"],
"matches": ["http://*/*"],
"run_at": "document_end"
} ],
"permissions" : ["tabs", "unlimitedStorage", "http://*/*", "notifications"],
"name" : "YourToolbar",
"version" : "1.1",
"description" : "Make your own Toolbar"
}
toolbar.html
<!-- Close Button -->
<a href="javascript:hideToolbar()"><input type="image" src="images/close.png" name="close" width="18" height="18"></a>
Tool.js
function hideToolbar()
{
chrome.extension.sendRequest({action : "hideToolbar"});
window.webkitNotifications.createHTMLNotification('instantMessage.html', 'Ask Show Menu').show();
}
listener.js
个chrome.extension.onRequest.addListener(function(request, sender, sendResponse){
if(request.action)
{
$('body').remove();
console.log('Received Start');
alert(request.action);
console.log('Received End');
}
else
{
console.log('nothing');
alert('Not For Me [listener.js]');
}
});
background.js
chrome.extension.onRequest.addListener(function(request, sender, sendResponse)
{
if(request.newTab)
{
// Create a new Tab
chrome.tabs.create({url: request.newTab});
}
else if(request.newWindow)
{
// Create a new Window
chrome.windows.create({url: request.newWindow});
}
else if(request.action)
{
chrome.tabs.getAllInWindow(null, function(tabs) {
$.each(tabs, function() {
chrome.tabs.sendRequest(this.id, {"action":"hideToolbar"});
});
});
}
});
但问题是,的addListener没有阻止执行,他只是没抓住什么...
您能否请您说明您如何将请求发送至此内容脚本? – serg 2011-03-01 16:30:33