// ==UserScript==
// @name 媒體音量增強器
// @version 0.0.36-Beta
// @author Canaan HS
// @description 增強媒體音量最高至 20 倍,可記住增強設置後自動應用,部分網站可能無效或無聲,可選擇禁用。
// @description:zh-TW 增強媒體音量最高至 20 倍,可記住增強設置後自動應用,部分網站可能無效或無聲,可選擇禁用。
// @description:zh-CN 增强媒体音量最高至 20 倍,可记住增强设置后自动应用,部分网站可能无效或无声,可选择禁用。
// @description:en Boost media volume up to 20 times, automatically apply saved settings, may not work or have no sound on some sites, can disable if needed.
// @noframes
// @match *://*/*
// @icon https://cdn-icons-png.flaticon.com/512/8298/8298181.png
// @license MIT
// @namespace https://greasyfork.org/users/989635
// @run-at document-start
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_addStyle
// @grant GM_getResourceURL
// @grant GM_registerMenuCommand
// @grant GM_addValueChangeListener
// @resource Img https://cdn-icons-png.flaticon.com/512/8298/8298181.png
// @require https://update.greasyfork.org/scripts/487608/1413530/ClassSyntax_min.js
// ==/UserScript==
(async()=>{(new class extends Syntax{constructor(){super();this.Increase=this.Booster=null;this.EnhanceNodes=[];this.MediaContent=null;this.AudioContext=window.AudioContext||window.webkitAudioContext;this.ExcludeStatus=this.Init=this.ObserverOption=this.MediaObserver=null;this.Host=this.Device.Host;this.Lang=this.Language(this.Device.Lang);this.BannedHost=this.Store("g","BannedDomains_v2",{});this.GetBannedHost=b=>{this.ExcludeStatus=this.BannedHost[this.Host]??!1;b(!this.ExcludeStatus)};this.Banned=async()=>{this.ExcludeStatus?delete this.BannedHost[this.Host]:this.BannedHost[this.Host]=!0;this.Store("s","BannedDomains_v2",this.BannedHost);location.reload()};this.MenuHotkey=async()=>{this.AddListener(document,"keydown",b=>{b.altKey&&"B"==b.key.toUpperCase()&&this.BoosterMenu()},{passive:!0,capture:!0})}}BoosterFactory(b,c){try{if(!this.AudioContext)throw this.Lang.BT1;this.MediaContent||(this.MediaContent=new this.AudioContext);const e=this.EnhanceNodes.length;for(const a of b){const f=this.MediaContent.createMediaElementSource(a),g=this.MediaContent.createGain(),d=this.MediaContent.createBiquadFilter(),k=this.MediaContent.createBiquadFilter(),l=this.MediaContent.createBiquadFilter(),h=this.MediaContent.createDynamicsCompressor(),m=setInterval(()=>{a.volume=1},1E3);setTimeout(()=>{clearInterval(m)},3E3);g.gain.value=this.Increase**2;d.type="lowshelf";d.gain.value=2.2;d.frequency.value=200;k.type="peaking";k.Q.value=1;k.gain.value=3;k.frequency.value=1200;l.type="highshelf";l.gain.value=1.8;l.frequency.value=12E3;h.ratio.value=5.4;h.knee.value=.4;h.threshold.value=-12;h.attack.value=.02;h.release.value=.4;f.connect(g).connect(d).connect(k).connect(l).connect(h).connect(this.MediaContent.destination);a.setAttribute("Enhanced-Node",!0);this.EnhanceNodes.push(g)}this.EnhanceNodes.length>e&&(this.Log(this.Lang.BT3,{"Booster Media : ":b,"Elapsed Time : ":this.Runtime(c,{log:!1})},{collapsed:!1}),this.Init||(this.Init=!0,this.Menu({[this.Lang.MK]:{func:()=>alert(this.Lang.MKT)},[this.Lang.MM]:{func:()=>this.BoosterMenu()}},"Menu",2),this.MenuHotkey(),this.StoreListen([this.Host],a=>{a.far&&a.key==this.Host&&this.Booster.setVolume(a.nv)})));setTimeout(()=>{this.MediaObserver.observe(document,this.ObserverOption)},3E3);return{setVolume:a=>{this.Increase=a;this.EnhanceNodes.forEach(f=>{f.gain.value=this.Increase**2})}}}catch(e){this.Log(this.Lang.BT4,e,{type:"error",collapsed:!1})}}async Trigger(b,c){try{this.Increase=this.Store("g",this.Host)??1,this.Booster=this.BoosterFactory(b,c),this.AddStyle(".Booster-Modal-Background {top: 0;left: 0;opacity: 1;width: 100%;height: 100%;display: flex;z-index: 9999;overflow: auto;position: fixed;align-items: center;justify-content: center;transition: opacity 0.4s ease;}.Booster-Modal-Button {margin: 0 2% 2% 0;color: #d877ff;font-size: 16px;font-weight: bold;padding: 0 0.3rem;border-radius: 3px;background-color: #ffebfa;border: 1px solid rgb(124, 183, 252);}.Booster-Modal-Button:hover,.Booster-Modal-Button:focus {color: #fc0e85;cursor: pointer;text-decoration: none;}.Booster-Modal-Content {width: 400px;padding: 5px;overflow: auto;background-color: #cff4ff;border-radius: 10px;text-align: center;border: 2px ridge #82c4e2;border-collapse: collapse;margin: 2% auto 8px auto;}.Booster-Multiplier {font-size:25px;color:rgb(253, 1, 85);margin: 15px;font-weight:bold;}.Booster-Modal-Background-Closur {opacity: 0;pointer-events: none;}.Booster-Slider {width: 350px;}div input {cursor: pointer;}#sound-save {cursor: pointer;}","Booster-Menu",!1)}catch(e){this.Log("Trigger Error : ",e,{type:"error",collapsed:!1})}}async BoosterMenu(){if(!this.$$(".Booster-Modal-Background")){const b=document.createElement("div");b.innerHTML=`<div class="Booster-Modal-Background"><div class="Booster-Modal-Content"><h2 style="color: #3754f8;">${this.Lang.ST}</h2><div style="margin:1rem auto 1rem auto;"><div class="Booster-Multiplier"><span><img src="${GM_getResourceURL("Img")}" width="5%">${this.Lang.S1}</span><span id="Booster-CurrentValue">${this.Increase}</span><span>${this.Lang.S2}</span></div><input type="range" id="Adjustment-Sound-Enhancement" class="Booster-Slider" min="0" max="20.0" value="${this.Increase}" step="0.1"><br></div><div style="text-align: right;"><button class="Booster-Modal-Button" id="sound-save">${this.Lang.SS}</button></div></div></div>`;document.body.appendChild(b);const c=this.$$("#Booster-CurrentValue"),e=this.$$("#Adjustment-Sound-Enhancement");let a;this.Listen(e,"input",d=>{requestAnimationFrame(()=>{a=d.target.value;c.textContent=a;this.Booster.setVolume(a)})},{passive:!0,capture:!0});const f=this.$$(".Booster-Modal-Background");this.Listen(f,"click",d=>{d.stopPropagation();d=d.target;"sound-save"===d.id?(this.Increase=d=parseFloat(e.value),this.Store("s",this.Host,d),g()):"Booster-Modal-Background"===d.className&&g()},{capture:!0});function g(){f.classList.add("Booster-Modal-Background-Closur");setTimeout(()=>{f.remove()},1200)}}}async Injec(){const b=async c=>{this.Menu({[c]:{func:()=>this.Banned()}})};this.GetBannedHost(c=>{if(c){const e=this.Debounce(a=>{const f=[...this.$$("video, audio",{all:!0})].filter(g=>g&&!g.hasAttribute("Enhanced-Node"));0<f.length&&a(f)},400);this.Observer(document,()=>{const a=this.Runtime();e(f=>{this.MediaObserver.disconnect();this.Trigger(f,a)})},{mark:"Media-Booster",attributes:!1,throttle:500},a=>{this.MediaObserver=a.ob;this.ObserverOption=a.op;b(this.Lang.MD)})}else b(this.Lang.MS)})}Language(b){var c={MS:"\u2705 \u555f\u7528\u589e\u5e45",MD:"\u274c \u7981\u7528\u589e\u5e45",MK:"\ud83d\udcdc \u83dc\u55ae\u71b1\u9375",MM:"\ud83d\udee0\ufe0f \u8abf\u6574\u83dc\u55ae",MKT:"\u71b1\u9375\u547c\u53eb\u8abf\u6574\u83dc\u55ae!!\n\n\u5feb\u6377\u7d44\u5408 : (Alt + B)",BT1:"\u4e0d\u652f\u63f4\u97f3\u983b\u589e\u5f37\u7bc0\u9ede",BT2:"\u6dfb\u52a0\u589e\u5f37\u7bc0\u9ede\u5931\u6557",BT3:"\u6dfb\u52a0\u589e\u5f37\u7bc0\u9ede\u6210\u529f",BT4:"\u589e\u5f37\u5931\u6557",ST:"\u97f3\u91cf\u589e\u5f37",S1:"\u589e\u5f37\u500d\u6578 ",S2:" \u500d",SS:"\u4fdd\u5b58\u8a2d\u7f6e"},e={MS:"\u2705 \u542f\u7528\u589e\u5e45",MD:"\u274c \u7981\u7528\u589e\u5e45",MK:"\ud83d\udcdc \u83dc\u5355\u70ed\u952e",MM:"\ud83d\udee0\ufe0f \u8c03\u6574\u83dc\u5355",MKT:"\u70ed\u952e\u547c\u53eb\u8c03\u6574\u83dc\u5355!!\n\n\u5feb\u6377\u7ec4\u5408 : (Alt + B)",BT1:"\u4e0d\u652f\u63f4\u97f3\u9891\u589e\u5f3a\u8282\u70b9",BT2:"\u6dfb\u52a0\u589e\u5f3a\u8282\u70b9\u5931\u8d25",BT3:"\u6dfb\u52a0\u589e\u5f3a\u8282\u70b9\u6210\u529f",BT4:"\u589e\u5f3a\u5931\u8d25",ST:"\u97f3\u91cf\u589e\u5f3a",S1:"\u589e\u5f3a\u500d\u6570 ",S2:" \u500d",SS:"\u4fdd\u5b58\u8bbe\u7f6e"};c={"zh-TW":c,"zh-HK":c,"zh-MO":c,"zh-CN":e,"zh-SG":e,"en-US":{MS:"\u2705 Enable Boost",MD:"\u274c Disable Boost",MK:"\ud83d\udcdc Menu Hotkey",MM:"\ud83d\udee0\ufe0f Adjust Menu",MKT:"Hotkey to Call Menu Adjustments!!\n\nShortcut: (Alt + B)",BT1:"Audio enhancement node not supported",BT2:"Failed to add enhancement node",BT3:"Enhancement node added successfully",BT4:"Enhancement failed",ST:"Volume Boost",S1:"Boost Level ",S2:" X",SS:"Save Settings"}};return c[b]??c["en-US"]}}).Injec()})();