tronclass-auto 1.4.4 → 1.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/video.js +44 -24
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tronclass-auto",
3
- "version": "1.4.4",
3
+ "version": "1.4.6",
4
4
  "description": "自動觀看 eclass/TronClass 影片、填寫表單的 CLI 工具",
5
5
  "main": "src/index.js",
6
6
  "bin": {
package/src/video.js CHANGED
@@ -62,21 +62,62 @@ async function watchYouTube(page, stats) {
62
62
  try {
63
63
  const ytFrame = page.locator('iframe[src*="youtube"], iframe[src*="youtu.be"]');
64
64
  await ytFrame.click({ timeout: 5000 }).catch(() => {});
65
- await page.waitForTimeout(3000);
65
+ await page.waitForTimeout(5000);
66
66
 
67
67
  await page.evaluate(() => {
68
68
  const f = document.querySelector('iframe[src*="youtube"], iframe[src*="youtu.be"]');
69
69
  if (!f) return;
70
70
  f.contentWindow.postMessage('{"event":"listening"}', '*');
71
71
  });
72
- await page.waitForTimeout(2000);
72
+ await page.waitForTimeout(3000);
73
73
 
74
74
  await page.evaluate(() => {
75
75
  const f = document.querySelector('iframe[src*="youtube"], iframe[src*="youtu.be"]');
76
76
  if (!f) return;
77
77
  f.contentWindow.postMessage(JSON.stringify({ event: 'command', func: 'playVideo', args: [] }), '*');
78
78
  });
79
- await page.waitForTimeout(1000);
79
+ await page.waitForTimeout(3000);
80
+
81
+ const isPlaying = await page.evaluate(() => {
82
+ return new Promise((resolve) => {
83
+ const f = document.querySelector('iframe[src*="youtube"], iframe[src*="youtu.be"]');
84
+ if (!f) { resolve(false); return; }
85
+ const handler = (e) => {
86
+ try {
87
+ const d = typeof e.data === 'string' ? JSON.parse(e.data) : e.data;
88
+ if (d.event === 'infoDelivery' && d.info) {
89
+ window.removeEventListener('message', handler);
90
+ resolve(d.info.currentTime > 0);
91
+ }
92
+ } catch(e) {}
93
+ };
94
+ window.addEventListener('message', handler);
95
+ f.contentWindow.postMessage(JSON.stringify({ event: 'command', func: 'getCurrentTime', args: [] }), '*');
96
+ setTimeout(() => { window.removeEventListener('message', handler); resolve(false); }, 5000);
97
+ });
98
+ });
99
+
100
+ if (!isPlaying) {
101
+ log(chalk.yellow(' Player not responding, retrying click...'));
102
+ try {
103
+ const frame = page.frameLocator('iframe[src*="youtube"], iframe[src*="youtu.be"]');
104
+ await frame.locator('.ytp-large-play-button, .ytp-play-button').first().click({ timeout: 5000 }).catch(() => {});
105
+ } catch(e) {}
106
+ await page.waitForTimeout(3000);
107
+
108
+ await page.evaluate(() => {
109
+ const f = document.querySelector('iframe[src*="youtube"], iframe[src*="youtu.be"]');
110
+ if (!f) return;
111
+ f.contentWindow.postMessage('{"event":"listening"}', '*');
112
+ });
113
+ await page.waitForTimeout(2000);
114
+ await page.evaluate(() => {
115
+ const f = document.querySelector('iframe[src*="youtube"], iframe[src*="youtu.be"]');
116
+ if (!f) return;
117
+ f.contentWindow.postMessage(JSON.stringify({ event: 'command', func: 'playVideo', args: [] }), '*');
118
+ });
119
+ await page.waitForTimeout(3000);
120
+ }
80
121
 
81
122
  await page.evaluate(() => {
82
123
  const f = document.querySelector('iframe[src*="youtube"], iframe[src*="youtu.be"]');
@@ -160,27 +201,6 @@ async function watchYouTube(page, stats) {
160
201
  }
161
202
 
162
203
  log(chalk.green(' Done ✓'));
163
- log(chalk.gray(' Syncing with platform...'));
164
-
165
- for (let i = 0; i < 5; i++) {
166
- await page.evaluate(() => {
167
- const f = document.querySelector('iframe[src*="youtube"], iframe[src*="youtu.be"]');
168
- if (!f) return;
169
- try {
170
- f.contentWindow.postMessage(JSON.stringify({ event: 'command', func: 'seekTo', args: [670, true] }), '*');
171
- f.contentWindow.postMessage(JSON.stringify({ event: 'command', func: 'playVideo', args: [] }), '*');
172
- } catch(e) {}
173
- });
174
- await page.waitForTimeout(3000);
175
- await page.evaluate(() => {
176
- const f = document.querySelector('iframe[src*="youtube"], iframe[src*="youtu.be"]');
177
- if (!f) return;
178
- try {
179
- f.contentWindow.postMessage(JSON.stringify({ event: 'command', func: 'pauseVideo', args: [] }), '*');
180
- } catch(e) {}
181
- });
182
- await page.waitForTimeout(2000);
183
- }
184
204
 
185
205
  if (stats) stats.videosWatched++;
186
206
  return true;