未经检查的runtime.lastError:无法建立连接。接收端不存在。使用消息传递API时

时间:2019-03-31 05:50:48

标签: javascript google-chrome-extension google-chrome-devtools

我正在尝试为Google Chrome构建密码管理器扩展程序。我希望我的后台脚本检查与不同网站的登录URL匹配的URL,并且当它匹配时,我想将用户的凭据发送到内容脚本,该脚本将自动填充用户名和密码字段。但是我收到“未经检查的runtime.lastError:无法建立连接。接收端不存在。”错误

我也看到过类似的问题,主要的答案是禁用其他扩展,然后重试。我禁用了所有扩展并尝试,但仍然收到相同的错误。我还看到长连接可以解决此错误,但是我不想使用长连接,因为这是一次性消息

background.js

print(user.profile.email)
print(user.profile.name)
print(user.profile.familyName)
print(user.profile.givenName)
print(user.profile.hasImage)

content_script.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");
            }
        });
    }
})

我希望内容脚本能够接收到消息,并在当前选项卡中使参数可用。因此,非常感谢有关如何解决此错误的任何帮助。 谢谢

0 个答案:

没有答案