Let me explain my issue. I'm currently developing an Google Chrome Extension which inject a toolbar as an iframe in every web page.
The problem is that i need in some case to hide the toolbar, re-display it and things like that. Basicelly i was thinking to put my listener on my background-page, but it's useless because this page can't manipulate graphicely the object. So my plan was to put this listener on a content_script (who can manipulate graphiquely the objet). But the second problem is that a content-script in opposite to a background-page is not executed all the time but only once.
So i'm asking myself if it's possible to make a content-script sounds like a background-page, by putting a loop on it or something like that...
Thanks in advance.
I've tried this :
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(req开发者_运维知识库uest.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"} );
});
});
}
});
But the problem is that the addListener didn't block the execution and he just didn't catch anything...
To send a request from a background page to a content script you need to use chrome.tabs.sendRequest
(not chrome.extension.sendRequest
) and provide tab id.
In a content script you don't need to periodically create chrome.extension.onRequest.addListener
, just create it once and it will be there permanently.
EDIT
To send a request from a content script you need to run chrome.extension.sendRequest
there and add chrome.extension.onRequest.addListener
to a background page.
精彩评论