@@ -63,20 +63,23 @@ async function loginAndScrape(url, username, password, isDocker, headless) {
6363 // 1.1.1 舊版畫面
6464 // await page.waitForSelector('div[id="root"] > div > div > button');
6565 // await page.click('div[id="root"] > div > div > button');
66+ await delay ( 300 ) ;
6667 await page . waitForSelector (
6768 'div[id="root"] > div > div > div > div > div form > div > div button:last-of-type'
6869 ) ;
70+ await delay ( 30 ) ;
6971 await page . click (
7072 'div[id="root"] > div > div > div > div > div form > div > div button:last-of-type'
7173 ) ;
74+ await delay ( 3000 ) ;
7275 } catch ( error ) {
7376 console . error ( "點擊取消初始畫面失敗:" , error ) ;
74- tryCount ++ ;
75- if ( tryCount <= tryCountMax ) {
76- return await loginAndScrape ( url , username , password , isDocker , headless ) ;
77- } else {
78- throw new Error ( "重試點擊取消初始畫面失敗" ) ;
79- }
77+ // tryCount++;
78+ // if (tryCount <= tryCountMax) {
79+ // return await loginAndScrape(url, username, password, isDocker, headless);
80+ // } else {
81+ // throw new Error("重試點擊取消初始畫面失敗");
82+ // }
8083 }
8184
8285 try {
@@ -92,53 +95,15 @@ async function loginAndScrape(url, username, password, isDocker, headless) {
9295 }
9396 }
9497
95- // try {
96- // console.log("展開使用者列表");
97- // await selectProfileButton(page);
98- // } catch (error) {
99- // console.error("展開使用者列表失敗:", error);
100- // tryCount++;
101- // if (tryCount <= tryCountMax) {
102- // return await loginAndScrape(url, username, password, isDocker, headless);
103- // } else {
104- // throw new Error("重試展開使用者列表失敗");
105- // }
106- // }
107-
108- // try {
109- // console.log("點擊檔案列表");
110- // await clickProfile(page);
111- // } catch (error) {
112- // console.error("點擊檔案列表失敗:", error);
113- // tryCount++;
114- // if (tryCount <= tryCountMax) {
115- // return await loginAndScrape(url, username, password, isDocker, headless);
116- // } else {
117- // throw new Error("重試點擊檔案列表失敗");
118- // }
119- // }
120-
121- // try {
122- // console.log("領取每日獎勵");
123- // await claimCredit(page);
124- // } catch (error) {
125- // console.error("領取每日獎勵失敗:", error);
126- // tryCount++;
127- // if (tryCount <= tryCountMax) {
128- // return await loginAndScrape(url, username, password, isDocker, headless);
129- // } else {
130- // throw new Error("重試領取每日獎勵失敗");
131- // }
132- // }
133-
13498 try {
13599 console . log ( "直接從彈出視窗領取每日獎勵" ) ;
136100 await claimCreditFromPop ( page ) ;
137101 } catch ( error ) {
138102 console . error ( "彈出視窗領取每日獎勵失敗:" , error ) ;
139103 tryCount ++ ;
140104 if ( tryCount <= tryCountMax ) {
141- return await loginAndScrape ( url , username , password , isDocker , headless ) ;
105+ console . log ( "嘗試改用原本方法領取每日獎勵" ) ;
106+ await originalScrape ( url , username , password , isDocker , headless , page ) ;
142107 } else {
143108 throw new Error ( "彈出視窗領取每日獎勵失敗" ) ;
144109 }
@@ -148,16 +113,65 @@ async function loginAndScrape(url, username, password, isDocker, headless) {
148113 await browser . close ( ) ;
149114}
150115
116+ async function originalScrape (
117+ url ,
118+ username ,
119+ password ,
120+ isDocker ,
121+ headless ,
122+ page
123+ ) {
124+ try {
125+ console . log ( "展開使用者列表" ) ;
126+ await selectProfileButton ( page ) ;
127+ } catch ( error ) {
128+ console . error ( "展開使用者列表失敗:" , error ) ;
129+ tryCount ++ ;
130+ if ( tryCount <= tryCountMax ) {
131+ return await loginAndScrape ( url , username , password , isDocker , headless ) ;
132+ } else {
133+ throw new Error ( "重試展開使用者列表失敗" ) ;
134+ }
135+ }
136+
137+ try {
138+ console . log ( "點擊檔案列表" ) ;
139+ await clickProfile ( page ) ;
140+ } catch ( error ) {
141+ console . error ( "點擊檔案列表失敗:" , error ) ;
142+ tryCount ++ ;
143+ if ( tryCount <= tryCountMax ) {
144+ return await loginAndScrape ( url , username , password , isDocker , headless ) ;
145+ } else {
146+ throw new Error ( "重試點擊檔案列表失敗" ) ;
147+ }
148+ }
149+
150+ try {
151+ console . log ( "領取每日獎勵" ) ;
152+ await claimCredit ( page ) ;
153+ } catch ( error ) {
154+ console . error ( "領取每日獎勵失敗:" , error ) ;
155+ tryCount ++ ;
156+ if ( tryCount <= tryCountMax ) {
157+ return await loginAndScrape ( url , username , password , isDocker , headless ) ;
158+ } else {
159+ throw new Error ( "重試領取每日獎勵失敗" ) ;
160+ }
161+ }
162+ }
163+
151164//#region 登入
152165async function login ( page , username , password ) {
153166 await page . type ( "#email-input" , username ) ;
154167 await page . type ( "#password-input" , password ) ;
155168 await delay ( 300 ) ;
156169 await page . waitForSelector ( 'button[type="submit"]' ) ;
157170 await page . click ( 'button[type="submit"]' ) ;
158- await delay ( 3000 ) ;
171+ await delay ( 6000 ) ;
159172 try {
160- await page . $eval ( 'button[type="submit"]' , ( button ) => button . click ( ) ) ;
173+ await page . $eval ( 'button[type="submit"]' , ( button ) => button . clcik ( ) ) ;
174+ await delay ( 3000 ) ;
161175 } catch { }
162176}
163177
@@ -184,69 +198,47 @@ async function checkPopup(page) {
184198//#region 點擊頁首圖標
185199async function selectProfileButton ( page ) {
186200 while ( true ) {
201+ // try {
202+ // // 確認是否已登入
203+ // console.log("確認是否已登入");
204+ // try {
205+ // await page.click('button[type="submit"]');
206+ // } catch {}
207+ // let headerText = await page.$eval(
208+ // "header > div:nth-of-type(2)",
209+ // (el) => el.innerText
210+ // );
211+ // // 適配 UI 版本
212+ // if (headerText == null)
213+ // headerText = await page.$eval(
214+ // "header > div:nth-of-type(1)",
215+ // (el) => el.innerText
216+ // );
217+ // if (
218+ // !headerText ||
219+ // headerText.includes("Sign Up") ||
220+ // headerText.includes("Log in")
221+ // ) {
222+ // console.log("未登入");
223+ // continue;
224+ // } else {
225+ // console.log("已登入");
226+ // throw new Error();
227+ // }
228+ // } catch {
229+ // await delay(300);
230+ const isPopupClosed = await checkPopup ( page ) ;
187231 try {
188- // 確認是否已登入
189- console . log ( "確認是否已登入" ) ;
190- try {
191- await page . click ( 'button[type="submit"]' ) ;
192- } catch { }
193- let headerText = await page . $eval (
194- "header > div:nth-of-type(2)" ,
195- ( el ) => el . innerText
196- ) ;
197- // 適配 UI 版本
198- if ( headerText == null )
199- headerText = await page . $eval (
200- "header > div:nth-of-type(1)" ,
201- ( el ) => el . innerText
202- ) ;
203- if (
204- ! headerText ||
205- headerText . includes ( "Sign Up" ) ||
206- headerText . includes ( "Log in" )
207- ) {
208- console . log ( "未登入" ) ;
209- continue ;
210- } else {
211- console . log ( "已登入" ) ;
212- throw new Error ( ) ;
213- }
214- } catch {
232+ // 檢查個人資料頭像並點擊
233+ console . log ( "點擊頭像" ) ;
234+ await page . $eval ( "header > button:nth-of-type(2)" , ( el ) => el . click ( ) ) ;
235+ console . log ( "成功" ) ;
215236 await delay ( 300 ) ;
216- const isPopupClosed = await checkPopup ( page ) ;
217- try {
218- // 檢查個人資料頭像並點擊
219- console . log ( "點擊圖片頭像" ) ;
220- await page . $eval ( "header > span:last-child > img" , ( el ) => el . click ( ) ) ;
221- console . log ( "成功" ) ;
222- await delay ( 300 ) ;
223- break ;
224- } catch {
225- try {
226- // 如果沒有頭像的話會是div
227- console . log ( "點擊文字頭像" ) ;
228- await page . $eval ( "header > span:last-child > div" , ( el ) =>
229- el . click ( )
230- ) ;
231- console . log ( "成功" ) ;
232- await delay ( 300 ) ;
233- break ;
234- } catch ( err ) {
235- try {
236- // 檢查密碼錯誤
237- await page . $eval (
238- 'svg[data-testid="ReportProblemOutlinedIcon"]' ,
239- ( el ) => el
240- ) ;
241- throw new Error ( username , "登入失敗!" ) ;
242- } catch {
243- if ( ! isPopupClosed ) {
244- await checkPopup ( page ) ;
245- }
246- }
247- }
248- }
237+ break ;
238+ } catch ( err ) {
239+ throw new Error ( err ) ;
249240 }
241+ // }
250242 await delay ( 49 ) ;
251243 }
252244}
@@ -256,30 +248,16 @@ async function selectProfileButton(page) {
256248
257249async function clickProfile ( page ) {
258250 await page . waitForSelector (
259- "ul [role='menu'] > li [role='menuitem']:nth-of-type(1)"
251+ "div [role='menu'] > a [role='menuitem']:nth-of-type(1)"
260252 ) ;
261253 while ( true ) {
262254 try {
263255 // 在下拉式選單中找到並點擊個人資料按鈕
264- await page . click ( "ul [role='menu'] > li [role='menuitem']:nth-of-type(1)" ) ;
256+ await page . click ( "div [role='menu'] > a [role='menuitem']:nth-of-type(1)" ) ;
265257 await delay ( 300 ) ;
266258 break ;
267- } catch {
268- // 如果有頭像會是img
269- try {
270- await page . click ( "header > img" ) ;
271- await delay ( 300 ) ;
272- continue ;
273- } catch {
274- if ( ! ( await checkPopup ( page ) ) ) {
275- await page . click ( "header > div" ) ;
276- await delay ( 300 ) ;
277- continue ;
278- } else {
279- await delay ( 300 ) ;
280- continue ;
281- }
282- }
259+ } catch ( err ) {
260+ throw new Error ( err ) ;
283261 }
284262 }
285263}
@@ -300,13 +278,13 @@ async function claimCredit(page) {
300278 while ( true ) {
301279 // 領取!
302280 await page . click (
303- "section > div > div:nth-of-type(2 ) > div:nth-of-type(2) > button"
281+ "section > div > div:nth-of-type(1 ) > div:nth-of-type(2) > button"
304282 ) ;
305283 await delay ( 300 ) ;
306284 await page . reload ( ) ;
307285 await delay ( 5000 ) ;
308286 const updatedClaimBtnText = await page . $eval (
309- "section > div > div:nth-of-type(2 ) > div:nth-of-type(2) > button > span" ,
287+ "section > div > div:nth-of-type(1 ) > div:nth-of-type(2) > button > span" ,
310288 ( el ) => el . innerText
311289 ) ;
312290
@@ -355,24 +333,24 @@ async function claimCreditFromPop(page) {
355333 await delay ( 300 ) ;
356334 await page . reload ( ) ;
357335 await delay ( 5000 ) ;
358- const updatedClaimBtnText = await page . $eval (
359- "section > div > div > button > span:nth-of-type(2)" ,
360- ( el ) => el . innerText
361- ) ;
336+ // const updatedClaimBtnText = await page.$eval(
337+ // "section > div > div > button > span:nth-of-type(2)",
338+ // (el) => el.innerText
339+ // );
362340
363- await delay ( 300 ) ;
341+ // await delay(300);
364342
365- // 確認是不是 "close"
366- if (
367- updatedClaimBtnText . toLowerCase ( ) . includes ( "close" ) ||
368- updatedClaimBtnText . toLowerCase ( ) . includes ( "關閉" ) ||
369- updatedClaimBtnText . toLowerCase ( ) . includes ( "关闭" ) ||
370- updatedClaimBtnText . toLowerCase ( ) . includes ( "閉鎖" )
371- ) {
372- console . log ( "領取成功" ) ;
373- isClaimed = true ;
374- continue checkIsClaimed;
375- }
343+ // // 確認是不是 "close"
344+ // if (
345+ // updatedClaimBtnText.toLowerCase().includes("close") ||
346+ // updatedClaimBtnText.toLowerCase().includes("關閉") ||
347+ // updatedClaimBtnText.toLowerCase().includes("关闭") ||
348+ // updatedClaimBtnText.toLowerCase().includes("閉鎖")
349+ // ) {
350+ // console.log("領取成功");
351+ // isClaimed = true;
352+ // continue checkIsClaimed;
353+ // }
376354 }
377355 } catch {
378356 if ( ! ( await checkPopup ( page ) ) ) {
@@ -395,4 +373,7 @@ loginAndScrape(url, username, password, isDocker, headless)
395373 console . log ( "領取完畢" ) ;
396374 process . exit ( 0 ) ;
397375 } )
398- . catch ( ( error ) => console . error ( "異常:" , error ) ) ;
376+ . catch ( ( error ) => {
377+ console . error ( "異常:" , error ) ;
378+ process . exit ( 1 ) ;
379+ } ) ;
0 commit comments