Google chrome extension Can';t通过content.js和background.js进行通信,在我禁用扩展后,再在chrome扩展中启用它

Google chrome extension Can';t通过content.js和background.js进行通信,在我禁用扩展后,再在chrome扩展中启用它,google-chrome-extension,Google Chrome Extension,我想做的是 Popup.html chrome.runtime.sendMessage({greeting: "hello"}, function (response) { // console.log('Data to Call', response, dialingNumber); if (response == undefined || response == null) { window.confir

我想做的是

Popup.html

    chrome.runtime.sendMessage({greeting: "hello"}, function (response) {
            // console.log('Data to Call', response, dialingNumber);
            if (response == undefined || response == null) {
                window.confirm('Please LogIn...!');
            }
            else {
// ....
}  
chrome.runtime.onMessage.addListener(
        function (request, sender, sendResponse) {
            var credentials = localStorage.getItem('voi-auth').split("|");
            console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
            if (request.greeting == "hello")
                sendResponse({farewell: credentials});
        });  
在popup.html中,我创建了一个登录页面,在用户单击登录按钮后,我成功地在本地存储中登录,我存储了我的令牌和用户名

content.js

    chrome.runtime.sendMessage({greeting: "hello"}, function (response) {
            // console.log('Data to Call', response, dialingNumber);
            if (response == undefined || response == null) {
                window.confirm('Please LogIn...!');
            }
            else {
// ....
}  
chrome.runtime.onMessage.addListener(
        function (request, sender, sendResponse) {
            var credentials = localStorage.getItem('voi-auth').split("|");
            console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
            if (request.greeting == "hello")
                sendResponse({farewell: credentials});
        });  
background.js

    chrome.runtime.sendMessage({greeting: "hello"}, function (response) {
            // console.log('Data to Call', response, dialingNumber);
            if (response == undefined || response == null) {
                window.confirm('Please LogIn...!');
            }
            else {
// ....
}  
chrome.runtime.onMessage.addListener(
        function (request, sender, sendResponse) {
            var credentials = localStorage.getItem('voi-auth').split("|");
            console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
            if (request.greeting == "hello")
                sendResponse({farewell: credentials});
        });  
在我第一次安装时,它工作正常,能够从内容脚本调用存储在本地存储中的凭据

问题

    chrome.runtime.sendMessage({greeting: "hello"}, function (response) {
            // console.log('Data to Call', response, dialingNumber);
            if (response == undefined || response == null) {
                window.confirm('Please LogIn...!');
            }
            else {
// ....
}  
chrome.runtime.onMessage.addListener(
        function (request, sender, sendResponse) {
            var credentials = localStorage.getItem('voi-auth').split("|");
            console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
            if (request.greeting == "hello")
                sendResponse({farewell: credentials});
        });  
我的问题是,如果我转到扩展并禁用扩展,然后再次启用它,我无法在内容和后台脚本之间通信,当我调用发送消息函数时,内容脚本中未定义响应,即使本地存储具有令牌和用户名。可悲的是,只有当我检查background.js并一直打开它时,它才能正常工作。当我关闭弹出窗口时,它将不起作用


是否有任何解决方案可以避免这种情况,比如像这样执行后台页面?

这是Chrome中的一个已知问题,您可以在StackOverflow()上找到几个答案。简而言之,在后台页面中使用chrome.tabs.executeScript显式地重新输入内容脚本。@wOxxOm,当我禁用/启用我的extension@wOxxOm,我的意思是,你确定你提供的代码会工作吗?无论如何,这并不能解决我的问题。@wOxxOm,实际上,我的问题是background.js而不是content.jsI太懒了,无法让您相信根本的问题是一样的,所以希望其他人能在这里提供帮助。