homebridge-melcloud-control 4.0.0-beta.284 → 4.0.0-beta.285

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/melcloud.js +42 -16
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "MELCloud Control",
3
3
  "name": "homebridge-melcloud-control",
4
- "version": "4.0.0-beta.284",
4
+ "version": "4.0.0-beta.285",
5
5
  "description": "Homebridge plugin to control Mitsubishi Air Conditioner, Heat Pump and Energy Recovery Ventilation.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
package/src/melcloud.js CHANGED
@@ -299,27 +299,53 @@ class MelCloud extends EventEmitter {
299
299
  await page.waitForSelector('input[name="username"]', { timeout: 5000 });
300
300
  await page.type('input[name="username"]', this.user, { delay: 50 });
301
301
  await page.type('input[name="password"]', this.passwd, { delay: 50 });
302
- const button1 = await page.$('input[type="Submit"]');
302
+ const button1 = await page.$('input[type="submit"]');
303
303
  await Promise.all([button1.click(), page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 10000 })]);
304
304
 
305
- let authCode = null;
306
- const start = Date.now();
307
- const timeout = 30000; // 30s
308
-
309
- while (!authCode && Date.now() - start < timeout) {
310
- authCode = await page.evaluate(() => {
311
- const m = window.location.href.match(/[?&]code=([^&]+)/);
312
- return m ? m[1] : null;
313
- });
314
- if (!authCode) await new Promise(r => setTimeout(r, 200));
305
+ // 1️⃣ Sprawdzenie, czy code jest w URL
306
+ const url1 = page.url();
307
+ const urlMatch = url1.match(/[?&]code=([^&]+)/);
308
+ if (urlMatch) {
309
+ console.log('Authorization code found in URL');
310
+ return urlMatch[1];
315
311
  }
316
- console.log('Authorization code:', page);
317
- console.log('Authorization code1:', window.location.href);
318
- if (!authCode) {
319
- throw new Error('Authorization code not found in URL');
312
+
313
+ // 2️⃣ Jeśli nie w URL, sprawdź body HTML (form_post)
314
+ const html = await page.content();
315
+
316
+ // Form_post pattern
317
+ const formCodeMatch = html.match(/name="code"\s+value="([^"]+)"/);
318
+ const formStateMatch = html.match(/name="state"\s+value="([^"]+)"/);
319
+ const formActionMatch = html.match(/action="([^"]+)"/);
320
+
321
+ if (formCodeMatch && formStateMatch && formActionMatch) {
322
+ console.log('Authorization code found in form_post HTML');
323
+ // Wyślij POST do action
324
+ const actionUrl = formActionMatch[1];
325
+ const formData = new URLSearchParams();
326
+ formData.append('code', formCodeMatch[1]);
327
+ formData.append('state', formStateMatch[1]);
328
+
329
+ const response = await page.evaluate(async (url, body) => {
330
+ const res = await fetch(url, {
331
+ method: 'POST',
332
+ body,
333
+ credentials: 'include'
334
+ });
335
+ return res.text();
336
+ }, actionUrl, formData);
337
+
338
+ // Spróbuj znaleźć code w odpowiedzi POST
339
+ const postCodeMatch = response.match(/code=([^&"']+)/);
340
+ if (postCodeMatch) return postCodeMatch[1];
320
341
  }
321
342
 
322
- console.log('Authorization code:', authCode);
343
+ // 3️⃣ Jeśli nie w form_post, sprawdź JS melcloudhome://
344
+ const jsCodeMatch = html.match(/melcloudhome:\/\/[^"'\s]*[?&]code=([^&"'\s]+)/);
345
+ if (jsCodeMatch) {
346
+ console.log('Authorization code found in JS redirect');
347
+ return jsCodeMatch[1];
348
+ }
323
349
 
324
350
  // 5. Wymień code na access_token
325
351
  const tokenResponse = await axios.post(TOKEN_ENDPOINT, new URLSearchParams({