Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.

237 views
Skip to first unread message

Colouredfunk

unread,
Mar 25, 2020, 3:16:48 PM3/25/20
to Chromium Extensions
I want to be able to check if "content_scripts.js" has been injected into the tab (It's for scenarios upon first install, where users might have existing tabs open and want to use the Extension, which appears as an iFrame inside the DOM)

I've got it working fine, and detecting it using messaging, but I can't seem to stop an error being produced 

"Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist."

Here is my code:

background.js
chrome.tabs.onActivated.addListener(function (activeInfo, something) {
  chrome.tabs.sendMessage(activeInfo.tabId, {mode: "check"}, function (response) {
    if (response) {
      console.log("Already there");
      injected = true;
    } else {
      injected = false;
      console.log("Not there, inject contentscript");
    }
  });
});


chrome.runtime.onInstalled.addListener((details) => {
  const currentVersion = chrome.runtime.getManifest().version
  const previousVersion = details.previousVersion
  const reason = details.reason
  Notification.requestPermission();
  console.log('Previous Version: ${previousVersion }')
  console.log('Current Version: ${currentVersion }')
  chrome.browserAction.setPopup({popup: "index.html"});


  switch (reason) {
    case 'install':
      console.log('New User installed the extension.')

      break;
    case 'update':
      console.log('User has updated their extension.')
      break;
    case 'chrome_update':
    case 'shared_module_update':
    default:
      console.log('Other install events within the browser')
      break;
  }
})


content_script.js
chrome.runtime.onMessage.addListener(
  function (request, sender, sendResponse) {
    if (request.mode == "check") {
      sendResponse({message: "hi"});
    }
    if (request.mode == "toggle") {
      toggle();
    }
  });


manifest.json
{
  "name": "xx",
  "version": "1.0.10",
  "short_name": "xx",
  "description": "xx",
  "author": "xx",
  "permissions": [
    "notifications",
    "tabs"
  ],
  "icons": { "16": "assets/icons/icon16.png",
    "48": "assets/icons/icon48.png",
    "128": "assets/icons/icon128.png" },
  "background": {
    "scripts":["background.js"],
    "persistent": true
  },
  "content_scripts": [{
    "run_at": "document_end",
    "matches": [
      "https://*/*",
      "http://*/*"
    ],
    "js": ["content_script.js"]
  }],
  "browser_action": {
    "default_title": "xx",
    "default_icon": "xx"
  },
  "web_accessible_resources": [
    "index.html"
  ],
  "manifest_version": 2,
  "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
}



Can anyone see where I'm going wrong?

Bohdan Dvorianov

unread,
Mar 26, 2020, 7:38:15 AM3/26/20
to Chromium Extensions
Maybe, you have a new empty tab or chrome://* tab opened at that time, so it can be from there. It's okay. You won't have access for those hosts, and, I'm almost sure, you won't ever need it.
If all works fine, but you only see this error in background page console, that's it. You can just ignore it.

Check it and let me know please. If it won't do, we'll see :)

середа, 25 березня 2020 р. 21:16:48 UTC+2 користувач Colouredfunk написав:

Colouredfunk

unread,
Mar 26, 2020, 7:49:27 AM3/26/20
to Chromium Extensions
Yes it all works fine, but I don't like the idea of the Extension producing loads of errors behind the scene. 

If I restart, close tabs, refresh, the error always appears...

wOxxOm

unread,
Mar 26, 2020, 10:08:46 AM3/26/20
to Chromium Extensions
You should also explicitly check chrome.runtime.lastError in your messaging callback:

if (response && !chrome.runtime.lastError) {
...

Colouredfunk

unread,
Mar 26, 2020, 10:28:37 AM3/26/20
to Chromium Extensions
Same issue. It produces the error even if I just do

chrome.tabs.sendMessage(activeInfo.tabId, {mode: 'check'}, response => {
      
});

wOxxOm

unread,
Mar 26, 2020, 10:30:29 AM3/26/20
to Chromium Extensions
Your code doesn't check lastError so it gets spammed into console.
You need to check it explicitly:

chrome.tabs.sendMessage(activeInfo.tabId, {mode: 'check'}, response => {
   if (!chrome.runtime.lastError) {
   ........
   }   
});

Colouredfunk

unread,
Mar 26, 2020, 11:59:41 AM3/26/20
to Chromium Extensions
Sorry I meant to say I've tried that first. 

wOxxOm

unread,
Mar 26, 2020, 12:05:08 PM3/26/20
to Chromium Extensions
Well, since programming is an exact thing the only explanation for this (except a bug in Chrome) is that the code that sends the message doesn't check chrome.runtime.lastError. This can happen if you didn't reload the background script after editing. Or maybe your code sends the message from a different place where there's no check. Or maybe you're seeing an old error on chrome://extensions page (it doesn't clear them automatically). Or maybe you're loading the background script also in another page (the usual mistake is to include it in browser_action popup). Anyway, this is as far as I can help without debugging the actual extension.
Reply all
Reply to author
Forward
0 new messages