This repository was archived by the owner on Apr 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwhosapp.js
More file actions
144 lines (128 loc) · 3.72 KB
/
whosapp.js
File metadata and controls
144 lines (128 loc) · 3.72 KB
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
var casper = require('casper').create({
clientScripts: [
'node_modules/jquery/dist/jquery.min.js'
],
pageSettings: {
userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.1500.71 Safari/537.36'
},
viewportSize: {
width: 1280,
height: 1024
},
waitTimeout: Infinity,
logLevel: 'error',
verbose: true
});
// http://docs.casperjs.org/en/latest/events-filters.html#remote-message
casper.on("remote.message", function(msg) {
this.echo("Console: " + msg);
});
// http://docs.casperjs.org/en/latest/events-filters.html#page-error
casper.on("page.error", function(msg, trace) {
this.echo("Error: " + msg);
// maybe make it a little fancier with the code from the PhantomJS equivalent
});
// http://docs.casperjs.org/en/latest/events-filters.html#resource-error
casper.on("resource.error", function(resourceError) {
this.echo("ResourceError: " + JSON.stringify(resourceError, undefined, 4));
});
// http://docs.casperjs.org/en/latest/events-filters.html#page-initialized
casper.on("page.initialized", function(page) {
// CasperJS doesn't provide `onResourceTimeout`, so it must be set through
// the PhantomJS means. This is only possible when the page is initialized
page.onResourceTimeout = function(request) {
console.log('Response Timeout (#' + request.id + '): ' + JSON.stringify(request));
};
});
var setTarget = function() {
return 'xxx';
}
var takeScreeshot = function() {
casper.capture('qrcode.png'
// {
// top: 214,
// left: 296,
// width: 250,
// height: 250
// }
);
casper.echo('QR Screeshot Taken!');
};
var isLoggedIn = function() {
return casper.evaluate(function() {
return !!$(".avatar").length;
});
};
var isRightChatOpen = function(stalkedPerson) {
return casper.evaluate(function(target) {
return $('.pane-chat-header .chat-body span').text() === target;
}, stalkedPerson);
};
var isOnline = function() {
return casper.evaluate(function() {
return $('.pane-chat-header .chat-body .chat-status span').text() === 'online';
});
}
var getDataRef = function() {
return casper.evaluate(function() {
return $(".qrcode").attr("data-ref");
});
};
var isQrCodeChanged = function(dataRef) {
return casper.evaluate(function(dataRef) {
return $(".qrcode").attr("data-ref") !== dataRef;
}, dataRef);
};
var isEmptyText = function() {
return casper.evaluate(function() {
return !!$(".empty-text").length;
});
}
var searchPerson = function(person) {
casper.sendKeys('.input-search', person);
};
var needToSearch = function() {
return casper.evaluate(function() {
return $(".chat").length > 0 && $(".chat").length !== 1;
});
};
var openChat = function() {
casper.click('.chat');
}
casper.start('https://web.whatsapp.com/', function() {
this.echo('Starting...');
this.waitForSelector('img', function() {
this.echo('QrCode is Loaded...');
takeScreeshot();
var dataRef = getDataRef();
this.waitFor(function check() {
if (isQrCodeChanged(dataRef)) {
dataRef = getDataRef();
takeScreeshot();
}
return isLoggedIn();
}, function then() {
var stalkedPerson = setTarget();
this.waitFor(function hangme() {
if (needToSearch()) {
searchPerson(stalkedPerson);
return isEmptyText();
} else {
openChat();
}
return isRightChatOpen(stalkedPerson);
}, function then() {
this.waitFor(function trackTheGuy() {
if (isOnline()) {
console.log("Is Online!");
console.log(Date.now());
}
return false;
}, function then() {
this.echo('Nobody calls me! SOB')
});
});
});
});
});
casper.run();