我可以将消息从页面直接发布到扩展程序的后台脚本,而无需使用chrome中的内容脚本吗?

时间:2017-12-24 12:24:31

标签: google-chrome-extension

我创建了一个简单的chrome扩展,为我的webPage(网站)添加了一些功能,只能通过扩展程序访问。是否可以直接从网页(通过char nameLed[256]; //Nom de la led char colorLed[256]; //La couleur de la led char I_directLed[100]; //L'intensité direct que peut supporter la led double U_directLed; //La tension direct que peut supporter la led char commentLed[256]; //Le commentaire sur la led char chaineSaveLed[1000];//Chaine concaténé GetCtrlVal(panel, TABPANEL_1_ST_Name, nameLed); //Panel > Tab Champ GetCtrlVal(panel, TABPANEL_1_ST_Color, colorLed); //Panel > Tab Champ GetCtrlVal(panel, TABPANEL_1_I_Direct_Led, &I_directLed); //Panel > Tab Champ GetCtrlVal(panel, TABPANEL_1_U_Led_Direct, &U_directLed); //Panel > Tab Champ GetCtrlVal(panel, TABPANEL_1_TXT_Comment, commentLed); //Panel > Tab Champ //Créer la chaine à enregistrer au format CSV //Concaténation de chaine strcat (chaineSaveLed, nameLed); strcat (chaineSaveLed, ","); strcat (chaineSaveLed, colorLed); strcat (chaineSaveLed, ","); strcat (chaineSaveLed, I_directLed); strcat (chaineSaveLed, ","); strcat (chaineSaveLed, U_directLed); strcat (chaineSaveLed, ","); strcat (chaineSaveLed, commentLed); strcat (chaineSaveLed, "/n"); printf ("%s\n", chaineSaveLed); )发布消息到扩展程序的后台脚本。如果不是,content-script的用例是什么,我的意思是为什么可以从网页访问它?

代码bellow表示我在页面和扩展程序之间的通信方式。它不起作用,chrome.runtime.connect(EXTENSION_ID);不会在myExtension中触发,onConnect.addListener不会在myExtension或我的网页中触发。

Here is the playground

enter image description here

port.onMessage
我的扩展程序中的

background.js

var port = chrome.runtime.connect("ppibnonicgkeojloifobdloaiajedhgg"); // this is extensionId I got from chrome://extension
port.onMessage.addListener(function (event) { 
    console.log(event);
});
port.postMessage({type: 'PYCHAT_SCREEN_SHARE_PING', text: 'start'});

的manifest.json

chrome.runtime.onConnect.addListener(function(port) {
  console.log("Connected from new port ", port);
  port.onMessage.addListener(function(msg) {
    console.log("Got new message ", msg);
    port.postMessage({type: "PYCHAT_SCREEN_SHARE_PING_RESPONSE", data: "successs"});
  });
});

1 个答案:

答案 0 :(得分:0)

感谢@wOxxOm @rsanchez我应该:

  • 在background.js
  • 中使用chrome.runtime.onMessageExternal
  • externally_connectable添加到manifest.json

https://localhost:8000

var port = chrome.runtime.connect("ppibnonicgkeojloifobdloaiajedhgg"); 
port.onMessage.addListener(function (event) { 
    console.log(event);
});
port.postMessage({type: 'PYCHAT_SCREEN_SHARE_PING', text: 'start'});

<强>的manifest.json

{
  "name": "test",
  "description": "test",
  "version": "1.0.0",
  "manifest_version": 2,
  "background": {
    "scripts": ["background.js"]
  },
  "permissions": [
    "desktopCapture",
    "tabs"
  ],
  "externally_connectable": {
    "matches": ["https://localhost:8000"] 
  }
}

<强> background.js

chrome.runtime.onConnectExternal.addListener(function(port) {
  port.onMessage.addListener(function(msg) {
    console.log("it works")
    port.postMessage({type: "PYCHAT_SCREEN_SHARE_PING_RESPONSE", data: "successs"});
  });
});