Selective HTML5 Media Audio Output for Chrome

Allows users to select specific audio output device for HTML5 audio / video in a per site basis.

目前為 2019-10-18 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Selective HTML5 Media Audio Output for Chrome
// @namespace    https://greasyfork.org/en/users/85671-jcunews
// @version      1.0.1
// @license      AGPLv3
// @author       jcunews
// @description  Allows users to select specific audio output device for HTML5 audio / video in a per site basis.
// @match        *://*/*
// @grant        GM_registerMenuCommand
// @grant        unsafeWindow
// @run-at       document-start
// ==/UserScript==

/*
Audio output selection can be accessed by clicking on the "Select audio output device for this site" menu
of this script's entry on the Tampermonkey/Violentmonkey/Greasemonkey browser extension's popup menu.

Alternatively, the audio selection menu can be provided as a bookmarklet and be added onto the browser's
bookmark toolbar. The bookmark URL should be:

  javascript:shmao_ujs()

Note:
HTML5 audio output selection is currently supported only on Chrome browser and its clones, and when the 
HTML page is not accessed using "file://" URL.
*/

((si, ds, sl, err, ec, pc) => {
  function errSet(e) {
    alert(`Failed to set audio output device to "${sl}".\n${e}`);
  }
  function setOutput(el, single) {
    if (si) {
      if (single) ec = null;
      el.setSinkId(si).catch(e => ec = e).finally(() => (single || !--pc) && ec && errSet(ec));
    }
  }
  function setAll(es) {
    pc = (es = document.querySelectorAll('audio,video')).length;
    ec = null;
    es.forEach(e => setOutput(e));
  }
  function devLabel(d) {
    return (/[0-9a-f]{64}/).test(d.deviceId) ? d.label : d.deviceId;
  }
  function refDevInfo() {
    if (location.protocol === "file:") return err = true;
    navigator.mediaDevices.getUserMedia({audio: true}).catch(() => err = true).then(() => {
      if (err) return;
      navigator.mediaDevices.enumerateDevices().catch(() => err = true).then((dis, ns, k) => {
        if (err || (dis.length && !dis[0].label)) return err = true;
        ns = {};
        k = "";
        ds = dis.reduce((r, di, i, s) => {
          if (di.kind === "audiooutput") {
            if (ns[di.label] && (ns[di.label] !== di.groupId)) {
              for (i = 2; i < 99; i++) {
                s = di.label + " #" + i;
                if (!ns[s]) {
                  di = Object.keys(di).reduce((r, k) => {
                    r[k] = di[k];
                    return r;
                  }, {});
                  di.label = s;
                  break;
                }
              }
            }
            ns[di.label] = di.groupId;
            if (sl && (devLabel(di) === sl)) k = di.deviceId;
            r.push(di);
          }
          return r;
        }, []);
        if (!ds.length) return;
        si = k ? k : "default";
        setAll();
      });
    });
  }
  if (!document.documentElement) return;
  ds = [];
  if (location.protocol !== "file:") {
    sl = localStorage.shmaoAudioOutput ? localStorage.shmaoAudioOutput : "";
  } else sl = "";
  refDevInfo();
  GM_registerMenuCommand("Select audio output device for this site", unsafeWindow.shmao_ujs = (a, j, k, l, s, t) => {
    if (err) {
      if (location.protocol !== "file:") {
        alert("Audio output device can not be selected at this time.");
      } else alert('Audio output device can not be selected for HTML pages accessed using "file://" URL.');
      return;
    }
    l = ds.map((d, i) => {
      if ((sl && (devLabel(d) === sl)) || (!sl && (d.deviceId === "default"))) j = i + 1;
      if (d.deviceId === "default") k = i + 1;
      return `[${i + 1}] ${d.label}`;
    }).join("\n") + "\n[0] <<Clear user selection (use the default device)>>";
    if (isNaN(j)) {
      s = `User selected audio output device is no longer installed:\n    ${sl}\n\n`;
      t = `[${k}] ${ds[k - 1].label}`;
    } else {
      s = "";
      t = `[${j}] ${ds[j - 1].label}`;
    }
    a = prompt(`${s}Currently selected audio output device:\n    ${t}\n\nPlease enter a device number to use:\n\n${l}`);
    if ((a === null) || !(a = a.replace(/^\s+|\s+$/g, "")) || isNaN(a = parseInt(a)) || (a < 0) || (a > ds.length)) return;
    if (a) {
      sl = devLabel(ds[a - 1]);
      if (location.protocol !== "file:") localStorage.shmaoAudioOutput = sl;
      si = ds[a - 1].deviceId;
    } else {
      sl = "";
      si = "default";
      if (location.protocol !== "file:") delete localStorage.shmaoAudioOutput;
    }
    setAll();
  });
  if (err) return;
  navigator.mediaDevices.addEventListener("devicechange", refDevInfo);
  (new MutationObserver(
    recs => recs.forEach(
      rec => rec.addedNodes.forEach(node => {
        if (["AUDIO", "VIDEO"].indexOf(node.tagName) >= 0) {
          setOutput(node, true);
          node.addEventListener("play", function() {
            setOutput(this, true);
          });
        }
      })
    )
  )).observe(document.documentElement, {childList: true, subtree: true});
})();