-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
118 lines (110 loc) · 3.14 KB
/
index.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
const fetch = require('node-fetch');
var HTML = require('node-html-parser');
var PNL = require('phonenumberlite');
var cache = {};
var config = {
fetch: (url, opts) => {
// Pages for specific phone numbers require cookies set by a previous visit, so save them.
if (cache.cookie) {
if (!opts) opts = {};
if (!opts.headers) opts.headers = {};
Object.assign(opts.headers, { 'cookie': cache.cookie });
}
return fetch(url, opts).then(res => {
cache.cookie = res.headers.get('set-cookie');
return res.text();
});
},
number: 'number-boxes-itemm-number',
country: 'number-boxes-item-country',
button: 'number-boxes1-item-button',
message: 'wr3pc333el1878'
}
var self = module.exports = {
config: opts => { Object.assign(config, opts) },
fetch: async () => {
var s = await config.fetch('https://receive-smss.com');
var html = HTML.parse(s);
var nbr = getText(html, config.number);
var loc = getText(html, config.country);
var stat = getText(html, config.button);
var ls = [];
for (var i = 0; i < nbr.length; i++) {
if (stat[i] != 'Open') continue;
var nn = PNL.parse(nbr[i]);
if (!nn) {
console.log('WARNING: ' + nbr[i] + ' failed to parse!')
continue
}
ls.push({ loc: loc[i], nbr: nn.internationalFormat });
}
return cache.numbers = ls;
},
numbers: async (country) => {
var ls = cache.numbers || await self.fetch();
return (country)
? ls.filter(o => o.loc == country).map(o => o.nbr)
: ls;
},
countries: async () => {
var ls = cache.numbers || await self.fetch();
return ls.map(o => o.loc).sort().unique();
},
messages: async (receiver) => {
var url = 'https://receive-smss.com/sms/' + clean(receiver);
var s = await config.fetch(url);
var html = HTML.parse(s);
var cells = getText(html, config.message);
var ret = [];
for (var i = 0; i < cells.length; i += 3) {
var o = {
sender: clean(cells[i]),
message: cells[i + 1],
time: getSeconds(cells[i + 2])
};
ret.push(o);
}
return ret;
},
check: async (receiver, sender, re) => {
sender = clean(sender);
var ls = await self.messages(receiver);
ls = ls.filter(o => clean(o.sender) == sender && o.message.match(re));
return ls.length > 0;
},
watch: (cfg) => {
if (!cfg && cache.watcher) {
clearInterval(cache.watcher);
return cache.watcher = undefined;
}
var count = cfg.count || 6;
cache.watcher = setInterval(chk, 'delay' in cfg ? cfg.delay : 5 * 60 * 1000);
async function chk() {
var res = await self.check(cfg.receiver, cfg.sender, cfg.re);
if (res || --count == 0) {
clearInterval(cache.watcher);
cfg.callback(res);
}
}
return cache.watcher;
}
};
function getText(html, className) {
return html.querySelectorAll('.' + className)
.map(node => node.rawText);
}
function getSeconds(s) {
var n = Number(s.match(/\d+/)[0])
if (s.match(/minute/)) n *= 60
if (s.match(/hour/)) n *= 60*60
return n
}
function clean(nbr) {
var t = typeof nbr;
if (t == 'number') nbr = nbr.toString();
if (t == 'object') throw new Error('Cannot clean objects');
return (nbr || '').replace(/\D/g, '');
}
Array.prototype.unique = function () {
return this.filter((e, pos) => this.indexOf(e) == pos);
}