Persist Twitter(X) Mute

Control the global mute for embedded videos on Twitter

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Persist Twitter(X) Mute
// @version     0.2.0
// @description Control the global mute for embedded videos on Twitter
// @namespace   https://github.com/oodzchen/user-scripts
// @author      Kholin
// @grant       GM.setValue
// @grant       GM.getValue
// @run-at      document-end
// @include     https://x.com/*
// @include     https://twitter.com/*
// @license     MIT
// ==/UserScript==

(async () => {
    const setPlayEvent = (vdoEl) => {
	vdoEl.onplay = async (ev) => {
	    const globalMuted = await GM.getValue('global_audio_muted')

	    if (globalMuted != undefined){
		const container = vdoEl.closest('div[data-testid^="cellInnerDiv-tweet-"]') || vdoEl.closest('div[data-testid="videoComponent"]')
		if(!container) return
		
		const relateMuteBtn = container.querySelector('div.r-ero68b:nth-child(2) button, button[data-testid^="immersive-tweet-mute-button-"]')
		if (relateMuteBtn){
		    if (vdoEl.muted != globalMuted){
			relateMuteBtn.click()
		    }
		} else {
		    vdoEl.muted = globalMuted
		}
	    }
	    
	}
    }

    const setMuteEvent = (mtEl) => {
	mtEl.onclick = (ev) => {
	    if (ev.isTrusted){
		const container = mtEl.closest('div[data-testid^="cellInnerDiv-tweet-"]') || mtEl.closest('div[data-testid="videoComponent"]')
		if (!container) return
		const relateVidEl = container.querySelector('video')
		if (!relateVidEl) return
		
		GM.setValue('global_audio_muted', !relateVidEl.muted)
	    }
	}
    }

    const callback = (mutations, observer) => {
	for (const mutation of mutations) {
	    const videoList = mutation.target.querySelectorAll('div[data-testid="videoComponent"] video')
	    Array.from(videoList).forEach(el => {
		if (el.isConnected) {
		    setPlayEvent(el)
		}
	    })

	    const muteBtnList = mutation.target.querySelectorAll('div.r-ero68b:nth-child(2) button, button[data-testid^="immersive-tweet-mute-button-"]')
	    Array.from(muteBtnList).forEach(el => {
		if (el.isConnected) {
		    setMuteEvent(el)
		}
	    })
	}
    }

    const observer = new MutationObserver(callback)

    observer.observe(document.body, {
	attributes: false,
	childList: true,
	subtree: true,
    })

})()