WebRTC media source switcher

Forcefully chose a specific webrtc input/output audio/video device

2020-04-01 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         WebRTC media source switcher
// @namespace    https://daniil.it
// @version      0.1.1
// @description  Forcefully chose a specific webrtc input/output audio/video device
// @author       Daniil Gentili <[email protected]>
// @match        https://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    var hidden = {
        enumerateDevices: navigator.mediaDevices.enumerateDevices.bind(navigator.mediaDevices)
    }
    var newRes = [];
    navigator.mediaDevices.enumerateDevices = () => hidden.enumerateDevices().then(res => {
        if (newRes) return newRes;

        var temp = {};
        var tempLabels = {};
        for (var i = 0; i < res.length; i++) {
            if (!temp[res[i].kind]) {
                temp[res[i].kind] = [];
                tempLabels[res[i].kind] = "Choose a "+res[i].kind+" device:\n";
            }
            temp[res[i].kind].push(res[i])
            tempLabels[res[i].kind] += temp[res[i].kind].length+": "+res[i].label+"\n"
        }
        for (let [key, value] of Object.entries(tempLabels)) {
            var choice = prompt(value);
            newRes.push(temp[key][choice-1]);
        }

        return newRes;
    })
})();