在 background.js 中,我将一些数据存储到本地存储中:
localStorage["domain"] = site; //Save the site to local storage for retrieval later when requested by the content script
localStorage["page"] = tab.title; //Save the page title to local storage for retrieval later
localStorage["url"] = tab.url; //Save the URL of the current page to local storage for retrieval
后来,我的内容脚本请求数据
chrome.extension.sendRequest({name:"domain"},
function(response)
{
subjectStr = response.domain;
});
chrome.extension.sendRequest({name:"url"},
function(response)
{
bodyStr = "URL of last page visited: " + response.url;
});
和 background.js 响应
//Wait for request for the site value and URL from content script
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse)
{
if (request.name == "url")
{
sendResponse({url: localStorage["url"]});
}
else
{
sendResponse({domain: localStorage["domain"] + ": " + localStorage["page"]});
}
}
);
但是,内容脚本永远不会接收到数据。有人知道为什么吗?
这是清单:
{
"name": "Test",
"version": "1.0",
"manifest_version": 2,
"description": "Test extension",
"browser_action": {
"default_icon": "no_msgs.png",
"default_title": "Press here to test."
},
"background": {
"scripts": ["background.js"]
},
"content_scripts": [{
"run_at": "document_end",
"js": ["postMsg.js"],
"matches": ["https://groups.google.com/forum/*"]
}],
"permissions": ["tabs",
"http://groups.google.com/forum/?fromgroups=#!forum/opencomments-site-discussions/*",
"https://groups.google.com/forum/?fromgroups=#!forum/opencomments-site-discussions/*"
]
}
和 no_msgs.png:
no_msgs http://www.opencomments.com/no_msgs.png
和 background.js:
var post_url = "https://groups.google.com/forum/?fromgroups=#!newtopic/opencomments-site-discussions";
chrome.browserAction.onClicked.addListener(function(main) {
});
function createEvent(tab){
}
function updateEvent(tabId, changeInfo, tab){
}
function miscEvent(tabId, eventInfo){
}
function getURL() {
chrome.tabs.getSelected(undefined, function(tab) {
var tmp = tab.url;
var site;
if (tab.url.indexOf("http://") == 0 || tab.url.indexOf("https://") == 0) {
site = getDomain(tab.url);
chrome.tabs.create({url: post_url});
localStorage["domain"] = site; //Save the site to local storage for retrieval later when requested by the content script
localStorage["page"] = tab.title; //Save the page title to local storage for retrieval later
localStorage["url"] = tab.url; //Save the URL of the current page to local storage for retrieval
}
});
}
//Wait for request for the site value and URL from content script
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse)
{
if (request.name == "url")
{
sendResponse({url: localStorage["url"]});
}
else
{
sendResponse({domain: localStorage["domain"] + ": " + localStorage["page"]});
}
}
);
//Fetches the domain from the URL
function getDomain(url){
var tmp = url.substring(url.indexOf("//") + 2);
var tmp2 = tmp.indexOf("/");
var str = tmp.substring(0, tmp2);
var index = str.indexOf(".");
while ((tmp = str.substring(index + 1)).indexOf(".") != -1){
str = str.substring(index + 1);
index = str.indexOf(".");
}
index = str.indexOf(".");
return str;
}
// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
getURL();
});
最后是 postMsg.js:
var subjectStr = '';
var bodyStr = '';
chrome.extension.sendRequest({name:"domain"},
function(response) {
subjectStr = response.domain;
});
chrome.extension.sendRequest({name:"url"},
function(response) {
bodyStr = "URL of last page visited: " + response.url;
});