M
M
MikMik2019-08-01 01:02:24
Google Chrome
MikMik, 2019-08-01 01:02:24

How to send messages to an additional page?

Tell me how to exchange messages with the extension page. I don't seem to be doing that. Messages come and the response is not, throws out the error Unchecked runtime.lastError: The message port closed before a response was received.
on the background

chrome.runtime.onMessage.addListener(
    function (request, sender, sendResponse) {
        if (request.command == "getEventList") {
            getEventList().then((eventList) => {
                sendResponse({res: 'eventList'});
            });
        }
    }
)


function getEventList(e) {
    return new Promise((resolve)=>{
        var eventList = new Array();
        db.transaction([eventStore], "readonly").objectStore(eventStore).openCursor().onsuccess = function (e) {
            var cursor = e.target.result;
            if (cursor) {
                eventList.push(cursor.value);
                cursor.continue();
            }
            resolve(eventList);
        }
    })
}

On the page
document.addEventListener("DOMContentLoaded", function () {
    chrome.runtime.sendMessage({command: "getEventList"}, function(response) {
        console.log(response);
    });
})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tioffs, 2019-08-01
@MikMik

Detailed description of messaging: https://habr.com/en/company/waves/blog/451796/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question