tronclass-auto 1.5.3 → 1.5.5

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 +38 -13
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tronclass-auto",
3
- "version": "1.5.3",
3
+ "version": "1.5.5",
4
4
  "description": "自動觀看 eclass/TronClass 影片、填寫表單的 CLI 工具",
5
5
  "main": "src/index.js",
6
6
  "bin": {
package/src/video.js CHANGED
@@ -66,31 +66,56 @@ async function watchYouTube(page, stats) {
66
66
  return false;
67
67
  }
68
68
 
69
- const evalYT = async (code) => {
70
- return await ytFrame.evaluate(code).catch(() => null);
71
- };
69
+ log(chalk.gray(` Frame found: ${ytFrame.url().substring(0, 60)}`));
70
+
71
+ let playerReady = false;
72
+ for (let attempt = 0; attempt < 15; attempt++) {
73
+ try {
74
+ const ready = await ytFrame.evaluate(() => {
75
+ const p = document.querySelector('#movie_player');
76
+ return !!(p && typeof p.playVideo === 'function');
77
+ });
78
+ if (ready) { playerReady = true; break; }
79
+ } catch (e) {}
80
+ if (attempt % 3 === 0) log(chalk.gray(` Waiting for player... (${attempt + 1}/15)`));
81
+ await page.waitForTimeout(2000);
82
+ }
83
+
84
+ if (!playerReady) {
85
+ log(chalk.yellow(' YouTube player not loaded'));
86
+ return false;
87
+ }
72
88
 
73
89
  log(chalk.blue(' Starting video...'));
74
- await evalYT("() => { const p = document.querySelector('#movie_player'); if (p) p.playVideo(); }");
75
- await page.waitForTimeout(3000);
90
+ await ytFrame.evaluate(() => document.querySelector('#movie_player').playVideo());
91
+ await page.waitForTimeout(5000);
76
92
 
77
- const state1 = await evalYT("() => { const p = document.querySelector('#movie_player'); return p ? p.getPlayerState() : -1; }");
93
+ const state1 = await ytFrame.evaluate(() => document.querySelector('#movie_player').getPlayerState());
78
94
  if (state1 !== 1) {
79
- log(chalk.yellow(' Retrying play...'));
80
- await evalYT("() => { const p = document.querySelector('#movie_player'); if (p) p.playVideo(); }");
81
- await page.waitForTimeout(3000);
95
+ log(chalk.yellow(` State: ${state1}, retrying...`));
96
+ await ytFrame.evaluate(() => document.querySelector('#movie_player').playVideo());
97
+ await page.waitForTimeout(5000);
82
98
  }
83
99
 
84
- await evalYT("() => { const p = document.querySelector('#movie_player'); if (p) { p.mute(); p.setPlaybackRate(2); } }");
85
- await page.waitForTimeout(2000);
100
+ await ytFrame.evaluate(() => {
101
+ const p = document.querySelector('#movie_player');
102
+ p.mute();
103
+ p.setPlaybackRate(2);
104
+ });
105
+ await page.waitForTimeout(3000);
86
106
 
87
- rate = await evalYT("() => { const p = document.querySelector('#movie_player'); return p ? p.getPlaybackRate() : 1; }") || 1;
107
+ rate = await ytFrame.evaluate(() => document.querySelector('#movie_player').getPlaybackRate());
88
108
  log(chalk.blue(` Speed: ${rate}x`));
89
109
 
90
- const state2 = await evalYT("() => { const p = document.querySelector('#movie_player'); return p ? p.getPlayerState() : -1; }");
110
+ const state2 = await ytFrame.evaluate(() => document.querySelector('#movie_player').getPlayerState());
91
111
  if (state2 === 1) {
92
112
  log(chalk.green(' Playing ✓'));
93
113
  } else {
114
+ log(chalk.yellow(` Final state: ${state2}`));
115
+ }
116
+ } catch (e) {
117
+ log(chalk.yellow(` YouTube error: ${e.message}`));
118
+ }
94
119
  log(chalk.yellow(` State: ${state2}`));
95
120
  }
96
121
  } catch (e) {