Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.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扩展之间进行通信?_Javascript_Google Chrome_Google Chrome Extension_Localhost_Communication - Fatal编程技术网

Javascript 如何在两个不同浏览器实例中启动的两个google chrome扩展之间进行通信?

Javascript 如何在两个不同浏览器实例中启动的两个google chrome扩展之间进行通信?,javascript,google-chrome,google-chrome-extension,localhost,communication,Javascript,Google Chrome,Google Chrome Extension,Localhost,Communication,我需要使用google chrome浏览器的两个不同实例,然后在各自实例中的两个不同扩展之间建立通信 为了拥有不同的google chrome浏览器实例,我使用两种不同的快捷方式启动了chrome的可执行文件。以下是一个例子: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --user-data-dir="C:\Users\User\Desktop\temp1" "C:\Program Files (x86)\Goo

我需要使用google chrome浏览器的两个不同实例,然后在各自实例中的两个不同扩展之间建立通信

为了拥有不同的google chrome浏览器实例,我使用两种不同的快捷方式启动了chrome的可执行文件。以下是一个例子:

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
--user-data-dir="C:\Users\User\Desktop\temp1"

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
--user-data-dir="C:\Users\User\Desktop\temp2"
出于测试目的,我编写了两个扩展。 第一个是接收者,第二个是发送者。请检查以下源代码

第一次延期:

/* Extension1/manifest.json */ 

{
  "manifest_version": 2,
  "name": "Extension 1 receiver test",
  "description": "This is the main extension",
  "version": "1.0",
  "background": {
    "scripts": ["background.js"]
  }
} 


/* Extension1/background.js */ 

// request listener:
chrome.runtime.onMessageExternal.addListener(
  function(request, sender, sendResponse) {
    if (request.testPassed) {
      console.log('Test passed: '+request.testPassed);
    }
  });
/* Extension2/manifest.json */ 

{
  "manifest_version": 2,
  "name": "Extension 2 sender test",
  "description": "This is the secondary extension",
  "version": "1.0",
  "background": {
    "scripts": ["background.js"]
  }
}


/* Extension2/background.js */ 

// The ID of the 1st extension.
var laserExtensionId = "dnclcmbindlbmocdmjdeiecjffbobljc";

// Make a simple request:
chrome.runtime.sendMessage(laserExtensionId, {testPassed: true});
第二次延期:

/* Extension1/manifest.json */ 

{
  "manifest_version": 2,
  "name": "Extension 1 receiver test",
  "description": "This is the main extension",
  "version": "1.0",
  "background": {
    "scripts": ["background.js"]
  }
} 


/* Extension1/background.js */ 

// request listener:
chrome.runtime.onMessageExternal.addListener(
  function(request, sender, sendResponse) {
    if (request.testPassed) {
      console.log('Test passed: '+request.testPassed);
    }
  });
/* Extension2/manifest.json */ 

{
  "manifest_version": 2,
  "name": "Extension 2 sender test",
  "description": "This is the secondary extension",
  "version": "1.0",
  "background": {
    "scripts": ["background.js"]
  }
}


/* Extension2/background.js */ 

// The ID of the 1st extension.
var laserExtensionId = "dnclcmbindlbmocdmjdeiecjffbobljc";

// Make a simple request:
chrome.runtime.sendMessage(laserExtensionId, {testPassed: true});
如果两个扩展加载在同一浏览器中,但在浏览器的两个不同实例中不加载,则上述代码可以工作

注意:我已尝试将“external_connectable”行添加到清单文件以允许外部通信,但这些参数似乎都没有产生任何效果:

"externally_connectable": {
  "ids": ["*"]
}

"externally_connectable": {
  "matches": ["chrome-extension://insermyextensionidhereplease/*"]
}

"externally_connectable": {
  "matches": ["*://127.0.0.1/*"]
}

有人知道如何做到这一点吗?

你必须编写一个中介实用程序并使用nativeMessaging API。你能给我一些更多的线索吗?我对这种类型的应用程序没有任何经验。回答你的问题:“不,没有人知道如何使你试图做这项工作的方式,因为它不会那样工作。”