-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
251 lines (211 loc) · 6.93 KB
/
content.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// 发送消息到background.js
console.log("content.js");
sendMessage({ action: "getJquery" });
// 监听来自background.js的特定消息(可选)
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
// 检查消息内容以确定是否处理
switch (message.action) {
case "appendScript":
break;
default:
break;
}
});
function sendMessage(data) {
chrome.runtime.sendMessage(data);
}
// var url = `https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc&fs=
// ${encodeURIComponent("洛阳")},LYF&ts=
// ${encodeURIComponent("北京")},BJP&date=2024-09-28&flag=N,N,Y`;
// window.open(url);
window.onload = () => {
console.log("document onload");
executeAfterTime("2024-09-14T16:27:30");
};
// executeAfterTime("2024-09-14T16:27:30");
function executeAfterTime(targetDateStr) {
// 将目标时间字符串转换为Date对象
const targetDate = new Date(targetDateStr);
// 获取当前时间
const now = new Date();
// 计算时间差(毫秒)
const timeDiff = targetDate - now;
// 如果目标时间已经过去,则立即执行
if (timeDiff <= 0) {
execute();
} else {
// 否则,使用setTimeout在指定时间后执行
setTimeout(() => {
executeAfterTime(targetDateStr);
}, 200);
}
}
function execute() {
var listNum = 0;
var currentPath = location.pathname;
switch (currentPath) {
case "/otn/leftTicket/init":
// 查询车次
var tListEl = document.getElementById("queryLeftTable");
// 设置基础信息
// document.getElementById("fromStationText").value = "天津";
// document.getElementById("toStationText").value = "洛阳";
// document.getElementById("train_date").value = "2024-09-06";
// 点击查询
var queryBtn = document.getElementById("query_ticket");
queryBtn.click();
// 车次列表是否存在
this.isListExist(0);
break;
case "/otn/confirmPassenger/initDc":
// 提交订单
submitOrder();
break;
case "/otn/payOrder/init":
// 支付页面-倒计时10m
break;
default:
break;
}
}
function isListExist(num) {
const spans = document.querySelectorAll("tbody .number");
if (spans.length > 0) {
scheduleBtn();
} else if (num >= 20) {
console.log("未查询到数据,结束!");
return;
} else {
// 点击查询
var queryBtn = document.getElementById("query_ticket");
queryBtn.click();
setTimeout(() => {
num++;
isListExist(num);
}, 500);
}
}
function hasChildNodes(element) {
return element.childNodes.length > 0;
}
function scheduleBtn() {
var locationSearch = location.search;
const spans = document.querySelectorAll("tbody .number");
var trainNumber = "K269";
if (locationSearch.indexOf("abc") == -1) {
var url = location.href + "&abc=K4363";
window.open(url);
} else if (locationSearch.indexOf("abc") != -1) {
// trainNumber = "K4363";
localStorage.setItem("trainNumber", "K4363");
}
spans.forEach((span) => {
// 车次 k269 K4363
if (span.textContent == trainNumber) {
const tr = span.closest("tr");
const a = tr.querySelector("a.btn72");
if (a) {
setTimeout(() => {
a.click();
}, 1000);
}
}
});
}
function submitOrder() {
var personnelStr = "娄红霞";
if (localStorage.getItem("trainNumber")) {
personnelStr = "岳金明";
} else {
personnelStr = "娄红霞";
}
// 选择人员
var listItems = document.querySelectorAll("#normal_passenger_id li");
listItems.forEach(function (listItem) {
var label = listItem.querySelector("label");
if (label && label.textContent === personnelStr) {
var input = listItem.querySelector("input.check");
input.click();
}
});
// 选择席位
var selectElement = document.getElementById("seatType_1");
// 遍历select元素中的所有option
for (var i = 0; i < selectElement.options.length; i++) {
if (selectElement.options[i].text.includes("硬卧")) {
selectElement.options[i].selected = true;
break;
}
}
// 提交进入选座弹窗
var submitBtn = document.getElementById("submitOrder_id");
submitBtn.click();
setTimeout(() => {
fastTrain();
// 提交订单进入支付页面
clickSubmitOrder(0);
}, 500);
}
// 高铁座位
function highSpeedRail() {
var submitBtn = document.getElementById("submitOrder_id");
submitBtn.click();
var el = document.getElementById("1D");
el.click();
}
// 普快座位
function fastTrain() {
var bedItems = document.querySelectorAll(".bed-item");
bedItems.forEach(function (bedItem) {
var txtElement = bedItem.querySelector(".txt");
var personnelStr = "岳金明";
var seatType = personnelStr == "岳金明" ? "上铺" : "下铺";
if (txtElement && txtElement.textContent.trim() === seatType) {
var numberControlDiv = bedItem.querySelector(
".number-control-mini"
);
var increaseButton =
numberControlDiv.querySelector(".num-increase");
// 触发点击事件
increaseButton.click();
}
});
}
// 点击直到跳转订单页为止
function clickSubmitOrder(clickNum) {
var pathName = location.pathname;
if (clickNum > 200) {
return;
}
// && clickNum < 200
if (pathName != "/otn/payOrder/init" && clickNum < 100) {
clickNum++;
var qrSubmitIdEl = document.getElementById("qr_submit_id");
qrSubmitIdEl.click();
setTimeout(() => {
clickSubmitOrder(clickNum);
}, 500);
}
}
function sendPostRequest(url, data, callback) {
// 创建一个XMLHttpRequest对象
var xhr = new XMLHttpRequest();
// 初始化一个请求
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.setRequestHeader("x-from-business-line", "ubi");
// xhr.setRequestHeader("x-from-tenant", "9999");
// 注册请求完成的事件处理程序
xhr.onreadystatechange = function () {
// 请求完成(readyState为4)且响应状态码为200
if (xhr.readyState === 4 && xhr.status === 200) {
// 调用回调函数并传入响应的数据
callback(null, xhr.responseText);
} else if (xhr.readyState === 4) {
// 处理请求失败的情况
callback(xhr.statusText, null);
}
};
// 发送请求
xhr.send(JSON.stringify(data));
}