2021-07-28

I can't unmute a tab for a chrome extension

I've been trying to create a chrome extension that mutes the first 10 seconds of any youtube page. This is my first time making an extension and working with javascript so apologies in advance if I'm missing something obvious!

THE PROBLEM: Right now, the page will start off unmuted then randomly mute after 2 seconds, and then unmute for a second after 10 seconds, then mute again. I don't know why the listener keeps getting called.

I want it to start off muted and then unmute after 10 seconds.

Here are my files:

manifest.json

{
    "manifest_version":3,
    "name": "Muter",
    "description": "Mutes",
    "version": "1.0",

    "action":
    {
        "default_icon": "icon.png",
        "default_popup": "popup.html"
    },
    "permissions": [
        "webNavigation",
        "tabs",
        "scripting"
    ],
    "host_permissions": [
        "http://*/",
        "https://*/"
      ],
    "background": {
        "service_worker": "background-worker.js"
      }
}

background.js


var d = new Date();
var startTime = d.getTime();

console.log(startTime);

function muter(tabId){
          chrome.tabs.update(tabId, {muted: true});
        }

function unmuter(tabId){
          chrome.tabs.update(tabId, {muted: false});
        }


chrome.tabs.onUpdated.addListener(function(id, info, tab){
    if (tab.status !== "complete"){
        return;
    }

    if(tab.url.indexOf("youtube") != -1){
        muter(id);
        setTimeout(function(){ unmuter(id) }, 10000);
        console.log("hey")
    }
})

I haven't been able to find anything about this online either. Any help would be appreciated!



from Recent Questions - Stack Overflow https://ift.tt/3x8BksA
https://ift.tt/eA8V8J

No comments:

Post a Comment