-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.lua
855 lines (779 loc) · 29.5 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
local msg = require('mp.msg')
local utils = require("mp.utils")
require("options")
require("guess")
require("api")
require('extra')
require('render')
input_loaded, input = pcall(require, "mp.input")
uosc_available = false
pid = utils.getpid()
-- from http://lua-users.org/wiki/LuaUnicode
local UTF8_PATTERN = '[%z\1-\127\194-\244][\128-\191]*'
-- return a substring based on utf8 characters
-- like string.sub, but negative index is not supported
function utf8_sub(s, i, j)
if i > j then
return s
end
local t = {}
local idx = 1
for char in s:gmatch(UTF8_PATTERN) do
if i <= idx and idx <= j then
local width = #char > 2 and 2 or 1
idx = idx + width
t[#t + 1] = char
end
end
return table.concat(t)
end
-- abbreviate string if it's too long
function abbr_str(str, length)
if not str or str == '' then return '' end
local str_clip = utf8_sub(str, 1, length)
if str ~= str_clip then
return str_clip .. '...'
end
return str
end
-- 打开番剧数据匹配菜单
function get_animes(query)
local encoded_query = url_encode(query)
local url = options.api_server .. "/api/v2/search/episodes"
local params = "anime=" .. encoded_query
local full_url = url .. "?" .. params
local items = {}
local message = "加载数据中..."
local menu_type = "menu_anime"
local menu_title = "在此处输入番剧名称"
local footnote = "使用enter或ctrl+enter进行搜索"
local menu_cmd = { "script-message-to", mp.get_script_name(), "search-anime-event" }
if uosc_available then
update_menu_uosc(menu_type, menu_title, message, footnote, menu_cmd, query)
else
show_message(message, 30)
end
msg.verbose("尝试获取番剧数据:" .. full_url)
local res = get_danmaku_contents(full_url)
if res.status ~= 0 then
local message = "获取数据失败"
if uosc_available then
update_menu_uosc(menu_type, menu_title, message, footnote, menu_cmd, query)
else
show_message(message, 3)
end
msg.error("HTTP 请求失败:" .. res.stderr)
end
local response = utils.parse_json(res.stdout)
if not response or not response.animes then
local message = "无结果"
if uosc_available then
update_menu_uosc(menu_type, menu_title, message, footnote, menu_cmd, query)
else
show_message(message, 3)
end
msg.verbose("无结果")
return
end
for _, anime in ipairs(response.animes) do
table.insert(items, {
title = anime.animeTitle,
hint = anime.typeDescription,
value = {
"script-message-to",
mp.get_script_name(),
"search-episodes-event",
anime.animeTitle, utils.format_json(anime.episodes),
},
})
end
if uosc_available then
update_menu_uosc(menu_type, menu_title, items, footnote, menu_cmd, query)
elseif input_loaded then
show_message("", 0)
mp.add_timeout(0.1, function()
open_menu_select(items)
end)
end
end
function get_episodes(animeTitle, episodes)
local items = {}
for _, episode in ipairs(episodes) do
table.insert(items, {
title = episode.episodeTitle,
value = { "script-message-to", mp.get_script_name(), "load-danmaku",
animeTitle, episode.episodeTitle, episode.episodeId },
keep_open = false,
selectable = true,
})
end
local menu_type = "menu_episodes"
local menu_title = "剧集信息"
local footnote = "使用 / 打开筛选"
if uosc_available then
update_menu_uosc(menu_type, menu_title, items, footnote)
elseif input_loaded then
mp.add_timeout(0.1, function()
open_menu_select(items)
end)
end
end
function update_menu_uosc(menu_type, menu_title, menu_item, menu_footnote, menu_cmd, query)
local items = {}
if type(menu_item) == "string" then
table.insert(items, {
title = menu_item,
value = "",
italic = true,
keep_open = true,
selectable = false,
align = "center",
})
else
items = menu_item
end
local menu_props = {
type = menu_type,
title = menu_title,
search_style = menu_cmd and "palette" or "on_demand",
search_debounce = menu_cmd and "submit" or 0,
on_search = menu_cmd,
footnote = menu_footnote,
search_suggestion = query,
items = items,
}
local json_props = utils.format_json(menu_props)
mp.commandv("script-message-to", "uosc", "open-menu", json_props)
end
function open_menu_select(menu_items, is_time)
local item_titles, item_values = {}, {}
for i, v in ipairs(menu_items) do
item_titles[i] = is_time and "[" .. v.hint .. "] " .. v.title or
(v.hint and v.title .. " (" .. v.hint .. ")" or v.title)
item_values[i] = v.value
end
mp.commandv('script-message-to', 'console', 'disable')
input.select({
prompt = '筛选:',
items = item_titles,
submit = function(id)
mp.commandv(unpack(item_values[id]))
end,
})
end
-- 打开弹幕输入搜索菜单
function open_input_menu_get()
mp.commandv('script-message-to', 'console', 'disable')
local title = parse_title(true)
input.get({
prompt = '番剧名称:',
default_text = title,
cursor_position = title and #title + 1,
submit = function(text)
input.terminate()
mp.commandv("script-message-to", mp.get_script_name(), "search-anime-event", text)
end
})
end
function open_input_menu_uosc()
local items = {}
if danmaku.anime and danmaku.episode then
local episode = danmaku.episode:gsub("%s.-$","")
episode = episode:match("^(第.*[话回集]+)%s*") or episode
items[#items + 1] = {
title = string.format("已关联弹幕:%s-%s", danmaku.anime, episode),
bold = true,
italic = true,
keep_open = true,
selectable = false,
}
end
items[#items + 1] = {
hint = " 追加|ds或|dy或|dm可搜索电视剧|电影|国漫",
keep_open = true,
selectable = false,
}
local menu_props = {
type = "menu_danmaku",
title = "在此处输入番剧名称",
search_style = "palette",
search_debounce = "submit",
search_suggestion = parse_title(true),
on_search = { "script-message-to", mp.get_script_name(), "search-anime-event" },
footnote = "使用enter或ctrl+enter进行搜索",
items = items
}
local json_props = utils.format_json(menu_props)
mp.commandv("script-message-to", "uosc", "open-menu", json_props)
end
function open_input_menu()
if uosc_available then
open_input_menu_uosc()
elseif input_loaded then
open_input_menu_get()
end
end
-- 打开弹幕源添加管理菜单
function open_add_menu_get()
mp.commandv('script-message-to', 'console', 'disable')
input.get({
prompt = 'Input url:',
submit = function(text)
input.terminate()
mp.commandv("script-message-to", mp.get_script_name(), "add-source-event", text)
end
})
end
function open_add_menu_uosc()
local sources = {}
for url, source in pairs(danmaku.sources) do
if source.fname then
local item = {title = url, value = url, keep_open = true,}
if source.from == "api_server" then
if source.blocked then
item.hint = "来源:弹幕服务器(已屏蔽)"
item.actions = {{icon = "check", name = "unblock"},}
else
item.hint = "来源:弹幕服务器(未屏蔽)"
item.actions = {{icon = "not_interested", name = "block"},}
end
else
item.hint = "来源:用户添加"
item.actions = {{icon = "delete", name = "delete"},}
end
table.insert(sources, item)
end
end
local menu_props = {
type = "menu_source",
title = "在此输入源地址url",
search_style = "palette",
search_debounce = "submit",
on_search = { "script-message-to", mp.get_script_name(), "add-source-event" },
footnote = "使用enter或ctrl+enter进行添加",
items = sources,
item_actions_place = "outside",
callback = {mp.get_script_name(), 'setup-danmaku-source'},
}
local json_props = utils.format_json(menu_props)
mp.commandv("script-message-to", "uosc", "open-menu", json_props)
end
function open_add_menu()
if uosc_available then
open_add_menu_uosc()
elseif input_loaded then
open_add_menu_get()
end
end
-- 打开弹幕内容菜单
function open_content_menu(pos)
local items = {}
local time_pos = pos or mp.get_property_native("time-pos")
if comments ~= nil then
for _, event in ipairs(comments) do
table.insert(items, {
title = abbr_str(event.clean_text, 60),
hint = seconds_to_time(event.start_time + delay),
value = { "seek", event.start_time + delay, "absolute" },
active = event.start_time + delay <= time_pos and time_pos <= event.end_time + delay,
})
end
end
local menu_props = {
type = "menu_content",
title = "弹幕内容",
footnote = "使用 / 打开搜索",
items = items
}
local json_props = utils.format_json(menu_props)
if uosc_available then
mp.commandv("script-message-to", "uosc", "open-menu", json_props)
elseif input_loaded then
open_menu_select(items, true)
end
end
local menu_items_config = {
bold = { title = "粗体", hint = options.bold, original = options.bold,
footnote = "true / false", },
fontsize = { title = "大小", hint = options.fontsize, original = options.fontsize,
scope = { min = 0, max = math.huge }, footnote = "请输入整数(>=0)", },
outline = { title = "描边", hint = options.outline, original = options.outline,
scope = { min = 0.0, max = 4.0 }, footnote = "输入范围:(0.0-4.0)" },
shadow = { title = "阴影", hint = options.shadow, original = options.shadow,
scope = { min = 0, max = math.huge }, footnote = "请输入整数(>=0)", },
density = { title = "密度", hint = options.density, original = options.density,
scope = { min = -1, max = math.huge }, footnote = "整数(>=-1) -1:表示不重叠 0:表示无限制 其他表示限定条数", },
scrolltime = { title = "速度", hint = options.scrolltime, original = options.scrolltime,
scope = { min = 1, max = math.huge }, footnote = "请输入整数(>=1)", },
transparency = { title = "透明度", hint = options.transparency, original = options.transparency,
scope = { min = 0, max = 255 }, footnote = "输入整数:0(不透明)到255(完全透明)", },
displayarea = { title = "弹幕显示范围", hint = options.displayarea, original = options.displayarea,
scope = { min = 0.0, max = 1.0 }, footnote = "显示范围(0.0-1.0)", },
}
-- 创建一个包含键顺序的表,这是样式菜单的排布顺序
local ordered_keys = {"bold", "fontsize", "outline", "shadow", "density", "scrolltime", "transparency", "displayarea"}
-- 设置弹幕样式菜单
function add_danmaku_setup(actived, status)
if not uosc_available then
show_message("无uosc UI框架,不支持使用该功能", 2)
return
end
local items = {}
for _, key in ipairs(ordered_keys) do
local config = menu_items_config[key]
local item_config = {
title = config.title,
hint = "目前:" .. tostring(config.hint),
active = key == actived,
keep_open = true,
selectable = true,
}
if config.hint ~= config.original then
item_config.actions = {{icon = "refresh", name = key, label = "恢复默认配置 < " .. config.original .. " >"}}
end
table.insert(items, item_config)
end
local menu_props = {
type = "menu_style",
title = "弹幕样式",
search_style = "disabled",
footnote = "样式更改仅在本次播放生效",
item_actions_place = "outside",
items = items,
callback = { mp.get_script_name(), 'setup-danmaku-style'},
}
local actions = "open-menu"
if status ~= nil then
-- msg.info(status)
if status == "updata" then
-- "updata" 模式会保留输入框文字
menu_props.title = " " .. menu_items_config[actived]["footnote"]
actions = "update-menu"
elseif status == "refresh" then
-- "refresh" 模式会清除输入框文字
menu_props.title = " " .. menu_items_config[actived]["footnote"]
elseif status == "error" then
menu_props.title = "输入非数字字符或范围出错"
-- 创建一个定时器,在1秒后触发回调函数,删除搜索栏错误信息
mp.add_timeout(1.0, function() add_danmaku_setup(actived, "updata") end)
end
menu_props.search_style = "palette"
menu_props.search_debounce = "submit"
menu_props.footnote = menu_items_config[actived]["footnote"] or ""
menu_props.on_search = { "script-message-to", mp.get_script_name(), "setup-danmaku-style", actived }
end
local json_props = utils.format_json(menu_props)
mp.commandv("script-message-to", "uosc", actions, json_props)
end
-- 设置弹幕源延迟菜单
function danmaku_delay_setup(source_url)
if not uosc_available then
show_message("无uosc UI框架,不支持使用该功能", 2)
return
end
local sources = {}
for url, source in pairs(danmaku.sources) do
if source.fname and not source.blocked then
local item = {title = url, value = url, keep_open = true,}
item.hint = "当前弹幕源延迟:" .. (source.delay and tostring(source.delay) or "0.0") .. "秒"
item.active = url == source_url
table.insert(sources, item)
end
end
local menu_props = {
type = "menu_delay",
title = "弹幕源延迟设置",
search_style = "disabled",
items = sources,
callback = {mp.get_script_name(), 'setup-source-delay'},
}
if source_url ~= nil then
menu_props.title = "请输入数字,单位(秒)/ 或者按照形如\"14m15s\"的格式输入分钟数加秒数"
menu_props.search_style = "palette"
menu_props.search_debounce = "submit"
menu_props.on_search = { "script-message-to", mp.get_script_name(), "setup-source-delay", source_url }
end
local json_props = utils.format_json(menu_props)
mp.commandv("script-message-to", "uosc", "open-menu", json_props)
end
-- 总集合弹幕菜单
function open_add_total_menu_uosc()
local items = {}
local total_menu_items_config = {
{ title = "弹幕搜索", action = "open_search_danmaku_menu" },
{ title = "从源添加弹幕", action = "open_add_source_menu" },
{ title = "弹幕源延迟设置", action = "open_source_delay_menu" },
{ title = "弹幕样式", action = "open_setup_danmaku_menu" },
{ title = "弹幕内容", action = "open_content_danmaku_menu" },
}
if danmaku.anime and danmaku.episode then
local episode = danmaku.episode:gsub("%s.-$","")
episode = episode:match("^(第.*[话回集]+)%s*") or episode
items[#items + 1] = {
title = string.format("已关联弹幕:%s-%s", danmaku.anime, episode),
bold = true,
italic = true,
keep_open = true,
selectable = false,
}
end
for _, config in ipairs(total_menu_items_config) do
table.insert(items, {
title = config.title,
value = { "script-message-to", mp.get_script_name(), config.action },
keep_open = false,
selectable = true,
})
end
local menu_props = {
type = "menu_total",
title = "弹幕设置",
search_style = "disabled",
items = items,
}
local json_props = utils.format_json(menu_props)
mp.commandv("script-message-to", "uosc", "open-menu", json_props)
end
function open_add_total_menu_select()
local item_titles, item_values = {}, {}
local total_menu_items_config = {
{ title = "弹幕搜索", action = "open_search_danmaku_menu" },
{ title = "从源添加弹幕", action = "open_add_source_menu" },
{ title = "弹幕内容", action = "open_content_danmaku_menu" },
}
for i, config in ipairs(total_menu_items_config) do
item_titles[i] = config.title
item_values[i] = { "script-message-to", mp.get_script_name(), config.action }
end
mp.commandv('script-message-to', 'console', 'disable')
input.select({
prompt = '选择:',
items = item_titles,
submit = function(id)
mp.commandv(unpack(item_values[id]))
end,
})
end
function open_add_total_menu()
if uosc_available then
open_add_total_menu_uosc()
elseif input_loaded then
open_add_total_menu_select()
end
end
-- 视频播放时保存弹幕
function save_danmaku_func(suffix)
-- show_message(suffix)
-- 检查 suffix 是否存在(不是 nil)并且是字符串类型
if type(suffix) == "string" then
-- 将字符串转换为小写以确保比较时不区分大小写
suffix = string.lower(suffix)
if suffix == "xml" or suffix == "ass" then
local danmaku_path = os.getenv("TEMP") or "/tmp/"
local danmaku_file = utils.join_path(danmaku_path, "danmaku-" .. pid .. ".ass")
if file_exists(danmaku_file) then
local path = mp.get_property("path")
-- 排除网络播放场景
if not path or is_protocol(path) then
show_message("此弹幕文件不支持保存至本地")
msg.verbose("This danmaku file does not support saving.")
else
local dir = get_parent_directory(path)
local filename = mp.get_property('filename/no-ext')
local danmaku_out = utils.join_path(dir, filename .. "." .. suffix)
-- show_message(danmaku_out)
if file_exists(danmaku_out) then
show_message("已存在同名弹幕文件:" .. danmaku_out)
msg.verbose("Danmaku file with the same name already exists: " .. danmaku_out)
return
else
convert_with_danmaku_factory(danmaku_file, danmaku_out)
if file_exists(danmaku_out) then
if not options.save_danmaku then
show_message("成功保存 " .. suffix .. " 弹幕文件到视频文件目录")
end
msg.verbose("成功保存 " .. suffix .. " 弹幕文件到: " .. danmaku_out)
end
end
end
else
show_message("找不到弹幕文件:" .. danmaku_file)
msg.verbose("Can't find danmaku file:" .. danmaku_file)
end
else
msg.verbose("不支持的文件后缀: " .. (suffix or "未知"))
end
else
msg.verbose("Function value undefined" .. suffix)
end
end
-- 添加 uosc 菜单栏按钮
mp.commandv(
"script-message-to",
"uosc",
"set-button",
"danmaku",
utils.format_json({
icon = "search",
tooltip = "弹幕搜索",
command = "script-message open_search_danmaku_menu",
})
)
mp.commandv(
"script-message-to",
"uosc",
"set-button",
"danmaku_source",
utils.format_json({
icon = "add_box",
tooltip = "从源添加弹幕",
command = "script-message open_add_source_menu",
})
)
mp.commandv(
"script-message-to",
"uosc",
"set-button",
"danmaku_styles",
utils.format_json({
icon = "palette",
tooltip = "弹幕样式",
command = "script-message open_setup_danmaku_menu",
})
)
mp.commandv(
"script-message-to",
"uosc",
"set-button",
"danmaku_delay",
utils.format_json({
icon = "more_time",
tooltip = "弹幕源延迟设置",
command = "script-message open_source_delay_menu",
})
)
mp.commandv(
"script-message-to",
"uosc",
"set-button",
"danmaku_menu",
utils.format_json({
icon = "grid_view",
tooltip = "弹幕设置",
command = "script-message open_add_total_menu",
})
)
mp.register_script_message('uosc-version', function()
uosc_available = true
end)
-- 视频播放时保存弹幕
mp.register_script_message("immediately_save_danmaku", function(event)
save_danmaku_func(event)
end)
-- 注册函数给 uosc 按钮使用
mp.register_script_message("open_search_danmaku_menu", open_input_menu)
mp.register_script_message("search-anime-event", function(query)
if uosc_available then
mp.commandv("script-message-to", "uosc", "close-menu", "menu_danmaku")
end
local name, class = query:match("^(.-)%s*|%s*(.-)%s*$")
if name and class then
query_extra(name, class)
else
get_animes(query)
end
end)
mp.register_script_message("search-episodes-event", function(animeTitle, episodes)
if uosc_available then
mp.commandv("script-message-to", "uosc", "close-menu", "menu_anime")
end
get_episodes(animeTitle, utils.parse_json(episodes))
end)
-- Register script message to show the input menu
mp.register_script_message("load-danmaku", function(animeTitle, episodeTitle, episodeId)
danmaku.anime = animeTitle
danmaku.episode = episodeTitle
set_episode_id(episodeId, true)
end)
mp.register_script_message("open_add_source_menu", open_add_menu)
mp.register_script_message("add-source-event", function(query)
if uosc_available then
mp.commandv("script-message-to", "uosc", "close-menu", "menu_source")
end
add_danmaku_source(query, true)
end)
mp.register_script_message("open_add_total_menu", open_add_total_menu)
mp.register_script_message("open_source_delay_menu", danmaku_delay_setup)
mp.register_script_message("open_setup_danmaku_menu", function()
if uosc_available then
mp.commandv("script-message-to", "uosc", "close-menu", "menu_total")
end
add_danmaku_setup()
end)
mp.register_script_message("open_content_danmaku_menu", function()
if uosc_available then
mp.commandv("script-message-to", "uosc", "close-menu", "menu_total")
end
open_content_menu()
end)
mp.commandv("script-message-to", "uosc", "set", "show_danmaku", "off")
mp.register_script_message("set", function(prop, value)
if prop ~= "show_danmaku" then
return
end
if value == "on" then
if comments == nil then
local path = mp.get_property("path")
init(path)
else
if danmaku.anime and danmaku.episode then
show_message("加载弹幕:" .. danmaku.anime .. "-" .. danmaku.episode.. "\\N共计" .. #comments .. "条弹幕", 3)
else
show_message("弹幕加载成功,共计" .. #comments .. "条弹幕", 3)
end
show_danmaku_func()
end
else
show_message("关闭弹幕", 2)
hide_danmaku_func()
end
mp.commandv("script-message-to", "uosc", "set", "show_danmaku", value)
end)
mp.register_script_message("show_danmaku_keyboard", function()
if not enabled then
if comments == nil then
local path = mp.get_property("path")
init(path)
else
if danmaku.anime and danmaku.episode then
show_message("加载弹幕:" .. danmaku.anime .. "-" .. danmaku.episode.. "\\N共计" .. #comments .. "条弹幕", 3)
else
show_message("弹幕加载成功,共计" .. #comments .. "条弹幕", 3)
end
show_danmaku_func()
mp.commandv("script-message-to", "uosc", "set", "show_danmaku", "on")
end
else
show_message("关闭弹幕", 2)
hide_danmaku_func()
mp.commandv("script-message-to", "uosc", "set", "show_danmaku", "off")
end
end)
mp.register_script_message("setup-danmaku-style", function(query, text)
local event = utils.parse_json(query)
if event ~= nil then
-- item点击 或 图标点击
if event.type == "activate" then
if not event.action then
if ordered_keys[event.index] == "bold" then
options.bold = options.bold == "true" and "false" or "true"
menu_items_config.bold.hint = options.bold
end
-- "updata" 模式会保留输入框文字
add_danmaku_setup(ordered_keys[event.index], "updata")
return
else
-- msg.info("event.action:" .. event.action)
options[event.action] = menu_items_config[event.action]["original"]
menu_items_config[event.action]["hint"] = options[event.action]
add_danmaku_setup(event.action, "updata")
if event.action == "density" or event.action == "scrolltime" then
load_danmaku(true)
end
end
end
else
-- 数值输入
if text == nil or text == "" then
return
end
local newText, _ = text:gsub("%s", "") -- 移除所有空白字符
if tonumber(newText) ~= nil and menu_items_config[query]["scope"] ~= nil then
local num = tonumber(newText)
local min_num = menu_items_config[query]["scope"]["min"]
local max_num = menu_items_config[query]["scope"]["max"]
if num and min_num <= num and num <= max_num then
if string.match(menu_items_config[query]["footnote"], "整数") then
-- 输入范围为整数时向下取整
num = tostring(math.floor(num))
end
options[query] = tostring(num)
menu_items_config[query]["hint"] = options[query]
-- "refresh" 模式会清除输入框文字
add_danmaku_setup(query, "refresh")
if query == "density" or query == "scrolltime" then
load_danmaku(true, true)
end
return
end
end
add_danmaku_setup(query, "error")
end
end)
mp.register_script_message('setup-danmaku-source', function(json)
local event = utils.parse_json(json)
if event.type == 'activate' then
if event.action == "delete" then
local rm = danmaku.sources[event.value]["fname"]
if rm and file_exists(rm) and danmaku.sources[event.value]["from"] ~= "user_local" then
os.remove(rm)
end
danmaku.sources[event.value] = nil
remove_source_from_history(event.value)
mp.commandv("script-message-to", "uosc", "close-menu", "menu_source")
open_add_menu_uosc()
load_danmaku(true)
end
if event.action == "block" then
danmaku.sources[event.value]["blocked"] = true
add_source_to_history(event.value, danmaku.sources[event.value])
mp.commandv("script-message-to", "uosc", "close-menu", "menu_source")
open_add_menu_uosc()
load_danmaku(true)
end
if event.action == "unblock" then
danmaku.sources[event.value]["blocked"] = false
if danmaku.sources[event.value]["delay"] then
add_source_to_history(event.value, danmaku.sources[event.value])
else
remove_source_from_history(event.value)
end
mp.commandv("script-message-to", "uosc", "close-menu", "menu_source")
open_add_menu_uosc()
load_danmaku(true)
end
end
end)
mp.register_script_message("setup-source-delay", function(query, text)
local event = utils.parse_json(query)
if event ~= nil then
-- item点击
if event.type == "activate" then
danmaku_delay_setup(event.value)
end
else
-- 数值输入
if text == nil or text == "" then
return
end
local newText, _ = text:gsub("%s", "") -- 移除所有空白字符
if tonumber(newText) ~= nil then
local num = tonumber(newText)
danmaku.sources[query]["delay"] = tostring(num)
add_source_to_history(query, danmaku.sources[query])
mp.commandv("script-message-to", "uosc", "close-menu", "menu_delay")
danmaku_delay_setup(query)
load_danmaku(true, true)
elseif newText:match("^%-?%d+m%d+s$") then
local minutes, seconds = string.match(newText, "^(%-?%d+)m(%d+)s$")
minutes = tonumber(minutes)
seconds = tonumber(seconds)
if minutes < 0 then seconds = -seconds end
danmaku.sources[query]["delay"] = tostring(60 * minutes + seconds)
add_source_to_history(query, danmaku.sources[query])
mp.commandv("script-message-to", "uosc", "close-menu", "menu_delay")
danmaku_delay_setup(query)
load_danmaku(true, true)
end
end
end)