Google chrome extension 在传递历史记录搜索的消息中发送响应

Google chrome extension 在传递历史记录搜索的消息中发送响应,google-chrome-extension,Google Chrome Extension,我一直试图在后台页面发送搜索历史记录,并将响应发送回首页。我正在使用消息解析。 在搜索历史记录后,我无法返回响应 --------background.js------------- )) } 问题就在这里,因为在日志记录中,我得到了“未定义”的响应。 有人能告诉我解决方案吗大多数Chrome.*API函数都是异步的,请尝试这样做: background.js var SECONDS_PER_WEEK = 1000 * 60 * 60 * 24 * 7; chrome.extension.on

我一直试图在后台页面发送搜索历史记录,并将响应发送回首页。我正在使用消息解析。 在搜索历史记录后,我无法返回响应

--------background.js-------------

))

}

问题就在这里,因为在日志记录中,我得到了“未定义”的响应。
有人能告诉我解决方案吗

大多数
Chrome.*
API函数都是异步的,请尝试这样做:

background.js

var SECONDS_PER_WEEK = 1000 * 60 * 60 * 24 * 7;

chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
  if(request === "getHistory"){
    getHistory(sendResponse);
    return true;
  } 
});

function getHistory(sendResponse){
  var current_time = new Date().getTime();
  var timeToFetch = current_time - SECONDS_PER_WEEK;
  chrome.history.search({
    'text' : '',
    'startTime' : timeToFetch
  },
  function(resp){
    console.log("Response: " + resp);
    sendResponse(resp);
  });
}
function getHistory(){
var current_time = new Date().getTime();
var timeToFetch = current_time - SECONDS_PER_WEEK;
return chrome.history.search({
        'text' : '',
        'startTime' : timeToFetch
    },
    function(resp){
        return resp;
});
var SECONDS_PER_WEEK = 1000 * 60 * 60 * 24 * 7;

chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
  if(request === "getHistory"){
    getHistory(sendResponse);
    return true;
  } 
});

function getHistory(sendResponse){
  var current_time = new Date().getTime();
  var timeToFetch = current_time - SECONDS_PER_WEEK;
  chrome.history.search({
    'text' : '',
    'startTime' : timeToFetch
  },
  function(resp){
    console.log("Response: " + resp);
    sendResponse(resp);
  });
}