简体   繁体   English

Google Chrome扩展程序 - content_scripts与background_page js文件进行通信

[英]Google Chrome Extension - content_scripts to communicate with background_page js files

In my Google Chrome extension, I have a content_script that I want to communicate with my background_page. 在我的Google Chrome扩展程序中,我有一个content_script,我想与我的background_page进行通信。 Does anyone know how this can be done? 有谁知道如何做到这一点?

I have found a couple tutorials going over how a background page can communicate with a content_script but, as I have stated, I need the opposite to occur. 我发现了一些关于后台页面如何与content_script通信的教程,但正如我所说的,我需要相反的事情发生。

I want to run an automated script to clear my cache every 24 hours. 我想运行一个自动脚本来每24小时清除一次缓存。 I cannot do this from a content_script, I have to do this from the background page. 我无法从content_script执行此操作,我必须从后台页面执行此操作。 The only way I can get this to work right now is if I tie in a "button" but as I said before, I want it automated to run every 24 hours. 我现在能够让它工作的唯一方法是,如果我打上一个“按钮”,但正如我之前所说,我希望它每24小时自动运行一次。

BTW- I am already aware that Content Scripts cannot: Use chrome.* APIs (except for parts of chrome.extension) Use variables or functions defined by their extension's pages Use variables or functions defined by web pages or by other content scripts 顺便说一句 - 我已经知道内容脚本不能:使用chrome。* API(chrome.extension的部分除外)使用由其扩展名页面定义的变量或函数使用由网页或其他内容脚本定义的变量或函数

As you can see the first item listed there is: chrome APIs, but I need to use a chrome API in my content_script, so hopefully someone has a work around. 正如您所看到的那样,列出的第一项是:chrome API,但我需要在我的content_script中使用chrome API,所以希望有人可以解决这个问题。 Please let me know. 请告诉我。

Message Passing Doc from Google 消息从Google传递文档

Content script: 内容脚本:

chrome.extension.sendRequest({greeting: "hello"}, function(response) { //request
  console.log(response.farewell);           //receive response
});

Background page: 背景页面:

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

Also you can open up a Long-lived connection between content scripts and background page: 您还可以打开内容脚本和后台页面之间的长期连接:
http://code.google.com/chrome/extensions/messaging.html#connect http://code.google.com/chrome/extensions/messaging.html#connect

contentscript.js
================
var port = chrome.extension.connect({name: "knockknock"});
port.postMessage({joke: "Knock knock"});
port.onMessage.addListener(function(msg) {
      if (msg.question == "Who's there?")
    port.postMessage({answer: "Madame"});
  else if (msg.question == "Madame who?")
    port.postMessage({answer: "Madame... Bovary"});
});


background.html
===============
chrome.extension.onConnect.addListener(function(port) {
  console.assert(port.name == "knockknock");
  port.onMessage.addListener(function(msg) {
    if (msg.joke == "Knock knock")
      port.postMessage({question: "Who's there?"});
    else if (msg.answer == "Madame")
      port.postMessage({question: "Madame who?"});
    else if (msg.answer == "Madame... Bovary")
      port.postMessage({question: "I don't get it."});
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM