Skip to content

Commit 2cead80

Browse files
committed
新增 [强制当前网站使用全局音量] 功能(针对不支持调节音量的),以及附带的 [修改当前网站默认音量] 功能; 优化 修改全局默认音量后当前网页立即生效; 修复 忘记当前网站音量后脚本菜单里数值没变的问题
1 parent 49c6494 commit 2cead80

1 file changed

Lines changed: 63 additions & 4 deletions

File tree

HTML5Volume.user.js

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// @name:zh-CN HTML5 视频音频默认音量
44
// @name:zh-TW HTML5 視訊音訊預設音量
55
// @name:ru Громкость аудио-видео в формате HTML5 по умолчанию
6-
// @version 1.0.3
6+
// @version 1.0.4
77
// @author X.I.U
88
// @description Avoid being startled by some video/audio with default 100% volume! And support each website to remember the volume separately...
99
// @description:zh-CN 避免被一些默认 100% 音量的视频/音频吓一跳(或社死)!且支持各网站分别记住音量...
@@ -34,12 +34,58 @@
3434
let nowVolume = ' (跟随全局)'
3535
if (localStorage.getItem('html5_xiu_currentVolume')) nowVolume = ' [ ' + parseInt(localStorage.getItem('html5_xiu_currentVolume')) + '% ]'
3636
menu_ID[1] = GM_registerMenuCommand('🔁 忘记当前网站音量' + nowVolume, function(){resetCurrentVolume()});
37-
menu_ID[2] = GM_registerMenuCommand('💬 反馈 & 建议', function () {GM_openInTab('https://github.com/XIU2/UserScript#xiu2userscript', {active: true,insert: true,setParent: true}); GM_openInTab('https://greasyfork.org/zh-CN/scripts/438400/feedback', {active: true,insert: true,setParent: true});});
37+
// 强制当前网站使用全局音量(针对部分不支持调节音量的网站)
38+
if (menu_forcedToEnable('check')) { // 当前网站是否已存在强制列表中
39+
menu_ID[2] = GM_registerMenuCommand('✅ 已强制当前网站使用全局音量 (针对不支持调节音量的)', function(){menu_forcedToEnable('del')});
40+
menu_ID[4] = GM_registerMenuCommand('#️⃣ 修改当前网站默认音量 (针对不支持调节音量的)', function(){customCurrentDefaultVolume()});
41+
} else {
42+
menu_ID[2] = GM_registerMenuCommand('❌ 未强制当前网站使用全局音量 (针对不支持调节音量的)', function(){menu_forcedToEnable('add')});
43+
}
44+
menu_ID[3] = GM_registerMenuCommand('💬 反馈 & 建议', function () {GM_openInTab('https://github.com/XIU2/UserScript#xiu2userscript', {active: true,insert: true,setParent: true}); GM_openInTab('https://greasyfork.org/zh-CN/scripts/438400/feedback', {active: true,insert: true,setParent: true});});
3845
}
3946

4047
insPage();
4148
currentPage();
4249

50+
// 强制当前网站使用全局音量(针对部分不支持调节音量的网站)
51+
function menu_forcedToEnable(type) {
52+
switch(type) {
53+
case 'check':
54+
if(check()) return true
55+
return false
56+
break;
57+
case 'add':
58+
add();
59+
break;
60+
case 'del':
61+
del();
62+
break;
63+
}
64+
65+
function check() { // 存在返回真,不存在返回假
66+
let websiteList = GM_getValue('menu_forcedToEnable',[]); // 读取网站列表
67+
if (websiteList.indexOf(location.host) === -1) return false // 不存在返回假
68+
return true
69+
}
70+
71+
function add() {
72+
if (check()) return
73+
let websiteList = GM_getValue('menu_forcedToEnable',[]); // 读取网站列表
74+
websiteList.push(location.host); // 追加网站域名
75+
GM_setValue('menu_forcedToEnable', websiteList); // 写入配置
76+
location.reload(); // 刷新网页
77+
}
78+
79+
function del() {
80+
if (!check()) return
81+
let websiteList = GM_getValue('menu_forcedToEnable',[]), // 读取网站列表
82+
index = websiteList.indexOf(location.host);
83+
websiteList.splice(index, 1); // 删除网站域名
84+
GM_setValue('menu_forcedToEnable', websiteList); // 写入配置
85+
location.reload(); // 刷新网页
86+
}
87+
}
88+
4389

4490
// 网页本身的 Video Audio 标签
4591
function currentPage() {
@@ -88,7 +134,9 @@
88134

89135
// 判断该视频/音频元素是否已监听事件
90136
function isFirstEvent(target) {
91-
if (!target.controls) return; // 如果视频/音频已经有了自己的控件(即没有使用 HTML5 默认的控件),则退出
137+
if (!menu_forcedToEnable('check')) { // 如果未强制当前网站使用全局音量(针对部分不支持调节音量的网站)
138+
if (!target.controls) return; // 如果视频/音频已经有了自己的控件(即没有使用 HTML5 默认的控件),则退出
139+
}
92140
modifyVolume(target);
93141
// 如果没有该属性,则代表是还未监听事件
94142
if (target.dataset.html5VolumeXiu != 'true') {
@@ -111,8 +159,18 @@
111159

112160
// 修改全局默认音量
113161
function customDefaultVolume() {
114-
let newValue = parseFloat(prompt('修改全局默认音量,不影响各网站记住的音量,当前网页需刷新后生效~\n范围:0~100 (即 0%~100%,不需要加 % 百分号)\n默认:30', GM_getValue('menu_defaultVolume', 30)));
162+
let newValue = parseFloat(prompt('修改全局默认音量,不影响各网站记住的音量,修改后当前网页立即生效~\n范围:0~100 (即 0%~100%,不需要加 % 百分号)\n默认:30', GM_getValue('menu_defaultVolume', 30)));
115163
if (!Number.isNaN(newValue) && newValue >= 0 && newValue <= 100) {GM_setValue('menu_defaultVolume', newValue);}
164+
currentPage(); // 重置当前网页的音量
165+
registerMenuCommand(); // 重新注册菜单(刷新菜单上的音量值)
166+
}
167+
168+
169+
// 修改当前网站默认音量 (针对不支持调节音量的网站)
170+
function customCurrentDefaultVolume() {
171+
let newValue = parseFloat(prompt('修改当前网站默认音量 (针对不支持调节音量的网站),修改后立即生效~\n范围:0~100 (即 0%~100%,不需要加 % 百分号)\n默认:全局默认音量', localStorage.getItem('html5_xiu_currentVolume') || GM_getValue('menu_defaultVolume', 30)));
172+
if (!Number.isNaN(newValue) && newValue >= 0 && newValue <= 100) {localStorage.setItem('html5_xiu_currentVolume', newValue);}
173+
currentPage(); // 重置当前网页的音量
116174
registerMenuCommand(); // 重新注册菜单(刷新菜单上的音量值)
117175
}
118176

@@ -121,5 +179,6 @@
121179
function resetCurrentVolume() {
122180
if (localStorage.getItem('html5_xiu_currentVolume')) localStorage.removeItem('html5_xiu_currentVolume') // 清理 localStorage
123181
currentPage(); // 重置当前网页的音量
182+
registerMenuCommand(); // 重新注册菜单(刷新菜单上的音量值)
124183
}
125184
})();

0 commit comments

Comments
 (0)