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