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.