Transfer control from popup to content script - Google Chrome Extension
PRINT SCREEN to understand my problem : link
I'm building an extension which inserts a button into a specific web page's DOM. This insertion is achieved by the injectScript() function from insert.js which is correctly executed and correctly entered in the "content_scripts": field of the manifest file.
injectScript() injects (in the DOM of the currently loaded webpage, if it's, of course, the page I'm looking for) a script tag containing the same inject.js (that contains the onclick event for the button), a style tag containing the CSS for the button, and most importantly, a div that contains a text obtained from background.html by calling chrome.extension.sendRequest({greeting: "dami"}, function(response) {...}); the text inserted in this div is used to populate some other div from the webpage by clicking on the button which I inserted.
This works perfectly but I also need to solve another issue:
The specific text which I inject into the webpage's DOM is injected only when chrome.tabs.onSelectionChanged , chrome.tabs.onUpdated, and chrome.history.onVisited events happen. I also want to re-inject this text whenever my popup.html modifies it by changing localStorage["builtin"]. So what I want is this: when I click the OK button from the popup.html page I want to use chrome.extension.sendRequest({greeting: "reintrodu"}, function(response) {...}); from popup.html to send a request to the same content script that managed my first text injection, but it seems it doesn't work.
Nothing happens when I click the button! the test alert boxes don't show anything. When I press the OK button the popup.html just closes and no exchange of data seems to happen.
This is the function that is triggered by the OK button from my popup.html :
function closer()
{
if ($('input[name=check]').is(':checked'))
{
localStorage["builtin"] = document.getElementById("tkey_1").value;
localStorage["build"] = "true";
}
else
{
localStorage["builtin"] = document.getElementById("tkey_1").value;
localStorage["build"] = "false";
}
chrome.extension.sendRequest({greeting: "reintrodu"}, function(response)
{
alert(response.farewell);
});
window.close();
}
chrome.extension.sendRequest({greeting: "reintrodu"}, function(response) {...}) should send the request to the content script that is inject.js:
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
// console.log(sender.tab ? "from a content script:" + sender.tab.url : "from the extension");
if (request.greeting === "history")
{
console.log("registered in history");
sendResponse({farewell: "goodbye fr开发者_开发问答om history"});
PageShowHandler();
}
else if (request.greeting === "historyrem")
{
console.log("registered in historyrem");
sendResponse({farewell: "goodbye from historyrem"});
PageShowHandler();
}
else if (request.greeting === "exploit")
{
console.log("registered in exploit");
sendResponse({farewell: "goodbye from exploit"});
PageShowHandler();
}
else if (request.greeting === "reintrodu")
{
console.log("registered in reintrodu");
sendResponse({farewell: "goodbye from reintrodu"});
//PageShowHandler();
alert('prins reintrodu');
}
else if (request.greeting === "selected")
{
console.log("registered in selected");
sendResponse({farewell: "goodbye from selected"});
PageShowHandler();
}
else
sendResponse({}); // snub them.
});
And this is background.html which seems do to the job for the initial injection, unlike popup.html :
<html>
<head>
<script type="text/javascript">
//Fired when a URL is visited, providing the HistoryItem data for that URL.
chrome.tabs.onSelectionChanged.addListener(function(tabId, selectInfo)
{
chrome.tabs.sendRequest(tabId, {greeting: "selected"}, function(response) {
console.log(response.farewell);
});
});
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
{
chrome.tabs.sendRequest(tab.id, {greeting: "exploit"}, function(response) {
console.log(response.farewell);
});
});
chrome.history.onVisited.addListener(function(result)
{
//alert(result.url);
//chrome.tabs.sendRequest(null, {greeting: "history"}, function(response) {
// console.log(response.farewell);
//});
chrome.windows.getCurrent(function(w)
{
chrome.tabs.getSelected(w.id, function (tabul)
{
//alert(tabul.id);
chrome.tabs.sendRequest(tabul.id, {greeting: "history"}, function(response) {
console.log(response.farewell);
});
});
});
});
chrome.history.onVisitRemoved.addListener(function(removed)
{
//alert(result.url);
//chrome.tabs.sendRequest(null, {greeting: "historyrem function(response) {
// console.log(response.farewell);
//});
chrome.windows.getCurrent(function(w)
{
chrome.tabs.getSelected(w.id, function (tabul)
{
//alert(tabul.id);
chrome.tabs.sendRequest(tabul.id, {greeting: "historyrem"}, function(response) {
console.log(response.farewell);
});
});
});
});
chrome.extension.onRequest.addListener(function(request, sender, sendResponse)
{
if(request.greeting == "dami")
{
if(localStorage["build"] == "true")
{
sendResponse({farewell: localStorage["builtin"]});
}
else
{
sendResponse({farewell: "textul default"});
}
}
else
{
sendResponse({farewell: "n-am ba; du-te dracu!"});
}
});
</script>
</head>
<body></body>
</html>
This is the manifest:
{
"name" : "gMail Adder ",
"version" : "1.0",
"description" : "Google Chrome Gmail Adder",
"options_page": "options.html",
"background_page": "background.html",
"run_at": "document_start",
"permissions": [
"tabs",
"history",
"http://*/*",
"https://*/*"
],
"content_scripts": [
{
"matches": ["*://*.google.mail.com/*", "https://*.google.mail.com/*" ,"http://mail.google.com/*" ,"https://mail.google.com/*", "https://www.google.com/*", "http://www.google.com/*", "file:///*"],
"css": ["toggle.css"],
"js": ["jquery-1.4.4.min.js", "inject.js"]
}
],
"browser_action" : {
"default_icon" : "Quest Icon 11.png",
"default_popup": "popup.html"
}
}
You are closing a popup window before receiving a response from the request because API calls are asynchronous.
Also if you want to send a request to a content script you need to use chrome.tabs.sendRequest
instead.
So your popup should look like this:
function closer()
{
...
//todo: get tabid of a tab where the content script you are interested in is
chrome.tabs.sendRequest(tabId, {greeting: "reintrodu"}, function(response)
{
alert(response.farewell);
//closing only after receiving response
window.close();
});
}
精彩评论