forked from DanielHe4rt/bbbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
193 lines (175 loc) · 4.68 KB
/
helper.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
const configs = require('./config');
const { createCanvas, loadImage } = require('canvas');
const canvas = createCanvas(265, 53);
const ctx = canvas.getContext('2d');
const isColumnWhite = (ctx, height, x) => {
for (let y = 0; y < height; y++) {
let data = ctx.getImageData(x, y, 1, 1).data;
if (!isWhite(data)) return false;
}
return true;
};
const resolveImages = async () => {
return await loadImage('alo.png').then(async img => {
let result = false;
let w2 = img.width;
let h2 = img.height;
ctx.drawImage(img, 0, 0);
let item = [];
let items = [];
let isLastColorWhite = true;
for (let i = 0; i <= w2; i++) {
let isColumnWhiteHolder = isColumnWhite(ctx, h2, i);
if (!isColumnWhiteHolder && isLastColorWhite) {
item.push(i);
}
if (isColumnWhiteHolder && !isLastColorWhite) {
item.push(i);
items.push(item);
item = [];
}
isLastColorWhite = isColumnWhiteHolder;
}
items.forEach((item, index) => {
let width = item[1] - item[0];
let newCanvas = createCanvas(width, 53);
let newCtx = newCanvas.getContext('2d');
newCtx.drawImage(img, item[0], 0, width, 53, 0, 0, width, 53);
let lines = findLines(newCtx, 53);
let inflections = findInflections(newCtx, width);
let data = {
color: checkImage(newCanvas, newCtx, width),
lines: findLines(newCtx, 53),
inflections: findInflections(newCtx, width),
diffHigh: lines.first - inflections.highest,
diffLow: inflections.lowest - lines.last
};
if (
(data.diffHigh == 6 || data.diffHigh == 7) &&
data.diffLow >= 4 &&
data.diffLow <= 9 &&
data.color.black >= 1150 &&
data.color.black <= 1265
) {
result = item;
}
});
return result;
});
};
const isRowWhite = (ctx, width, y) => {
for (let x = 0; x < width; x++) {
let data = ctx.getImageData(x, y, 1, 1).data;
if (!isWhite(data)) return false;
}
return true;
};
const checkImage = (canvas, ctx, width) => {
let results = {
white: 0,
black: 0
};
for (let i = 0; i <= width; i++) {
for (let y = 0; y <= 53; y++) {
let data = ctx.getImageData(i, y, 1, 1).data;
isWhite(data) ? results.white++ : results.black++;
}
}
return results;
};
const findInflections = (ctx, width) => {
let results = {
highest: 0,
lowest: 0
};
for (let y = 0; y < 53; y++) {
if (!isRowWhite(ctx, width, y) && !results.highest) {
results.highest = y;
}
if (!isRowWhite(ctx, width, y)) {
results.lowest = y;
}
}
return results;
};
const findLines = ctx => {
let results = {
first: 0,
last: 0
};
for (let y = 0; y < 53; y++) {
let data = ctx.getImageData(0, y, 1, 1).data;
if (results.first === 0 && !isWhite(data)) {
results.first = y;
}
if (!isWhite(data)) {
results.last = y;
}
}
return results;
};
const isWhite = data => {
return data[0] == 255 && data[1] == 255 && data[2] == 255 ? true : false;
};
const refreshCaptcha = async (page, xpath) => {
let handler = await page.$x(xpath);
setTimeout(() => {
handler[0].click();
}, 10);
};
const clickXPath = async (page, xpath) => {
let handler = await page.$x(xpath);
setTimeout(() => {
handler[0].click();
}, 1000);
};
const removeSponsor = async page => {
await page.evaluate(_ => {
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML =
'.tag-manager-publicidade-container { display: none; !important}';
document.getElementsByTagName('head')[0].appendChild(style);
});
};
const scrollToTop = async page => {
await page.evaluate(_ => {
window.scrollBy(0, -10000);
});
};
const revote = async page => {
setTimeout(async () => {
await page.waitForXPath(configs.xpaths.finishText).then(async () => {
const retryBtn = await page.$x(configs.xpaths.finishButton);
retryBtn[0].click();
setTimeout(async () => {
retryBtn[0].click();
}, 500);
setTimeout(async () => {
await scrollToTop(page);
setTimeout(async () => {
await clickXPath(page, configs.xpaths.user);
}, 1000);
setTimeout(async () => {
await clickXPath(page, configs.xpaths.user);
const refreshBtn = await page.waitForXPath(
configs.xpaths.buttonCaptcha
);
refreshBtn.click();
setTimeout(() => {
refreshBtn.click();
scrollToTop(page);
}, 200);
}, 1000);
}, 750);
});
}, 3000);
};
module.exports = {
resolveImages,
refreshCaptcha,
clickXPath,
scrollToTop,
revote,
removeSponsor
};