最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What caused the "Unchecked runtime.lastError: The message port closed before a response was received.&

programmeradmin5浏览0评论

I have searched around but it's all about people complaining the bug. Many posts say that you should check all your extensions.

However, this is something I encountered when I am developing an extension.

Here is how it happens:

I have a listener on background.js:

chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
  console.log('get:', request);
  if (request.hasOwnProperty('opt')) {
    trackPage('opt/' + request.opt);
  }
  return Promise.resolve("");
});

And here is the trigger in my option page:

track('something');
function track(msg){
  chrome.runtime.sendMessage({opt: msg}, function(response) {
    console.log(response);
  });
}

The error occurs when the track function is fired.

How can I fix the error totally?

I have searched around but it's all about people complaining the bug. Many posts say that you should check all your extensions.

However, this is something I encountered when I am developing an extension.

Here is how it happens:

I have a listener on background.js:

chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
  console.log('get:', request);
  if (request.hasOwnProperty('opt')) {
    trackPage('opt/' + request.opt);
  }
  return Promise.resolve("");
});

And here is the trigger in my option page:

track('something');
function track(msg){
  chrome.runtime.sendMessage({opt: msg}, function(response) {
    console.log(response);
  });
}

The error occurs when the track function is fired.

How can I fix the error totally?

Share Improve this question edited Mar 22, 2022 at 12:13 ted 14.7k10 gold badges68 silver badges113 bronze badges asked Mar 18, 2019 at 15:20 darkrose1977darkrose1977 3071 gold badge3 silver badges12 bronze badges 5
  • Chrome extensions API doesn't support Promises. Either use the API the way it's shown in the documentation or switch to the modern way with Mozilla's WebExtension polyfill. – woxxom Commented Mar 19, 2019 at 4:53
  • Would you please share more information about the two ways? Thanks! – darkrose1977 Commented Mar 19, 2019 at 5:35
  • I thought you would google up the keywords I gave. The documentation. The polyfill. – woxxom Commented Mar 19, 2019 at 5:38
  • Did you find a fix for this? – Patrick Clancey Commented Oct 18, 2019 at 15:55
  • not yet. just give up – darkrose1977 Commented Oct 22, 2019 at 4:14
Add a comment  | 

6 Answers 6

Reset to default 7

You can't return a Promise to make the function async, you have to return true. So change this:

return Promise.resolve("");

To this:

Promise.resolve("").then(result => sendResponse(result));
return true;

I'm not sure why all the "it's Chrome extensions" responses are down-graded. I turned off all extensions; problem gone. Tried different browsers (even Microsoft); no problem. Back to Chrome and turned one extension back on; problem returned. Tried with a simple web page: <?php ; (yes, nothing) problem persists.

So, fuggedaboudit. Or, quoting Bill Murray in "Meatballs," "It just doesn't matter!"

In my case I just replaced the old chrome apis which were inside a Promise with updated ones:

Old:

  sendMessageToTab: (tabId, msg) => {
    return new Promise(resolve => global.chrome.tabs.sendMessage(tabId, msg, resolve));
  },

The updated ones:

sendMessageToTab: (tabId, msg) => global.chrome.tabs.sendMessage(tabId, msg)

If you see this error on other websites too then don't be bothered because it's not generated by your app, probably some Chrome Extension.

I disabled Tampermonkey Chrome extension and the error disappeared.

check extentions chrome, update or remove or deactive

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论