-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrailway.js
162 lines (147 loc) · 4.58 KB
/
railway.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
'use strict';
const puppeteer = require('puppeteer');
async function stealth(page) {
page.setDefaultNavigationTimeout(60000);
await page.setViewport({ width: 1050, height: 682 });
await page.setUserAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Safari/537.36");
await page.evaluateOnNewDocument(() => {
// 反爬虫检测
Object.defineProperty(navigator, 'webdriver', {
get: () => false,
});
Object.defineProperty(navigator, 'plugins', {
get: () => [
{
0: {type: "application/x-google-chrome-pdf", suffixes: "pdf", description: "Portable Document Format", enabledPlugin: Plugin},
description: "Portable Document Format",
filename: "internal-pdf-viewer",
length: 1,
name: "Chrome PDF Plugin"
},
{
0: {type: "application/pdf", suffixes: "pdf", description: "", enabledPlugin: Plugin},
description: "",
filename: "mhjfbmdgcfjbbpaeojofohoefgiehjai",
length: 1,
name: "Chrome PDF Viewer"
},
{
0: {type: "application/x-nacl", suffixes: "", description: "Native Client Executable", enabledPlugin: Plugin},
1: {type: "application/x-pnacl", suffixes: "", description: "Portable Native Client Executable", enabledPlugin: Plugin},
description: "",
filename: "internal-nacl-plugin",
length: 2,
name: "Native Client"
}
],
});
window.navigator.chrome = {
runtime: {},
loadTimes: function() {},
csi: function() {},
app: {}
};
});
}
async function sleep(ms) {
return new Promise((resolve => setTimeout(()=>resolve(), ms)));
}
async function login_github(page, username, password) {
var url = "https://github.com/login";
console.log("opening " + url + "...");
await page.goto(url, { waitUntil: "networkidle2" });
var name = await page.$("#login_field");
if(name) {
console.log("github needs to login");
await name.type(username);
await page.type("#password", password);
await page.click("input[name='commit']");
}
else {
console.log("github already logged in");
await page.close();
}
}
async function login_railway(page) {
var url = "https://railway.app/"; // set your service url here!
console.log("opening " + url + "...");
await page.goto(url, { waitUntil: "networkidle2" });
var [button] = await page.$x("//button[contains(., 'Login')]");
if(button) {
console.log("railway needs to login");
await button.click();
var [button] = await page.$x("//span[contains(., 'GitHub')]");
if(button) {
console.log("login ...");
await button.click();
// 等待登录成功
await page.waitForSelector("a[name='Account Settings']");
await page.goto(url, { waitUntil: "networkidle2" });
}
}
else
console.log("railway already logged in");
}
async function main(action) {
// set up browser object
const browser = await puppeteer.launch( {
headless: !process.env.GNOME_TERMINAL_SCREEN,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
userDataDir: "./chromeData"
} );
console.log("browser started.");
//*
var [page] = await browser.pages();
await stealth(page);
//* login to github
var username = ""; // set your username here!
var password = ""; // set your password here!
await login_github(page, username, password)
//*/
//*
var page = await browser.newPage();
await stealth(page);
//* login to railway
await login_railway(page);
//*
// 设置选择器
var s_svg = ".metro-pane-content ul:nth-child(1) button svg";
var s_remove = "div[role='menu'][data-state='open'] div:nth-of-type(4)";
var s_rollback = "div[role='menu'][data-state='open'] div:nth-child(1)";
var s_confirm = "button.confirm[color='red'] span";
if(action == "down") {
var svg = await page.$(s_svg);
if(svg) {
console.log("click svg:" + JSON.stringify(svg));
await svg.click();
await page.waitForSelector(s_remove);
console.log("click remove.");
await page.click(s_remove);
await page.waitForSelector(s_confirm);
console.log("click confirm.");
await page.click(s_confirm);
await page.waitForNetworkIdle("networkidle2");
console.log("down ok");
}
}
else if (action == "up") {
var svg = await page.$(s_svg);
if(svg) {
console.log("click svg:" + JSON.stringify(svg));
await svg.click();
await page.waitForSelector(s_rollback);
console.log("click rollback.");
await page.click(s_rollback);
await page.waitForSelector(s_confirm);
console.log("click confirm.");
await page.click(s_confirm);
await page.waitForNetworkIdle("networkidle2");
console.log("up ok");
}
}
//*/
if (action != "nc")
await browser.close();
}
let action = process.argv.splice(2)[0];
main(action);