joeywatts / disable-autogain-control-extension

A chrome extension which disables the automatic microphone gain control in the MediaStream Web API.
MIT License
185 stars 19 forks source link

Switch to Manifest v3 for Chrome #14

Open AlexWayfer opened 2 months ago

AlexWayfer commented 2 months ago

image

image

dot-i commented 1 week ago

ChatGPT suggests the following v3 manifest ;-)

{
    "name": "Disable Automatic Gain Control",
    "version": "1.3",
    "description": "Disables the automatic microphone gain control enabled by web applications like Google Meet and Hangouts.",
    "manifest_version": 3,
    "background": {
        "service_worker": "background.js"
    },
    "content_scripts": [
      {
        "matches": ["<all_urls>"],
        "js": ["installDisableAutogain.js"]
      }
    ],
    "web_accessible_resources": [
        {
            "resources": ["disableAutogain.js"],
            "matches": ["<all_urls>"]
        }
    ],
    "action": {},
    "permissions": [
        "activeTab",
        "contextMenus"
    ],
    "host_permissions": [
        "http://*/",
        "https://*/"
    ]
}

And the following background.js changes:

// Show context menu that allows enabling/disabling on a per-domain basis.
chrome.action.onClicked.addListener((tab) => {
    const { origin } = new URL(tab.url);
    chrome.permissions.contains({
        origins: [origin + "/*"],
    }, (hasPermission) => {
        if (hasPermission) {
            chrome.permissions.remove({
                origins: [origin + "/*"]
            }, () => chrome.tabs.reload(tab.id));
        } else {
            chrome.permissions.request({
                origins: [origin + "/*"]
            }, () => chrome.tabs.reload(tab.id));
        }
    });
});

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

/**
 * @param {chrome.tabs.Tab} tab 
 */
function injectScriptIfNecessary(tab) {
    if (tab.status !== "loading" || !tab.url) {
        return;
    }

    try {
        const { origin, protocol } = new URL(tab.url);
        if (protocol !== "https:" && protocol !== "http:") {
            return;
        }
        chrome.permissions.contains({
            origins: [origin + "/*"]
        }, (hasPermission) => {
            if (hasPermission) {
                // Note: ExecuteScript is not available in Manifest v3. 
                // You need to use content_scripts in the manifest instead of this line.
                chrome.scripting.executeScript({
                    target: {tabId: tab.id, allFrames: true},
                    files: ["installDisableAutogain.js"]
                });
            }
            chrome.action.setTitle({
                title: hasPermission
                    ? "Disable Automatic Gain Control"
                    : "Enable Automatic Gain Control",
                tabId: tab.id,
            });
            chrome.action.setBadgeText({
                text: hasPermission ? "On" : "",
                tabId: tab.id,
            });
        });
    } catch (e) {
        console.error("Failed to inject script", e);
    }
}

function showUsage() {
    chrome.tabs.create({
        url: chrome.runtime.getURL("usage.html")
    });
}

function showUpgradeNotice() {
    chrome.tabs.create({
        url: chrome.runtime.getURL("upgradeFromV1.0.html")
    });
}

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
    if (typeof message === "object" && message["type"] === "enable-meet-hangouts") {
        chrome.permissions.request({
            origins: [
                "https://meet.google.com/*",
                "https://hangouts.google.com/*"
            ]
        }, (granted) => {
            sendResponse(granted);
        });
        return true;
    }
});

chrome.contextMenus.create({
    title: "Usage",
    contexts: ["action"],
    onclick: () => {
        showUsage();
    }
});

chrome.runtime.onInstalled.addListener(({ reason, previousVersion }) => {
    if (reason === "update" && previousVersion === "1.0") {
        showUpgradeNotice();
    } else if (reason === "install") {
        showUsage();
    }
});

But I have absolutely no idea if it will work just like this -- just passing this along to see if it can help get this fixed smoothly...