--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/3b153abd-a277-45cd-a449-9341b4f8747fn%40chromium.org.
Hi Patrick,Thank you for your response. I was unaware that offscreen elements can only be created by background scripts. I have updated my coed accordingly - it may not be the best workaround right now. Still, now, my content script, after running the checkForVideoElement() function, sends a message to background.js, which in turn initiates the offscreen document. Here is my below code. Could you help me understand why background.js is throwing this error:
Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist.updated content.js:
// Try to find the video element immediatelyif (!checkForVideoElement()) {// If not found, use a mutation observer to watch for changesconst observer = new MutationObserver((mutationsList, observer) => {for (const mutation of mutationsList) {if (mutation.type === 'childList') {if (checkForVideoElement()) {// Stop observing once the video element is foundobserver.disconnect();break;}}}});// Start observing the document body for added nodesobserver.observe(document.body, { childList: true, subtree: true });}function checkForVideoElement() {var video = document.querySelector("video");if (video) {console.log(video);if (video.currentTime <= 60) {console.log("Video found less than 60s");
chrome.runtime.sendMessage("runOffscreenTask");// creating offscreen document to play the custom sound// chrome.offscreen.createDocument({// url: chrome.runtime.getURL("offscreen.html"),// reasons: ["AUDIO_PLAYBACK"],// justification: "Playing Custom Sound"// }, () => {// chrome.runtime.sendMessage("playCustomSound");// });}return true;}return false;}background.js:chrome.tabs.onUpdated.addListener(function (tabId, info, tab) {if (info.status == "complete") {console.log("Tab updated");chrome.tabs.update(tabId, { muted: true });chrome.scripting.executeScript({target: { tabId: tabId },files: ['scripts/content.js']}, () => {console.log("Content script injected successfully");})// keep the tab muted until the custom Tudum sound has played + remainder of 8 secondschrome.storage.local.get("customTudumDuration", function (result) {let duration = result.customTudumDuration || 0;console.log("Duration of the custom Tudum sound: " + duration + "ms");setTimeout(() => {chrome.tabs.update(tabId, { muted: false });}, (8000 - duration));});}}});chrome.runtime.onMessage.addListener(async (message, sender, sendResponse) => {if (message === "runOffscreenTask") {await setupOffscreenDocument('offscreen.html');chrome.runtime.sendMessage("playCustomSound");}});let creating; // A global promise to avoid concurrency issuesasync function setupOffscreenDocument() {// Check all windows controlled by the service worker to see if one// of them is the offscreen document with the given pathconst offscreenUrl = chrome.runtime.getURL("offscreen.html");const existingContexts = await chrome.runtime.getContexts({contextTypes: ['OFFSCREEN_DOCUMENT'],documentUrls: [offscreenUrl]});if (existingContexts.length > 0) {return;}// create offscreen documentif (creating) {await creating;} else {creating = chrome.offscreen.createDocument({
url: chrome.runtime.getURL("offscreen.html"),reasons: ["AUDIO_PLAYBACK"],justification: "Playing Custom Sound",
});await creating;creating = null;}}