-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path天翼云电脑保活.js
More file actions
73 lines (70 loc) · 2.47 KB
/
Copy path天翼云电脑保活.js
File metadata and controls
73 lines (70 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const puppeteer = require("puppeteer");
const dayjs = require("dayjs");
// 刷新间隔,默认 5 分钟
const refreshTime = 5;
async function main() {
try {
const browser = await puppeteer.launch({
headless: "new",
executablePath: "/usr/bin/chromium-browser",
args: ["--no-sandbox", "--disable-dev-shm-usage"],
// headless: false,
// executablePath:
// "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
});
const page = await browser.newPage();
await page.goto("https://pc.ctyun.cn/#/login");
const imageData = await page.$eval(".self-qr", (el) => el.title);
console.log(
"扫码登录:",
`https://cli.im/api/qrcode/code?text=${encodeURIComponent(imageData)}`
);
await page.waitForNavigation();
await page.waitForSelector(".icon-get-into");
const id = await page.$eval(
".desktop-main-name-code",
(el) => el.textContent
);
console.log("登录成功:", id);
const desktopUrl = `https://pc.ctyun.cn/#/desktop?id=${Buffer.from(
id.slice(-8)
).toString("base64")}`;
console.log("desktopUrl", desktopUrl);
await page.$eval(".icon-get-into", (el) => el.click());
await page.waitForSelector(".btn-show-toolbar--safe-img + span");
await new Promise((r) => setTimeout(r, 10000));
const delay = await page.$eval(
".btn-show-toolbar--safe-img + span",
(el) => el.innerText
);
console.log(`云电脑窗口已启动,当前延迟:${delay}!`);
console.log("--------------------------------------");
const timer = setInterval(async () => {
try {
await page.reload();
await page.waitForSelector(".btn-show-toolbar--safe-img + span");
await new Promise((r) => setTimeout(r, 10000));
const delay = await page.$eval(
".btn-show-toolbar--safe-img + span",
(el) => el.innerText
);
console.log(
`[${dayjs().format(
"YYYY MM-DD HH:mm:ss"
)}] 窗口刷新成功,当前延迟:${delay}!`
);
} catch (error) {
clearInterval(timer);
await browser.close();
console.log(
`[${dayjs().format("YYYY MM-DD HH:mm:ss")}] 窗口刷新失败!!!`
);
console.log(error);
}
}, refreshTime * 60 * 1000);
} catch (error) {
console.log("程序启动失败!!!");
console.log(error);
}
}
main();