Chrome扩展程序未选中runtime.lastError:无法建立连接。接收端不存在

时间:2019-10-17 05:04:01

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

我知道也有类似的问题,但是我还没有找到可以解决我问题的问题

基本上,我试图像这样从我的background.js脚本向content.js脚本发送一条消息

background.js

chrome.runtime.onInstalled.addListener(() => {

  // Listen for when a user is on correct site and enable the extension
  chrome.webNavigation.onCompleted.addListener(() => {
    chrome.tabs.query({ active: true, currentWindow: true }, ([{ id }]) => {
      chrome.pageAction.show(id);
    });
  }, { url: [{ urlMatches: 'website.com' }] });

  chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
    checkUrl(tab);
  });

  chrome.tabs.onActivated.addListener(() => {
    checkUrl();
  });

  chrome.tabs.onCreated.addListener((tab) => {
    checkUrl(tab);
  });
});

// Check to see if current url is either website or website1
const checkUrl = (tab = null) => {
  if (!tab) {
    chrome.tabs.query({ active: true, lastFocusedWindow: true }, (tabs) => {
      tab = tabs[0];
      if (tab.url.includes('website')) {
        getInfo(tab);
      } else if (tab.url.includes('website2')) {
        getOtherInfo();
      }
    });
  } else if (tab) {
    if (tab.url.includes('website')) {
      getInfo(tab);
    } else if (tab.url.includes('website2')) {
      getOtherInfo();
    }
  }
}

const getInfo = (tab) => {
  chrome.tabs.sendMessage(tab.id, { text: 'report_back' }, (result) => {
    console.log('report_back', result);
  });
}

const getOtherInfo = () => {

}

content.js

chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
  console.log(msg);
  if (msg.text === 'report_back') {
    setTimeout(() => {
      sendResponse(document);
    }, 10000);
  }
});

但是每次我导航到正确的页面时,在background.js控制台中都会遇到以下错误

  

未经检查的runtime.lastError:无法建立连接。接收端不存在。

而我的console.log是tabs undefined

我不确定自己在做什么错吗?

编辑

我现在正在执行与official messaging docs

中指定的完全相同的操作

background.js

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
    console.log(response.farewell);
  });
});

content.js

chrome.runtime.onMessage.addListener(
  function (request, sender, sendResponse) {
    console.log(sender.tab ?
      "from a content script:" + sender.tab.url :
      "from the extension");
    if (request.greeting == "hello") {
      sendResponse({ farewell: "goodbye" });
    }
    return true;
  });

并且它不起作用..我在做什么错了?

我什至尝试将sendResponse包装在setTimeout()中,但仍然没有变化

if (request.greeting == "hello") {
      setTimeout(() => {
        sendResponse({ farewell: "goodbye" });
      }, 10000);
      return true;
    }

编辑2

如果有错误,我已经尝试重新运行该功能

chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
    chrome.tabs.sendMessage(tabs[0].id, { greeting: "hello" }, function (response) {
      if (chrome.runtime.lastError) {
        console.log('error');
        setTimeout(getInfo, 1000);
      } else {
        console.log(response.farewell);
      }
    });
  });

我反复出错

enter image description here

任何帮助将不胜感激!! 谢谢

0 个答案:

没有答案