1

在 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;  
});
4

1 回答 1

2

适用于您的代码,我使用消息而不是请求。

关注Rob W帖子(link1link2)以获取有关 sendMessage() 与 sendRequest() 的更多信息

示例代码和输出

来自 background.js 的输出

在此处输入图像描述

内容脚本 scripts.js 的输出

在此处输入图像描述

清单.json

{
"name":"Local Storage",
"description":"Local Storage Demo",
"manifest_version":2,
"background":{
    "scripts":["background.js"]
},
"content_scripts": [
    {
      "matches": ["https://www.google.co.in/*"],
      "js": ["scripts.js"]
    }
  ],
"permissions":[
    "tabs","<all_urls>"
],

"version":"1"
}

背景.js

function storeData(){
    localStorage["domain"] = "google";  //Save the site to local storage for retrieval later when requested by the content script
    localStorage["page"] = "Google";   //Save the page title to local storage for retrieval later
    localStorage["url"] = "https://www.google.co.in/";  //Save the URL of the current page to local storage for retrieval
}
chrome.extension.onMessage.addListener(
  function(request, sender, sendResponse) 
  {
  console.log("request recieved is "+request);
  if (request.name == "url")
  {
    console.log("sending response for request URL"+ localStorage["url"]);
  sendResponse({url: localStorage["url"]});
  }
  else
  {
  console.log("sending response for request URL"+   localStorage["page"]);
  sendResponse({domain: localStorage["domain"] + ": " + localStorage["page"]});
  }
}
);
window.onload = function(){
    storeData();
}

脚本.js

function requestBackground(){
    chrome.extension.sendMessage({name:"domain"}, 
    function(response)
    {
    console.log("response recived for response.domain  "+response.domain);   
    });

chrome.extension.sendMessage({name:"url"},
    function(response)
    {
    console.log("response recived for last page visited: " + response.url);  
    });
}

window.onload = function(){
    requestBackground();
}

让我知道它是否仍然失败。

于 2012-11-30T06:25:44.203 回答