Передача долгоживущих соединений из popup.js в contentscript.js

Реализация данной затеи желательно с использованием manifest v2.

Реализация с manifest v3 есть по ссылки

Используется реализация долгоживущих сообщений. Отправка производится в contentscript.js из различных элементов расширения: popup.js и background.js.

После щелчка по кнопки находящейся в popup.js, в окне созданном в background.js через window.open, нужно доставить сообщение в contentscript.js.

При отправке сообщения по щелчку кнопки sendMessageButton в консоли popup ошибка: «Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.»

Manifest.json

{
  "name": "Проверка sms",
  "description": "test",
  "version": "1.0.0",
  "manifest_version": 2,
  
    "icons": {
    "48": "images/get_started48.png"
    },
  
  "background": {
    "scripts": ["background.js"]
  },

  "browser_action": {},
  "content_scripts": [
    {
      "matches": [
        "http://*/*",
        "https://*/*"
      ],
      "js": [
        "contentscript.js"
      ],

      "run_at": "document_end",
      "all_frames": true
    }
  ],
  
  "permissions": [
    "storage",
    "clipboardWrite",
    "idle",
    "tabs"
  ]
}

popup.js

const sendMessageButton = document.querySelector("#clearLS");
sendMessageButton.onclick = function() {
    
    chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
        
         let activeTab = tabs[0]
          const port = chrome.tabs.connect(
            activeTab.id,
            {name: "user"}
          );
        
            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"});
                });
    });
}

contentscript.js

chrome.runtime.onConnect.addListener(function(port) {
    
    console.assert(port.name == "user");    
        port.onMessage.addListener(function(msg) {
            if (msg.joke == "Knock knock") {
                console.log('Текст сообщения ', msg.joke)
              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."});
          });

})

Ответы (0 шт):