讓我解釋我的問題。我目前正在開發一款谷歌瀏覽器擴展程序,它在每個網頁中都將工具欄作爲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