How to fix 'Unchecked runtime.lastError: The message port closed before a response was received' chrome issue?

前端 未结 17 1982
孤街浪徒
孤街浪徒 2020-12-07 08:43

I\'m using VueJS and Laravel for my project. This issue started to show lately and it shows even in the old git branches.

This error only shows in Chrome browser.

相关标签:
17条回答
  • 2020-12-07 09:32

    Post is rather old and not closely related to Chrome extensions development, but let it be here.

    I had same problem when responding on message in callback. The solution is to return true in background message listener.

    Here is simple example of background.js. It responses to any message from popup.js.

    chrome.runtime.onMessage.addListener(function(rq, sender, sendResponse) {
        // setTimeout to simulate any callback (even from storage.sync)
        setTimeout(function() {
            sendResponse({status: true});
        }, 1);
        // return true;  // uncomment this line to fix error
    });
    

    Here is popup.js, which sends message on popup. You'll get exceptions until you un-comment "return true" line in background.js file.

    document.addEventListener("DOMContentLoaded", () => {
        chrome.extension.sendMessage({action: "ping"}, function(resp) {
            console.log(JSON.stringify(resp));
        });
    });
    

    manifest.json, just in case :) Pay attention on alarm permissions section!

    {
      "name": "TestMessages",
      "version": "0.1.0",
      "manifest_version": 2,
      "browser_action": {
        "default_popup": "src/popup.html"
      },
      "background": {
        "scripts": ["src/background.js"],
        "persistent": false
      },
      "permissions": [
        "alarms"
      ]
    }
    
    0 讨论(0)
  • 2020-12-07 09:34

    This error is generally caused by one of your Chrome extensions.

    I recommend installing this One-Click Extension Disabler, I use it with the keyboard shortcut COMMAND (⌘) + SHIFT (⇧) + D — to quickly disable/enable all my extensions.

    Once the extensions are disabled this error message should go away.

    Peace! ✌️

    0 讨论(0)
  • For me it was Auto Tab Discard, which throws that error on pinned tabs. I created a bug report, https://github.com/rNeomy/auto-tab-discard/issues/101.

    0 讨论(0)
  • 2020-12-07 09:35

    I disabled Chrome extension "Coupons at Checkout" and this problem was solved.

    0 讨论(0)
  • 2020-12-07 09:37

    Disable if there is any anti-virus extension is installed on the browser. In my case, the anti-virus extension was the culprit.

    0 讨论(0)
提交回复
热议问题