0

I recieve the following error sometimes:

Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist.

popup.js

const sendStartSignal = document.getElementById("start-timer");
sendStartSignal.onclick = async function (e)
{
    ...

    // Get the current tab
    let tab = await getCurrentTab()
    // only send a message if you watching a youtube video
    if(typeof tab !== 'undefined' && tab.url.includes("youtube.com/watch")){
        const response = await chrome.tabs.sendMessage(
            tab.id,
            { workTime: workTime,
              restTime: restTime, }
        );
    }
    else{
        console.error("No youtube video on tab.")
    }
};// ERROR SHOWS IT HAPPENING ON THIS LINE

async function getCurrentTab() {
    let queryOptions = { active: true, lastFocusedWindow: true };
    // `tab` will either be a `tabs.Tab` instance or `undefined`.
    let [tab] = await chrome.tabs.query(queryOptions);
    return tab;
  }

And then I have a content.js script that processes the sendMessage:

chrome.runtime.onMessage.addListener(
    function (request, sender, sendResponse){
        sendResponse({status: "recieved tab with YouTube video"});
        startTimer(document.querySelector('video'), request.workTime, request.restTime);
});

function startTimer(video, workTime, restTime) {
    
    ... 

    const intervalId = setInterval(() => {

        ...

    }, 1000);

}

**I was wondering if there was a promise I wasn't catching if so where? **

And in my manifest I have

"permissions": [
        "scripting",
        "activeTab",
        "tabs",
    ],
"content_scripts": [
  {
    "js": ["./scripts/content.js"],
    "matches": ["*://*.youtube.com/watch*"]
  }
]

I have tried using the console log in the popup and I am able inside the if loop that contains chrome.tabs.sendMessage function but either that function or after it nothing else works and I receive the previous error.

I have looked at other blog posts and I'm not sure where my error is.

Ideally I would be on a youtube.com/watch page and when I use the popup.html and click the button to submit me pomodoro time info the content js does it business and interacts with the youtube page.

  • [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Norio Yamamoto Jan 17 '23 at 06:47
  • 1
    1) Remove `watch` from `matches` because youtube is a SPA so it keeps the same page when you navigate from `/` to `/watch`, see [how to handle it](/a/34100952). 2) [Chrome extension content script re-injection after upgrade or install](https://stackoverflow.com/q/10994324), 3) There's a bug in Chrome if you debug the popup in devtools, see [this workaround](/a/63886442). – wOxxOm Jan 17 '23 at 08:36

0 Answers0