Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Google Chrome扩展-与后台页面js文件通信的内容脚本_Javascript_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript Google Chrome扩展-与后台页面js文件通信的内容脚本

Javascript Google Chrome扩展-与后台页面js文件通信的内容脚本,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,在我的Google Chrome扩展中,我有一个内容脚本,我想与我的背景页面进行通信。有人知道如何做到这一点吗 我已经找到了一些教程,介绍了背景页面如何与内容脚本通信,但是,正如我所说的,我需要出现相反的情况 我想每24小时运行一个自动脚本来清除缓存。我不能从内容脚本中执行此操作,我必须从后台页面执行此操作。我现在唯一能让它正常工作的方法是,如果我系上一个“按钮”,但正如我之前所说,我希望它每24小时自动运行一次 顺便说一句,我已经知道内容脚本不能: 使用chrome.*api(chrome.e

在我的Google Chrome扩展中,我有一个内容脚本,我想与我的背景页面进行通信。有人知道如何做到这一点吗

我已经找到了一些教程,介绍了背景页面如何与内容脚本通信,但是,正如我所说的,我需要出现相反的情况

我想每24小时运行一个自动脚本来清除缓存。我不能从内容脚本中执行此操作,我必须从后台页面执行此操作。我现在唯一能让它正常工作的方法是,如果我系上一个“按钮”,但正如我之前所说,我希望它每24小时自动运行一次

顺便说一句,我已经知道内容脚本不能: 使用chrome.*api(chrome.extension部分除外) 使用由其扩展页定义的变量或函数 使用由网页或其他内容脚本定义的变量或函数

正如您所看到的,这里列出的第一项是:chromeapi,但是我需要在我的content_脚本中使用chromeapi,所以希望有人能解决这个问题。请告诉我。

内容脚本:

chrome.extension.sendRequest({greeting: "hello"}, function(response) { //request
  console.log(response.farewell);           //receive response
});
chrome.extension.onRequest.addListener(     //listen to requests
  function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
    if (request.greeting == "hello")
      sendResponse({farewell: "goodbye"});  //send response
  });
背景页面:

chrome.extension.sendRequest({greeting: "hello"}, function(response) { //request
  console.log(response.farewell);           //receive response
});
chrome.extension.onRequest.addListener(     //listen to requests
  function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
    if (request.greeting == "hello")
      sendResponse({farewell: "goodbye"});  //send response
  });

此外,您还可以在内容脚本和背景页面之间建立长期连接: