This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 304
/
main.js
231 lines (201 loc) · 6.57 KB
/
main.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
var MAX_CYCLES = 3;
var currentCycle = 0;
// 请求函数
var request = (functionId, body = {}) =>
fetch("https://api.m.jd.com/client.action", {
body: `functionId=${functionId}&body=${JSON.stringify(
body
)}&client=wh5&clientVersion=1.0.0`,
headers: {
"content-type": "application/x-www-form-urlencoded",
},
method: "POST",
credentials: "include",
});
console.log("💡 正在执行抢炸弹任务");
// 战队抢炸弹,感谢 @elevenDimension 提供 functionId
request("cakebaker_pk_getCakeBomb");
// 主程序
var main = (executeNextCycle) => {
var secretp = "";
var taskList = [];
// 恢复被覆盖的 alert 函数
(() => {
var frame = document.createElement("iframe");
frame.style.display = "none";
document.body.appendChild(frame);
window.alert = frame.contentWindow.alert;
})();
// 模拟任务完成请求
var collector = (task, actionType) => {
console.log(actionType ? "@领取任务:" : "@执行任务:", task);
request("cakebaker_ckCollectScore", {
taskId: task.taskId,
itemId: task.itemId,
actionType: actionType ? 1 : undefined,
safeStr: {
secretp,
// 请自行抓包后,把校验信息填充到下面
info: "",
encrypt: 0,
is_trust: true,
time: 0,
nonstr: "",
token: "",
sign: "",
},
})
.then((res) => res.json())
.then((res) => {
console.log("调用结果:", res.data);
// 如果是执行任务,即任务已经完成,则进行下一个任务
if (!actionType) {
start();
}
});
};
// 甄选优品任务处理
var superiorTask = (() => {
// 是否有请求正在执行
var isBusy = false;
return (rawTaskCollection) => {
var getFeedDetail = (copiedTaskCollection) => {
request("cakebaker_getFeedDetail", {
taskIds: copiedTaskCollection["productInfoVos"]
.map((item) => item.itemId)
.toString(),
})
.then((res) => res.json())
.then((res) => {
var result = res.data.result;
// 确认任务集合所在键名
var taskCollectionContentKeyName = Object.keys(result).find(
(keyName) =>
/Vos?$/.test(keyName) && !["taskVos"].includes(keyName)
);
result[taskCollectionContentKeyName].forEach((taskCollection) => {
Array(taskCollection.maxTimes - taskCollection.times)
.fill(true)
.forEach((_, index) => {
taskList.unshift({
taskName: taskCollection.taskName,
taskId: taskCollection.taskId,
taskType: taskCollection.taskType,
waitDuration: taskCollection.waitDuration,
itemId: taskCollection.productInfoVos[index].itemId,
});
});
});
// 解除请求锁定
isBusy = false;
});
};
if (!isBusy) {
isBusy = true;
getFeedDetail(JSON.parse(JSON.stringify(rawTaskCollection)));
} else {
// 一秒后重试
setTimeout(
getFeedDetail,
1000,
JSON.parse(JSON.stringify(rawTaskCollection))
);
}
};
})();
// 开始任务
var start = () => {
var task = taskList.pop();
if (task) {
// 除了小精灵和连签外的任务要先领取
if (!["小精灵", "连签得金币"].includes(task.taskName)) {
setTimeout(collector, 0, task, true);
}
// 至少等 2 秒再执行任务
setTimeout(collector, (2 + task.waitDuration) * 1000, task);
} else {
// 执行下一轮任务
executeNextCycle();
}
};
(() => {
// 获取基础信息
Promise.all([
request("cakebaker_getHomeData"),
request("cakebaker_getTaskDetail"),
])
.then(([homeData, taskData]) =>
Promise.all([homeData.json(), taskData.json()])
)
.then(([homeData, taskData]) => {
// 如果无法获取任务详情
if (taskData.data.bizCode !== 0) {
if (
taskData.data.bizCode === -7 &&
!~navigator.userAgent.indexOf("jdapp")
) {
console.log("当前浏览器 UA:" + navigator.userAgent);
throw "任务详情获取失败,请确保已设置正确的浏览器 User-Agent。";
} else {
throw `【错误信息:${JSON.stringify(taskData.data)}】`;
}
}
// 获取签名 token
secretp = homeData.data.result.cakeBakerInfo.secretp;
// 生成任务队列
taskData.data.result.taskVos.forEach(async (taskCollection) => {
// 跳过部分邀请任务
if (/助力|站队/.test(taskCollection.taskName)) return;
// 针对甄选优品任务的处理
if (taskCollection["productInfoVos"]) {
superiorTask(taskCollection);
}
// 确认任务集合所在键名
var taskCollectionContentKeyName = Object.keys(taskCollection).find(
(keyName) =>
/Vos?$/.test(keyName) &&
!["productInfoVos", "scoreRuleVos"].includes(keyName)
);
// 某类任务下的任务集合内容
taskCollectionContent = taskCollection[taskCollectionContentKeyName];
if (!taskCollectionContent) return;
Array(taskCollection.maxTimes - taskCollection.times)
.fill(true)
.forEach((_, index) => {
taskList.push({
taskName: taskCollection.taskName,
taskId: taskCollection.taskId,
taskType: taskCollection.taskType,
waitDuration: taskCollection.waitDuration,
itemId:
taskCollectionContent instanceof Array
? taskCollectionContent[index].itemId
: taskCollectionContent.itemId,
});
});
});
console.log(taskList);
// 开始任务
start();
});
})();
};
// 循环执行主程序
var excuteMain = () => {
console.log(
`💡 正在执行第 ${currentCycle + 1} 轮任务,还有 ${
MAX_CYCLES - (currentCycle + 1)
} 轮未执行。`
);
new Promise(main).then(() => {
currentCycle++;
if (currentCycle < MAX_CYCLES) {
// 延迟一些时间才执行下一轮
setTimeout(excuteMain, 10000);
} else {
console.log("@任务已完成!");
alert("任务完成!");
}
});
};
excuteMain();