Youtube Music 自動純音訊切換器

播放Youtube Music時自動切換成純音訊模式節省流量

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Youtube Music Auto Audio Mode
// @name:zh-TW   Youtube Music 自動純音訊切換器
// @name:zh-CN   Youtube Music 自动纯音讯切换器
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Youtube Music simple script for save the network traffic with auto switch music to audio-only mode
// @description:zh-tw 播放Youtube Music時自動切換成純音訊模式節省流量
// @description:zh-cn 播放Youtube Music时自动切换成纯音讯模式节省流量
// @author       You
// @match        https://music.youtube.com/*
// @icon         https://music.youtube.com/favicon.ico
// @grant        GM_registerMenuCommand
// @grant        GM_setValue
// @grant        GM_getValue
// @license MIT
// ==/UserScript==

const settings = {
	isEnabled: true,
	isHidden: false,
	autoMusicIntervalNumber: undefined,
};

window.onload = async () => {
	window.trustedTypes.createPolicy("default", { createHTML: (string, sink) => string });
	await loadSettings();
	await saveSettings();
	await GM_registerMenuCommand("Open Menu", () => {
		// gm menu command
		createSettingsPanel();
		settings.isHidden = false;
	});
	if (settings.isEnabled) {
		execute();
	}
	if (!settings.isHidden) {
		createSettingsPanel();
	}
};

async function saveSettings() {
	await GM.setValue("autoMusicMode", JSON.stringify(settings));
}

async function loadSettings() {
	const storedSettings = await GM.getValue("autoMusicMode");
	if (storedSettings) {
		Object.assign(settings, JSON.parse(storedSettings));
	}
}

function execute() {
	switchToAudio();
	const id = setInterval(() => {
		switchToAudio();
	}, 10000);
	settings.autoMusicIntervalNumber = id;
	saveSettings();
}

function switchToAudio() {
	if (document.querySelector('ytmusic-av-toggle[class="style-scope ytmusic-player-page"]').getAttribute("playback-mode") === "OMV_PREFERRED") {
		document.getElementsByClassName("song-button style-scope ytmusic-av-toggle")[0].click();
	}
}

function createSettingsPanel() {
	const panel = document.createElement("div");
	panel.id = "settingsPanel";
	panel.style.position = "fixed";
	panel.style.top = "10px";
	panel.style.right = "10px";
	panel.style.backgroundColor = "rgba(0, 0, 0, 1)";
	panel.style.padding = "10px";
	panel.style.borderRadius = "5px";
	panel.style.zIndex = "9999";
	panel.style.fontSize = "14px";

	panel.innerHTML = `
		<h3 style="color: white;">Only Audio</h3>
        <label style="color: white;">
            <input type="checkbox" id="isEnabled" ${settings.isEnabled ? "checked" : ""}>
            On/Off
        </label><br>
        <button id="hideSettings" style="margin-top: 10px;">close</button>
    `;

	document.body.appendChild(panel);

	document.getElementById("isEnabled").addEventListener("change", async (e) => {
		settings.isEnabled = e.target.checked;
		if (settings.isEnabled) {
			execute();
		} else {
			clearInterval(settings.autoMusicIntervalNumber);
			settings.autoMusicIntervalNumber = undefined;
		}
		await saveSettings();
	});
	document.getElementById("hideSettings").addEventListener("click", async () => {
		settings.isHidden = true;
		document.getElementById("settingsPanel").remove();
		await saveSettings();
	});
}