Javascript 未选中的runtime.lastError:无法建立连接。接收端不存在。使用消息传递API时
我正在尝试为Google Chrome构建一个密码管理器扩展。我希望我的背景脚本检查URL是否与不同网站的登录URL匹配,如果匹配,我希望将用户的凭据发送到内容脚本,内容脚本将自动填充用户名和密码字段。但是我得到了“Unchecked runtime.lastError:无法建立连接。接收端不存在。”错误 我见过类似的问题,主要的答案是禁用其他扩展并重试。我禁用了所有扩展并尝试了,但仍然得到相同的错误。我还看到长寿命连接解决了这个错误,但我不想使用长寿命连接,因为这是一条一次性消息 background.jsJavascript 未选中的runtime.lastError:无法建立连接。接收端不存在。使用消息传递API时,javascript,google-chrome-extension,google-chrome-devtools,Javascript,Google Chrome Extension,Google Chrome Devtools,我正在尝试为Google Chrome构建一个密码管理器扩展。我希望我的背景脚本检查URL是否与不同网站的登录URL匹配,如果匹配,我希望将用户的凭据发送到内容脚本,内容脚本将自动填充用户名和密码字段。但是我得到了“Unchecked runtime.lastError:无法建立连接。接收端不存在。”错误 我见过类似的问题,主要的答案是禁用其他扩展并重试。我禁用了所有扩展并尝试了,但仍然得到相同的错误。我还看到长寿命连接解决了这个错误,但我不想使用长寿命连接,因为这是一条一次性消息 backgr
/*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;
});
log(“内容脚本正在运行”);
chrome.runtime.onMessage.addListener(函数(请求、发送方、发送响应){
控制台日志(“收到的消息”);
console.log(request.args);
var args=request.args;
console.log('args',args);
var forms=document.getElementsByTagName(“表单”);
控制台日志(表格);
for(让表格的形式){
控制台日志(表格);
for(让index=0;index=0;reverseIndex--){
常量元素=form.elements[reverseIndex];
if(element.type==='text'){
element.value=args.username;
打破
}
}
}
否则{
继续;
}
}
}
发送响应(“成功”);
返回true;
});
我希望内容脚本接收消息并使参数在当前选项卡中可用。因此,任何关于如何修复此错误的帮助都是非常感谢的。
谢谢当发送邮件时,选项卡没有运行内容脚本时,就会发生这种情况。例如,您的内容脚本声明时没有“run_at”:“document_start”。或者您在上重新加载了扩展chrome://extensions 页面,未重新加载网页或未通过chrome.tabs.executeScript手动重新输入内容脚本。典型的解决方案是让内容脚本发送消息并在回调中处理响应。您的后台脚本将有一个onMessage侦听器,并将保持频道打开。谢谢。将内容脚本声明为“run_at”:.json中的“document_start”解决了此问题。当发送消息时,选项卡没有运行内容脚本时,会发生这种情况。例如,您的内容脚本声明时没有“run_at”:“document_start”。或者您在上重新加载了扩展chrome://extensions 页面,未重新加载网页或未通过chrome.tabs.executeScript手动重新输入内容脚本。典型的解决方案是让内容脚本发送消息并在回调中处理响应。您的后台脚本将有一个onMessage侦听器,并将保持频道打开。谢谢。将内容脚本声明为“run_at”:manifest.json中的“document_start”解决了此问题。