-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodex-background.ps1
More file actions
1387 lines (1228 loc) · 53.3 KB
/
Copy pathcodex-background.ps1
File metadata and controls
1387 lines (1228 loc) · 53.3 KB
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
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
[CmdletBinding()]
param(
# 背景模式:image(固定单图)/ random(目录混合随机)/ video(目录随机视频)。
[ValidateSet("image", "random", "video")]
[string]$BackgroundMode = "random",
# 固定图片路径(image 模式用);默认指向脚本同级的 assets 示例图。
[string]$ImagePath = (Join-Path $PSScriptRoot "assets\sample-background.jpg"),
# 媒体目录(random / video 模式用,图片视频可混放同一目录);默认指向脚本同级 assets。
[string]$MediaDirectory = (Join-Path $PSScriptRoot "assets"),
# 固定视频路径(可选;image 模式下想直接放视频时用)。
[string]$VideoPath,
# 运行时轮换间隔(秒),默认 60 分钟;0 表示不轮换,仅在启动时随机一次。
[ValidateRange(0, 86400)]
[int]$RotateInterval = 3600,
# 覆盖层透明度(兜底默认值);图片/视频可分别用下面两个参数覆盖。
[ValidateRange(0.01, 1.0)]
[double]$Opacity = 0.15,
# 图片背景透明度;未指定(<=0)时回退到 $Opacity。
[ValidateRange(0, 1.0)]
[double]$ImageOpacity = 0,
# 视频背景透明度;未指定(<=0)时回退到 $Opacity。
# 默认 0.2(比图片 0.15 略高,视频动态内容需要更明显)。
[ValidateRange(0, 1.0)]
[double]$VideoOpacity = 0.2,
# Codex 桌面应用的 CDP 调试端口(由 Codex++ launcher 在激活 Codex 时附加)。
[ValidateRange(1, 65535)]
[int]$DebugPort = 9229,
# Codex++ 主程序(codex-plus-plus.exe)路径,用于方案 C 启动 Codex。
# 留空时自动探测本机 Codex++ 安装位置。
[string]$CodexPlusLauncherPath = "",
# 是否压制 Codex++ 的静态背景图覆盖层(codex-plus-image-overlay)。
# Codex++ 背景图默认应在 Codex++ 设置里关闭;本开关作保险,应对升级/配置重置导致诈尸。
[switch]$SuppressCodexPlus,
# 不启动 Codex++/Codex,只连接已在跑的 CDP 端口注入(适合 Codex 已开的情况)。
[switch]$NoLaunch,
# Codex MSIX 应用的 AUMID(无 Codex++ 时用于自激活 Codex)。留空则自动探测。
[string]$CodexAumid = "",
# 仅验证参数和资源,不连接或启动 Codex。
[switch]$ValidateOnly
)
$ErrorActionPreference = "Stop"
# ============================================================
# 读取 config.json:未显式传命令行参数时用 config 覆盖脚本默认值。
# 优先级:命令行参数 > config.json > 脚本默认值。
# 这样快捷方式无需携带参数(避免 .lnk 属性保存截断),配置由 config.json 提供。
# ============================================================
$configPath = Join-Path $PSScriptRoot "config.json"
if (Test-Path -LiteralPath $configPath -PathType Leaf) {
try {
$config = Get-Content -LiteralPath $configPath -Raw -Encoding UTF8 | ConvertFrom-Json -AsHashtable
# 白名单:只认这些配置项,避免未知键污染变量。
$configKeys = @("BackgroundMode", "ImagePath", "MediaDirectory", "VideoPath", "RotateInterval", "Opacity", "ImageOpacity", "VideoOpacity")
foreach ($key in $configKeys) {
# 只在 config 有该键、且命令行未显式传该参数时,才用 config 值覆盖默认。
if ($config.ContainsKey($key) -and -not $PSBoundParameters.ContainsKey($key)) {
Set-Variable -Name $key -Value $config[$key] -Scope Script
}
}
}
catch {
Write-Warning "config.json 解析失败,已忽略(使用默认值):$($_.Exception.Message)"
}
}
# 支持的媒体扩展名分类。
$script:ImageExtensions = @(".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp")
$script:VideoExtensions = @(".mp4", ".webm", ".mov", ".mkv", ".avi")
function Get-MediaMimeType {
param(
[Parameter(Mandatory)]
[string]$Path
)
switch ([IO.Path]::GetExtension($Path).ToLowerInvariant()) {
".jpg" { return "image/jpeg" }
".jpeg" { return "image/jpeg" }
".png" { return "image/png" }
".gif" { return "image/gif" }
".webp" { return "image/webp" }
".bmp" { return "image/bmp" }
".mp4" { return "video/mp4" }
".webm" { return "video/webm" }
".mov" { return "video/quicktime" }
".mkv" { return "video/x-matroska" }
".avi" { return "video/x-msvideo" }
default { throw "不支持的媒体格式:$([IO.Path]::GetExtension($Path))" }
}
}
function Get-MediaType {
param(
[Parameter(Mandatory)]
[string]$Path
)
$ext = [IO.Path]::GetExtension($Path).ToLowerInvariant()
if ($script:ImageExtensions -contains $ext) { return "image" }
if ($script:VideoExtensions -contains $ext) { return "video" }
return $null
}
function Get-RandomMediaFromDirectory {
param(
[Parameter(Mandatory)]
[string]$Directory,
[Parameter(Mandatory)]
[ValidateSet("random", "video")]
[string]$Mode
)
$files = @(Get-ChildItem -LiteralPath $Directory -File -ErrorAction Stop)
$imagePool = @()
$videoPool = @()
foreach ($f in $files) {
$type = Get-MediaType -Path $f.FullName
if (-not $type) { continue }
$entry = [pscustomobject]@{
Path = $f.FullName
Type = $type
FileName = $f.Name
}
if ($type -eq "video") { $videoPool += $entry }
else { $imagePool += $entry }
}
if ($Mode -eq "video") {
if ($videoPool.Count -eq 0) {
throw "媒体目录中没有可用的视频文件:$Directory"
}
return ($videoPool | Get-Random)
}
# random 模式:图片视频 1:1 比例(先 50/50 选类型,再从对应池随机选一个)。
if ($imagePool.Count -eq 0 -and $videoPool.Count -eq 0) {
throw "媒体目录中没有可用的媒体文件:$Directory"
}
if ($imagePool.Count -eq 0) { return ($videoPool | Get-Random) }
if ($videoPool.Count -eq 0) { return ($imagePool | Get-Random) }
if ((Get-Random -Maximum 2) -eq 0) {
return ($imagePool | Get-Random)
}
return ($videoPool | Get-Random)
}
function Test-CdpAvailable {
param(
[Parameter(Mandatory)]
[int]$Port,
[int]$TimeoutSeconds = 2,
# 连续失败多少次才判定端口不可用(容忍短暂抖动/大文件传输期间的响应变慢)。
[int]$RequiredFailures = 3
)
# 单次探测:不只是 HTTP 通就行,还要验证返回的确实是 CDP(有 webSocketDebuggerUrl 字段)。
# 避免端口被别的程序占用时拿到非 CDP 响应却误判为可用。
$tries = if ($RequiredFailures -gt 1) { $RequiredFailures } else { 1 }
for ($i = 1; $i -le $tries; $i++) {
try {
$resp = Invoke-RestMethod -Uri "http://127.0.0.1:$Port/json/version" -Method Get -TimeoutSec $TimeoutSeconds
# CDP /json/version 必含 webSocketDebuggerUrl;非 CDP 服务不会返回这个字段。
if ($resp -and $resp.webSocketDebuggerUrl) {
return $true
}
# HTTP 通但不是 CDP(端口被其他程序占用)——视为不可用,不重试。
return $false
}
catch {
if ($i -lt $tries) { Start-Sleep -Milliseconds 500 }
}
}
return $false
}
function Find-CodexCdpPort {
# 从正在运行的 Codex/ChatGPT 进程命令行解析 --remote-debugging-port=NNNN。
# CDP host 进程名随版本变化:旧版是 Codex.exe,新版 26.707+ 改名 ChatGPT.exe。
# 新版 Codex++ 不再固定用 9229,而是动态端口(如 10373),必须实时探测。
# 返回端口数字;找不到返回 $null。
# ⚠️ 绝对不能返回 9230——那是 ZCode 的 CDP 端口,连上会把背景注入到 ZCode!
try {
$procs = Get-CimInstance Win32_Process -ErrorAction Stop | Where-Object {
$_.Name -in @('Codex.exe', 'ChatGPT.exe') -and $_.CommandLine -match 'remote-debugging-port=(\d+)'
}
foreach ($p in $procs) {
if ($p.CommandLine -match 'remote-debugging-port=(\d+)') {
$port = [int]$Matches[1]
if ($port -eq 9230) { continue } # 排除 ZCode 端口
# 确认该端口确实在监听(避免解析到子进程的无效端口)。
if (Test-CdpAvailable -Port $port -TimeoutSeconds 1 -RequiredFailures 1) {
return $port
}
}
}
} catch {
# Get-CimInstance 失败(如系统页面文件不足)→ 兜底:扫描常见 CDP 端口。
# 注意:不包含 9230(ZCode 专用,连上会误注入 ZCode 页面)。
Write-Warning "进程命令行查询失败,尝试扫描常见 CDP 端口..."
foreach ($candidatePort in @(9229, 3713, 2604, 6363, 10373)) {
if (Test-CdpAvailable -Port $candidatePort -TimeoutSeconds 1 -RequiredFailures 1) {
return $candidatePort
}
}
}
return $null
}
function Find-CodexPlusLauncher {
# 自动探测本机 Codex++ 主程序(codex-plus-plus.exe)路径。
$commonCandidates = @(
(Join-Path $env:LOCALAPPDATA "Programs\Codex++\codex-plus-plus.exe"),
(Join-Path $env:LOCALAPPDATA "Programs\Codex++\Codex++.exe")
)
foreach ($candidate in $commonCandidates) {
if (Test-Path -LiteralPath $candidate -PathType Leaf) {
return $candidate
}
}
$regRoots = @(
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
"HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall",
"HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
)
foreach ($root in $regRoots) {
if (-not (Test-Path $root)) { continue }
$entries = Get-ChildItem $root -ErrorAction SilentlyContinue | ForEach-Object {
Get-ItemProperty $_.PSPath -ErrorAction SilentlyContinue
} | Where-Object { $_.DisplayName -match "Codex\+\+" }
foreach ($entry in $entries) {
foreach ($propName in @("InstallLocation", "DisplayIcon", "UninstallString")) {
$val = $entry.$propName
if (-not $val) { continue }
$cleaned = $val.Trim('"').Trim()
$dir = if (Test-Path $cleaned -PathType Container) { $cleaned }
elseif (Test-Path $cleaned -PathType Leaf) { Split-Path -Parent $cleaned }
else {
$firstToken = ($cleaned -split '\s+')[0].Trim('"')
if (Test-Path $firstToken -PathType Leaf) { Split-Path -Parent $firstToken }
}
if ($dir) {
$candidate = Join-Path $dir "codex-plus-plus.exe"
if (Test-Path -LiteralPath $candidate -PathType Leaf) {
return $candidate
}
}
}
}
}
$lnkDirs = @(
[Environment]::GetFolderPath("Desktop"),
[Environment]::GetFolderPath("CommonDesktopDirectory"),
(Join-Path $env:APPDATA "Microsoft\Windows\Start Menu\Programs"),
(Join-Path $env:ProgramData "Microsoft\Windows\Start Menu\Programs")
)
foreach ($dir in $lnkDirs) {
if (-not (Test-Path $dir)) { continue }
$lnks = Get-ChildItem $dir -Recurse -Filter "*.lnk" -ErrorAction SilentlyContinue |
Where-Object { $_.Name -match "Codex\+\+" }
foreach ($lnk in $lnks) {
try {
$sh = New-Object -ComObject WScript.Shell
$target = $sh.CreateShortcut($lnk.FullName).TargetPath
[void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($sh)
if ($target -and $target -match "codex-plus-plus\.exe$" -and (Test-Path -LiteralPath $target -PathType Leaf)) {
return $target
}
} catch {}
}
}
return $null
}
function Start-CodexViaLauncher {
param(
[Parameter(Mandatory)]
[string]$LauncherPath
)
Write-Host "正在通过 Codex++ 启动 Codex:$LauncherPath"
return Start-Process -FilePath $LauncherPath
}
function Stop-CodexProcesses {
# 优雅关闭当前所有 Codex 主进程(自激活前需重启 Codex 以附加 CDP 端口)。
# 只杀名为 Codex 的进程,不动其他程序。
$processes = @(Get-Process -Name "Codex" -ErrorAction SilentlyContinue)
if ($processes.Count -eq 0) { return }
Write-Host "正在关闭 Codex(重启以开启调试端口)..."
foreach ($process in $processes) {
if ($process.MainWindowHandle -ne 0) {
[void]$process.CloseMainWindow()
}
}
$deadline = [DateTime]::UtcNow.AddSeconds(8)
do {
Start-Sleep -Milliseconds 250
$remaining = @(Get-Process -Name "Codex" -ErrorAction SilentlyContinue)
} while ($remaining.Count -gt 0 -and [DateTime]::UtcNow -lt $deadline)
if ($remaining.Count -gt 0) {
$remaining | Stop-Process -Force -ErrorAction SilentlyContinue
Wait-Process -Id $remaining.Id -Timeout 10 -ErrorAction SilentlyContinue
}
}
function Get-CodexAumid {
param(
# 可选手动指定 AUMID,留空则自动探测。
[string]$Aumid = ""
)
if (-not [string]::IsNullOrWhiteSpace($Aumid)) {
return $Aumid
}
# 动态探测:Get-AppxPackage 取 PackageFamilyName,拼成 AUMID(PackageFamilyName!App)。
$pkg = Get-AppxPackage -Name "OpenAI.Codex" -ErrorAction SilentlyContinue | Select-Object -First 1
if (-not $pkg -or -not $pkg.PackageFamilyName) {
throw "未找到 OpenAI.Codex MSIX 包。请从 Microsoft Store 安装 Codex 桌面版。"
}
return ($pkg.PackageFamilyName + "!App")
}
function Start-CodexViaMsix {
# 无 Codex++ 时的回退启动:用 COM IApplicationActivationManager 激活 Codex MSIX,
# 并附加 --remote-debugging-port 让 Codex 自己开启 CDP 端口(不依赖 Codex++)。
# CDP 端口只在进程启动时生效,故必须先关闭已运行的 Codex 再重新激活(会中断当前会话)。
param(
[Parameter(Mandatory)]
[int]$Port,
[string]$Aumid = ""
)
$resolvedAumid = Get-CodexAumid -Aumid $Aumid
# 先关闭已运行的 Codex(CDP 参数只在启动时生效)。
Stop-CodexProcesses
Start-Sleep -Milliseconds 500
# COM 激活的 C# 代码(GUID/CLSID 来自 IApplicationActivationManager 标准定义)。
$activationCode = @'
using System;
using System.Runtime.InteropServices;
[Flags]
public enum ActivateOptions {
None = 0x00000000,
DesignMode = 0x00000001,
NoErrorUI = 0x00000002,
NoSplashScreen = 0x00000004
}
[ComImport]
[Guid("2e941141-7f97-4756-ba1d-9decde894a3d")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IApplicationActivationManager {
int ActivateApplication(
[MarshalAs(UnmanagedType.LPWStr)] string appUserModelId,
[MarshalAs(UnmanagedType.LPWStr)] string arguments,
ActivateOptions options,
out UInt32 processId);
}
[ComImport]
[Guid("45BA127D-10A8-46EA-8AB7-56EA9078943C")]
class ApplicationActivationManager {}
public static class CodexMsixActivator {
public static UInt32 Activate(string aumid, string arguments) {
var manager = (IApplicationActivationManager)new ApplicationActivationManager();
UInt32 pid;
int hr = manager.ActivateApplication(aumid, arguments, ActivateOptions.None, out pid);
Marshal.ThrowExceptionForHR(hr);
return pid;
}
}
'@
# 避免重复 Add-Type(同一 AppDomain 内只能定义一次)。
if (-not ("CodexMsixActivator" -as [type])) {
Add-Type -TypeDefinition $activationCode -Language CSharp
}
# 激活参数:开启 CDP 端口 + 允许回环 origin(Chromium 安全校验)。
$arguments = "--remote-debugging-port=$Port --remote-allow-origins=*"
Write-Host "正在直接激活 Codex(MSIX):$resolvedAumid"
$codexPid = [CodexMsixActivator]::Activate($resolvedAumid, $arguments)
Write-Host "Codex 已激活(PID:$codexPid),等待 CDP 端口 $Port ..."
}
function Wait-CdpTargets {
param(
[Parameter(Mandatory)]
[int]$Port,
[int]$TimeoutSeconds = 90
)
$endpoint = "http://127.0.0.1:$Port/json/list"
$deadline = [DateTime]::UtcNow.AddSeconds($TimeoutSeconds)
$lastError = $null
do {
try {
$rawTargets = Invoke-RestMethod -Uri $endpoint -Method Get -TimeoutSec 2
if ($rawTargets -isnot [System.Array]) {
$rawTargets = @($rawTargets)
}
else {
$rawTargets = @($rawTargets)
}
# Codex 上有两个 page:主窗口 index.html 与头像浮窗 avatar-overlay。
# 只注入主窗口,浮窗排除。用 List 显式收集,避免 @(...|Where) 单元素时枚举属性的陷阱。
$injectableTargets = [Collections.Generic.List[object]]::new()
foreach ($t in $rawTargets) {
if ($t.webSocketDebuggerUrl -and
$t.type -eq "page" -and
$t.url -notmatch "avatar-overlay" -and
$t.title -notmatch "DevTools") {
$injectableTargets.Add($t)
}
}
if ($injectableTargets.Count -gt 0) {
return ,$injectableTargets
}
}
catch {
$lastError = $_.Exception.Message
}
Start-Sleep -Milliseconds 300
} while ([DateTime]::UtcNow -lt $deadline)
$detail = if ($lastError) { ";最后错误:$lastError" } else { "" }
throw "等待 Codex CDP 页面超时:$endpoint$detail"
}
function Invoke-CdpCommand {
param(
[Parameter(Mandatory)]
[string]$WebSocketUrl,
[Parameter(Mandatory)]
[string]$Method,
$Parameters = @{},
[int]$CommandId = 1
)
# Chromium 对同一 CDP target 的 WebSocket 快速重连偶发拒绝(HTTP 500 而非 101)。
# 这里对连接阶段做有限重试,发命令/收响应阶段不重试(避免重复执行)。
$maxAttempts = 3
$socket = $null
$cancellation = [Threading.CancellationTokenSource]::new([TimeSpan]::FromSeconds(30))
try {
$connected = $false
for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) {
$socket = [Net.WebSockets.ClientWebSocket]::new()
try {
$socket.ConnectAsync([Uri]$WebSocketUrl, $cancellation.Token).GetAwaiter().GetResult()
$connected = $true
break
}
catch {
if ($socket) { try { $socket.Dispose() } catch {} }
$socket = $null
if ($attempt -lt $maxAttempts) {
Start-Sleep -Milliseconds (300 * $attempt)
}
else {
throw
}
}
}
if (-not $connected) {
throw "无法建立到 $WebSocketUrl 的 CDP WebSocket 连接(重试 $maxAttempts 次均失败)。"
}
$payload = [ordered]@{
id = $CommandId
method = $Method
params = $Parameters
} | ConvertTo-Json -Compress -Depth 20
$payloadBytes = [Text.Encoding]::UTF8.GetBytes($payload)
$payloadSegment = [ArraySegment[byte]]::new($payloadBytes)
$socket.SendAsync(
$payloadSegment,
[Net.WebSockets.WebSocketMessageType]::Text,
$true,
$cancellation.Token
).GetAwaiter().GetResult()
do {
$stream = [IO.MemoryStream]::new()
try {
do {
$buffer = [byte[]]::new(65536)
$bufferSegment = [ArraySegment[byte]]::new($buffer)
$receiveResult = $socket.ReceiveAsync(
$bufferSegment,
$cancellation.Token
).GetAwaiter().GetResult()
if ($receiveResult.MessageType -eq [Net.WebSockets.WebSocketMessageType]::Close) {
throw "CDP WebSocket 在返回命令结果前关闭。"
}
$stream.Write($buffer, 0, $receiveResult.Count)
} while (-not $receiveResult.EndOfMessage)
$responseText = [Text.Encoding]::UTF8.GetString($stream.ToArray())
$response = $responseText | ConvertFrom-Json
}
finally {
$stream.Dispose()
}
} while ($response.id -ne $CommandId)
if ($response.error) {
throw "CDP 命令失败 [$Method]:$($response.error.message)"
}
return $response.result
}
finally {
if ($socket) {
if ($socket.State -eq [Net.WebSockets.WebSocketState]::Open) {
try {
$socket.CloseAsync(
[Net.WebSockets.WebSocketCloseStatus]::NormalClosure,
"done",
[Threading.CancellationToken]::None
).GetAwaiter().GetResult()
}
catch {}
}
try { $socket.Dispose() } catch {}
}
$cancellation.Dispose()
}
}
function New-OverlayJavaScript {
param(
[Parameter(Mandatory)]
[ValidateRange(0.01, 1.0)]
[double]$ImageOpacityValue,
[Parameter(Mandatory)]
[ValidateRange(0.01, 1.0)]
[double]$VideoOpacityValue,
[switch]$SuppressCodexPlus
)
$imageOpacityLiteral = $ImageOpacityValue.ToString(
"0.################",
[Globalization.CultureInfo]::InvariantCulture
)
$videoOpacityLiteral = $VideoOpacityValue.ToString(
"0.################",
[Globalization.CultureInfo]::InvariantCulture
)
# 压制 Codex++ 静态背景图(保险开关)。
$suppressBlock = if ($SuppressCodexPlus) {
@'
const CODEX_PLUS_OVERLAY_ID = "codex-plus-image-overlay";
function suppressCodexPlusOverlay() {
const el = document.getElementById(CODEX_PLUS_OVERLAY_ID);
if (el) el.remove();
}
suppressCodexPlusOverlay();
try {
new MutationObserver(suppressCodexPlusOverlay)
.observe(document.documentElement, { childList: true, subtree: true });
} catch (e) { /* observer 失败不影响主背景 */ }
'@
}
else {
""
}
# Codex 仅允许 blob: 作为此注入场景的可靠媒体源。
# 所有媒体都由页面端分块解码后创建 Blob,避免 dataURL 的整文件字符串副本。
return @"
(() => {
const overlayId = "cz-bg-rotator-overlay";
const opacityByType = { image: "$imageOpacityLiteral", video: "$videoOpacityLiteral" };
// 新脚本覆盖旧注入时,优先让旧 rotator 回收自己的候选层和 Blob。
const previousRotator = window.__czBgRotator;
if (previousRotator && typeof previousRotator.dispose === "function") {
try { previousRotator.dispose(); } catch (error) {}
}
// 持续清理改名前(codex-bg-rotator-overlay)的残留元素。
// 旧版注册的 addScriptToEvaluateOnNewDocument 会在每次页面加载时重建旧 overlay,
// 单次清理不够,必须用 MutationObserver 持续清除,否则多层背景叠加。
const LEGACY_OVERLAY_ID = "codex-bg-rotator-overlay";
function removeLegacyOverlay() {
const el = document.getElementById(LEGACY_OVERLAY_ID);
if (el) {
try {
if (el.tagName === "VIDEO") { el.pause(); }
el.removeAttribute("src");
el.remove();
} catch (error) {}
}
}
removeLegacyOverlay();
try {
new MutationObserver(removeLegacyOverlay)
.observe(document.documentElement, { childList: true, subtree: true });
} catch (error) {}
// 兼容旧版本没有 dispose 的场景,避免遗留背景层继续引用旧 Blob。
const legacyOverlay = document.getElementById(overlayId);
if (legacyOverlay) {
const legacyUrl = legacyOverlay.src || "";
removeMediaElement(legacyOverlay);
if (legacyUrl.startsWith("blob:")) {
try { URL.revokeObjectURL(legacyUrl); } catch (error) {}
}
}
// activeBlobUrl 只指向已经展示的背景;候选资源单独跟踪,避免抢占时误释放当前背景。
let activeBlobUrl = "";
let activeElement = null;
let candidateBlobUrl = "";
let candidateElement = null;
let installToken = 0;
const chunkBuffers = Object.create(null);
function opacityFor(type) {
return opacityByType[type] || opacityByType.image;
}
// 仅回收本 rotator 创建的 Blob URL,重复调用也不会影响页面其他资源。
function revokeBlobUrl(url) {
if (typeof url === "string" && url.startsWith("blob:")) {
try { URL.revokeObjectURL(url); } catch (error) {}
}
}
function createElement(type) {
let el;
if (type === "video") {
el = document.createElement("video");
el.loop = true;
el.muted = true;
el.defaultMuted = true;
el.autoplay = true;
el.setAttribute("playsinline", "");
el.setAttribute("webkit-playsinline", "");
el.setAttribute("aria-hidden", "true");
} else {
el = document.createElement("img");
el.alt = "";
el.setAttribute("aria-hidden", "true");
}
const commonStyle = {
position: "fixed",
inset: "0",
width: "100vw",
height: "100vh",
objectFit: "cover",
objectPosition: "center center",
opacity: opacityFor(type),
pointerEvents: "none",
zIndex: "2147483646",
userSelect: "none"
};
for (const k in commonStyle) {
el.style[k] = commonStyle[k];
}
return el;
}
// 停止并移除媒体元素,使浏览器可以立刻释放其对应的解码与网络资源。
function removeMediaElement(el) {
if (!el) return;
try {
const tagName = el.tagName.toLowerCase();
if (tagName === "video") {
el.pause();
}
// 图片和视频都先解除 src,再移除节点,避免旧资源继续被元素引用。
el.removeAttribute("src");
if (tagName === "video") {
el.load();
}
el.remove();
} catch (error) {}
}
// 隐藏窗口被 Chromium 暂停后,重新可见或获得焦点时恢复当前活动视频。
function resumeActiveVideo() {
const video = activeElement || document.getElementById(overlayId);
if (!video || video.tagName.toLowerCase() !== "video") return;
try {
video.muted = true;
video.defaultMuted = true;
const playback = video.play();
if (playback && typeof playback.catch === "function") {
playback.catch(() => {});
}
} catch (error) {}
}
function onVisibilityChange() {
if (!document.hidden) {
resumeActiveVideo();
}
}
document.addEventListener("visibilitychange", onVisibilityChange);
window.addEventListener("focus", resumeActiveVideo);
// 候选背景尚未展示时可直接销毁,当前活动背景始终保持不动。
function discardCandidate() {
const element = candidateElement;
const url = candidateBlobUrl;
candidateElement = null;
candidateBlobUrl = "";
removeMediaElement(element);
revokeBlobUrl(url);
}
// CDP 传入的是单块 Base64;在本次调用内立即转换为二进制,避免字符串长期驻留。
function decodeBase64Chunk(base64Chunk) {
const binary = atob(base64Chunk);
const bytes = new Uint8Array(binary.length);
for (let index = 0; index < binary.length; index++) {
bytes[index] = binary.charCodeAt(index);
}
return bytes;
}
// 图片等待 load,视频等待获得可播放帧并且静音播放成功;超时或失败时保留旧背景。
function waitForCandidate(element, type, blobUrl) {
return new Promise((resolve) => {
let settled = false;
const finish = (success) => {
if (settled) return;
settled = true;
clearTimeout(timeoutId);
element.removeEventListener("load", onImageLoad);
element.removeEventListener("loadeddata", onVideoData);
element.removeEventListener("error", onError);
resolve(success);
};
const onImageLoad = () => finish(true);
const onVideoData = () => {
try {
element.muted = true;
element.defaultMuted = true;
const playback = element.play();
if (playback && typeof playback.then === "function") {
playback.then(() => finish(true)).catch(() => finish(false));
} else {
finish(true);
}
} catch (error) {
finish(false);
}
};
const onError = () => finish(false);
const timeoutId = setTimeout(() => finish(false), 15000);
element.addEventListener("error", onError, { once: true });
if (type === "video") {
element.addEventListener("loadeddata", onVideoData, { once: true });
} else {
element.addEventListener("load", onImageLoad, { once: true });
}
element.src = blobUrl;
});
}
// 只有候选层准备就绪后才替换活动背景,避免轮换期间出现空白。
async function stageBlob(blobUrl, type) {
const root = document.documentElement;
if (!root) {
revokeBlobUrl(blobUrl);
return false;
}
const token = ++installToken;
discardCandidate();
const candidate = createElement(type);
candidate.style.opacity = "0";
candidateElement = candidate;
candidateBlobUrl = blobUrl;
root.appendChild(candidate);
const ready = await waitForCandidate(candidate, type, blobUrl);
if (token !== installToken || !ready) {
if (candidateElement === candidate) {
discardCandidate();
} else {
removeMediaElement(candidate);
revokeBlobUrl(blobUrl);
}
return false;
}
const oldElement = activeElement;
const oldBlobUrl = activeBlobUrl;
candidate.style.opacity = opacityFor(type);
removeMediaElement(oldElement);
candidate.id = overlayId;
activeElement = candidate;
activeBlobUrl = blobUrl;
candidateElement = null;
candidateBlobUrl = "";
revokeBlobUrl(oldBlobUrl);
resumeActiveVideo();
return true;
}
const rotator = {
// 创建一条媒体传输的二进制块缓冲;同 id 重入时先释放旧缓冲。
beginChunkedMedia(id, mime, type) {
if (!id || !mime || (type !== "image" && type !== "video")) return false;
rotator.abortChunkedMedia(id);
chunkBuffers[id] = { mime: mime, type: type, parts: [] };
return true;
},
// 每块在本调用内解码,chunkBuffers 只持有原始字节而不持有完整 Base64 文本。
appendChunk(id, base64Chunk) {
const buffer = chunkBuffers[id];
if (!buffer) return false;
try {
buffer.parts.push(decodeBase64Chunk(base64Chunk));
return true;
} catch (error) {
rotator.abortChunkedMedia(id);
return false;
}
},
// Blob 构造完成后立即清空分块数组,后续由候选层负责接管或回收 Blob URL。
async finalizeChunkedMedia(id) {
const buffer = chunkBuffers[id];
if (!buffer) return false;
delete chunkBuffers[id];
let blobUrl = "";
try {
const blob = new Blob(buffer.parts, { type: buffer.mime });
buffer.parts.length = 0;
blobUrl = URL.createObjectURL(blob);
return await stageBlob(blobUrl, buffer.type);
} catch (error) {
buffer.parts.length = 0;
revokeBlobUrl(blobUrl);
return false;
}
},
// PowerShell 传输中断时显式释放尚未 finalize 的原始字节块。
abortChunkedMedia(id) {
const buffer = chunkBuffers[id];
if (!buffer) return false;
buffer.parts.length = 0;
delete chunkBuffers[id];
return true;
},
// 重复注入或页面卸载前统一回收候选、活动背景和所有未完成传输。
dispose() {
installToken++;
document.removeEventListener("visibilitychange", onVisibilityChange);
window.removeEventListener("focus", resumeActiveVideo);
Object.keys(chunkBuffers).forEach((id) => rotator.abortChunkedMedia(id));
discardCandidate();
const element = activeElement || document.getElementById(overlayId);
const url = activeBlobUrl || (element && element.src) || "";
activeElement = null;
activeBlobUrl = "";
removeMediaElement(element);
revokeBlobUrl(url);
},
version: "2.0"
};
window.__czBgRotator = rotator;
$suppressBlock
return true;
})();
"@
}
function Install-CodexBackground {
param(
[Parameter(Mandatory)]
$Targets,
[Parameter(Mandatory)]
[string]$JavaScript
)
if ($Targets -is [array]) {
$targetList = @($Targets)
}
else {
$targetList = @($Targets)
}
$successCount = 0
$failureMessages = [Collections.Generic.List[string]]::new()
foreach ($target in $targetList) {
$wsUrl = [string]$target.webSocketDebuggerUrl
$targetTitle = [string]$target.title
$targetUrl = [string]$target.url
if ([string]::IsNullOrWhiteSpace($wsUrl)) {
$failureMessages.Add("$targetTitle:webSocketDebuggerUrl 为空")
continue
}
try {
# 新文档注册保证页面刷新或导航后仍会重新创建背景层。
Invoke-CdpCommand `
-WebSocketUrl $wsUrl `
-Method "Page.addScriptToEvaluateOnNewDocument" `
-Parameters @{ source = $JavaScript } `
-CommandId 1 | Out-Null
# 当前文档不会触发上面的注册脚本,因此需要立即执行一次。
Invoke-CdpCommand `
-WebSocketUrl $wsUrl `
-Method "Runtime.evaluate" `
-Parameters @{
expression = $JavaScript
returnByValue = $true
awaitPromise = $true
} `
-CommandId 2 | Out-Null
$successCount++
Write-Host "已注入页面:$targetTitle ($targetUrl)"
}
catch {
$failureMessages.Add("$targetTitle:$($_.Exception.Message)")
}
}
if ($successCount -eq 0) {
throw "未能向任何 Codex 页面注入背景。$($failureMessages -join ';')"
}
if ($failureMessages.Count -gt 0) {
Write-Warning "部分页面注入失败:$($failureMessages -join ';')"
}
return $successCount
}
function Send-MediaToPage {
# 通过 CDP 向页面流式推送新媒体;PowerShell 端始终只保留一个原始字节块。
param(
[Parameter(Mandatory)]
[string]$WebSocketUrl,
[Parameter(Mandatory)]