javascript - Chrome extension - message port closed before a response was received - Stack Overflow

I get this error in my simple chrome extension. I'm trying to pass some messages from background.j

I get this error in my simple chrome extension. I'm trying to pass some messages from background.js to my content script. The first message will be sent without problems, but the second one give me this error _generated_background_page.html:1 Unchecked runtime.lastError: The message port closed before a response was received.

I need to pass a response from the content script after the first message is recived, this because I need to remove event listener from webRequest.onCompleted otherwise I will get a loop problem with message passing. I'm not sure if I'm doing the things well also with removeListener function.

Here is my code, any help will be appreciated

contentScript.js

const m3u8 = new M3U8(); 
console.log(m3u8);
chrome.runtime.onMessage.addListener( message => {    
    console.log(message);
    chrome.runtime.sendMessage({status: 'ok'})
    const download = m3u8.start(message.url);
    download.on("progress", progress => {
        console.log(progress);
    }).on("finished", finished => {
        console.log(finished);
    }).on("error", error => {
        console.log(error);
    });
});

background.js

const sendURL = (request) => {
    chrome.tabs.sendMessage(request.tabId, {url: request.url}, response => {
        if( response.status === 'ok' ){
            chrome.webRequest.onCompleted.removeListener( sendURL )
        }
        return true;
    })
}

chrome.webRequest.onCompleted.addListener( request => {
    console.log(request);
    sendURL(request)
},{
    urls: ["https://*.myurl/*/*prog_index.m3u8*"],
    types: ["xmlhttprequest"]
},["responseHeaders"]);

I get this error in my simple chrome extension. I'm trying to pass some messages from background.js to my content script. The first message will be sent without problems, but the second one give me this error _generated_background_page.html:1 Unchecked runtime.lastError: The message port closed before a response was received.

I need to pass a response from the content script after the first message is recived, this because I need to remove event listener from webRequest.onCompleted otherwise I will get a loop problem with message passing. I'm not sure if I'm doing the things well also with removeListener function.

Here is my code, any help will be appreciated

contentScript.js

const m3u8 = new M3U8(); 
console.log(m3u8);
chrome.runtime.onMessage.addListener( message => {    
    console.log(message);
    chrome.runtime.sendMessage({status: 'ok'})
    const download = m3u8.start(message.url);
    download.on("progress", progress => {
        console.log(progress);
    }).on("finished", finished => {
        console.log(finished);
    }).on("error", error => {
        console.log(error);
    });
});

background.js

const sendURL = (request) => {
    chrome.tabs.sendMessage(request.tabId, {url: request.url}, response => {
        if( response.status === 'ok' ){
            chrome.webRequest.onCompleted.removeListener( sendURL )
        }
        return true;
    })
}

chrome.webRequest.onCompleted.addListener( request => {
    console.log(request);
    sendURL(request)
},{
    urls: ["https://*.myurl/*/*prog_index.m3u8*"],
    types: ["xmlhttprequest"]
},["responseHeaders"]);
Share Improve this question asked Aug 9, 2020 at 4:46 alfaun0alfaun0 2291 gold badge4 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You were sending a new message, not a response. And it failed because there was no corresponding onMessage in the background script.

Let's use the simple messaging example correctly.

  1. Add sendResponse parameter to onMessage:

    chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {  
    
  2. Use sendResponse instead of chrome.runtime.sendMessage

    chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {  
      sendResponse({status: 'ok'});
      //.........
    });
    

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743777947a4505576.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信