chrome扩展多个标签和chrome.runtime.connect

时间:2016-04-06 21:32:55

标签: javascript google-chrome google-chrome-extension

如果网址为test.com,我将从内容脚本中打开与后台长期连接。我正在使用chrome.runtime.connect。但是如果我用test.com打开多个标签,我会从后台脚本中获得多个onMessage调用。

问题是内容脚本不是与多个标签共享,而是端口如何?

1 个答案:

答案 0 :(得分:1)

这是设计使然,因为如果您没有调用self.myFunction = function () {console.log(JSON.stringfy(this.obj))}; $mdDialog.show({ controller: function ctrl(myfunction) { }, controllerAs: 'ctrl', templateUrl: 'views/modal_templates/dialog01.template.html', parent: angular.element(document.body), targetEvent: ev, clickOutsideToClose: true, locals: { myfunctiion: myFunction.bind(self), } }) ,则会保留以前的连接,每次使用runtime.Port.disconnect打开新标签页时,您的内容脚本都会被加载,开始建立另一个长期连接。

您可以在创建新标签时调用test.com来切断当前连接,或者您可以通过runtime.Port.disconnect区分不同的连接,您可以Port访问port.MessageSender ,其中包含runtime.onConnect.addListener(function(port){});作为发件人的唯一信息。