0
votes

I am working on a Chrome extension. I have a content script and an event page. From the content script, I send a message using chrome.runtime.sendMessage() to the event page. On the event page, I use onMessage event listener to send back a reponse -- however, I would like to send this reponse AFTER chrome has detected that a file has started downloading.

contentScript.js

window.location.href = download_link; //redirecting to download a file    

chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
    console.log(response.farewell);
});

eventPage.js

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {

    chrome.downloads.onCreated.addListener(function(DownloadItem downloadItem) {
        sendResponse({farewell: "goodbye"});

        return true;
    });

});

Now, I haven't tried chrome.downloads.onCreated listener before, but I'm assuming this is the correct syntax. However, the code is not working, and the console is returning this error:

Error in event handler for (unknown): Cannot read property 'farewell' of undefined
Stack trace: TypeError: Cannot read property 'farewell' of undefined
    at chrome-extension://dlkbhmbjncfpnmfgmpbmdfjocjbflmbj/ytmp3.js:59:31
    at disconnectListener (extensions::messaging:335:9)
    at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
    at EventImpl.dispatchToListener (extensions::event_bindings:395:22)
    at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
    at Event.publicClass.(anonymous function) [as dispatchToListener] (extensions::utils:65:26)
    at EventImpl.dispatch_ (extensions::event_bindings:378:35)
    at EventImpl.dispatch (extensions::event_bindings:401:17)
    at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
    at Event.publicClass.(anonymous function) [as dispatch] (extensions::utils:65:26)

I have tried this without the chrome.downloads.onCreated listener and it works, the response is fetched by the content script. I read online that you need to add return true; in order to make it work, but it's not working for me. I suspect it's because of the second event listener, which enters a new scope meaning that sendResponse cannot be called from there -- if that's the case, how do I call the sendResponse function?

1
Why are you defining the chrome.downloads.onCreated listener inside of the chrome.runtime.onMessage listener? - berrberr
I don't know how else to use it -- I'm new to chrome extension event pages, so I'm not sure how to do this properly. How else can I call the chrome.download.onCreated from the content script when needed? - Prid

1 Answers

0
votes

You return true; from the wrong place.

The purpose of that return is to say "I have not called sendResponse yet, but I'm going to".

However, you are returning it from inside the onCreated callback - it is too late by then, as right after the listener is added your original onMessage handler terminates. You just have to move the line:

chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) {
    chrome.downloads.onCreated.addListener( function(DownloadItem downloadItem) {
        sendResponse({farewell: "goodbye"});
    });
    return true;
});