1

I am trying to build a password manager extension for Google Chrome. I want my background script to check for URLs matching with the login URLs of different websites and when it does match, I want to send the credentials of the user to the content script which will autofill the username and password fields. But I am getting the "Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist." error

I have seen similar questions and the major answer was to disable the other extensions and try again. I disabled all the extensions and tried, but still getting the same error. I also saw that long-lived connections solve this error, but I don't want to use a long-lived connection as this is a one time message

background.js

/*Listen for changes in URL */
chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab){

    if(changeInfo.url){
        console.log(changeInfo.url);
        chrome.storage.local.get(null,function(ans){
            console.log("local storage",ans);
         });
        chrome.storage.local.get(['loggedIn'],function(answer){
            console.log('logged in answer is',answer);
            console.log('logged in answer is',answer.loggedIn);
            if(answer.loggedIn===true){

                console.log("user logged in")
                /* Check whether the user is logged in and the url is in the user credential urls*/
                chrome.storage.local.get(['urls'],function(result){
                    console.log("stored urls",result.urls,"current url",changeInfo.url);
                    if(result.urls.includes(changeInfo.url)){
                        console.log("matching url");
                        console.log("matching url",changeInfo.url);
                        var urlIndex = result.urls.indexOf(changeInfo.url);
                        console.log('index',urlIndex);



                       console.log("main tab id is",tabId)
                       console.log("tab is",tab);

                       chrome.storage.local.get(['credentials'],function(result){
                            console.log(result);
                            var username = result.credentials[urlIndex].username;
                            var password = result.credentials[urlIndex].password;
                            console.log('username',username,password)

                            var msg = {
                                username : username,
                                password : password
                            }

                            /* Get the credentials for that site and send it to the content script autoFill.js */    

                            chrome.tabs.sendMessage(tabId,{args: msg},function(response) {
                                console.log(tabId,tab.url);
                                console.log(response);
                            });
                        });


                    }
                });
            }
            else{
                console.log("user not logged in");
            }
        });
    }
})

content_script.js


console.log("content script is running");

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    console.log("message recieved");
    console.log(request.args);
    var args=request.args;
    console.log('args',args);


    var forms = document.getElementsByTagName("form");
    console.log(forms);

    for(let form of forms){
        console.log(form);
        for (let index = 0; index < form.length; index++) {
            const element = form.elements[index];
            //console.log(element);
            if(element.type==='password'){
                element.value=args.password;
                console.log(element.value);
                for (let reverseIndex = index-1; reverseIndex >= 0; reverseIndex--) {
                    const element = form.elements[reverseIndex];
                    if(element.type==='text'){
                        element.value=args.username;
                        break;
                    }
                }
            }
            else{
                continue;
            }
        }
    }
    sendResponse("success");
    return true;
});

I am expecting the content script to receive the message and make the parameters available in the current tab. So any help about how to fix this error is much appreciated. Thanks

  • It happens when a tab didn't have the content script running at the time the message was sent. For example your content script is declared without "run_at": "document_start". Or you reloaded the extension on chrome://extensions page and didn't reload the web page or didn't reinject the content scripts manually via chrome.tabs.executeScript. A typical solution is to make the content script send a message and process the response in the callback. Your background script will have an onMessage listener and will [return true](https://stackoverflow.com/a/20077854) to keep the channel open. – wOxxOm Mar 31 '19 at 06:12
  • Thanks.Declaring the content script to "run_at": "document_start" in manifest.json solved the issue. – Kalana Dananjaya Apr 01 '19 at 08:53

0 Answers0