MCPcopy
hub / github.com/ocsjs/ocsjs / playMedia

Function playMedia

packages/scripts/src/utils/index.ts:79–117  ·  view source on GitHub ↗
(playFunction: () => Promise<void> | undefined | void)

Source from the content-addressed store, hash-verified

77
78// 有些网课会改变 media.play 方法,所以可能不是一个 promise
79export async function playMedia(playFunction: () => Promise<void> | undefined | void): Promise<boolean> {
80 // 尝试播放
81 const tryPlayMedia = () => {
82 return new Promise<void>((resolve, reject) => {
83 try {
84 const playRes = playFunction();
85 if (playRes) {
86 playRes.then(resolve).catch(reject);
87 } else {
88 resolve();
89 }
90 } catch (err) {
91 reject(err);
92 }
93 });
94 };
95
96 try {
97 await tryPlayMedia();
98 return true;
99 } catch (err) {
100 console.error(err);
101 if (String(err).includes(`failed because the user didn't interact with the document first`)) {
102 $modal.alert({
103 content:
104 '播放音视频失败,由于浏览器的用户隐私保护措施,如果要播放带有音量的视频,或者某些无法自动播放音视频的网站,您必须先点击一次页面上的任意位置脚本才能进行音视频的播放,后续无需重新点击。',
105 onClose: async () => {
106 await tryPlayMedia();
107 }
108 });
109 return true;
110 } else if (String(err).includes('The element has no supported sources')) {
111 $console.error('当前视频无法播放。');
112 } else {
113 $console.error('播放视频时发生未知错误:' + String(err));
114 }
115 return false;
116 }
117}
118
119/**
120 * 解除复制限制功能

Callers 12

mediaFunction · 0.90
watchMediaFunction · 0.90
studyFunction · 0.90
watchMediaFunction · 0.90
watchMediaFunction · 0.90
doWorkFunction · 0.90
doJobFunction · 0.90
watchFunction · 0.90
watchXnkFunction · 0.90
ai_watchFunction · 0.90
v2_watchFunction · 0.90
jobFunction · 0.90

Calls 1

tryPlayMediaFunction · 0.85

Tested by

no test coverage detected