让我解释一下我的问题。我目前正在开发一个谷歌浏览器扩展,它在每个网页中注入一个工具栏作为 iframe。
问题是在某些情况下我需要隐藏工具栏,重新显示它等等。基本上我想把我的听众放在我的背景页面上,但它没有用,因为这个页面不能以图形方式操作对象。所以我的计划是把这个监听器放在一个 content_script 上(他可以以图形方式操作对象)。但是第二个问题是与背景页面相反的内容脚本不是一直执行,而是只执行一次。
所以我问自己是否有可能通过在其上放置一个循环或类似的东西来使内容脚本听起来像一个背景页面......
提前致谢。
我试过这个:
清单.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"
}
工具栏.html
<!-- Close Button -->
<a href="javascript:hideToolbar()"><input type="image" src="images/close.png" name="close" width="18" height="18"></a>
工具.js
function hideToolbar()
{
chrome.extension.sendRequest({action : "hideToolbar"});
window.webkitNotifications.createHTMLNotification('instantMessage.html', 'Ask Show Menu').show();
}
监听器.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]');
}
});
背景.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 没有阻止执行,他只是没有捕捉到任何东西......