1
- var Nightmare = require ( 'nightmare' ) ;
2
- var hound = new Nightmare ( ) ;
1
+ var Nightmare = require ( 'nightmare' ) ,
2
+ readline = require ( 'readline' ) ;
3
+
4
+ var hound = new Nightmare ( {
5
+ cookiesFile : 'cookies.jar'
6
+ } ) ;
3
7
4
8
/*********** Change me! ***********/
5
9
var config = {
6
10
email : 'you can guess' ,
7
- password : 'what these are'
11
+ password : 'what these are' ,
12
+
13
+ siteUrl : 'https://stackoverflow.com' ,
14
+ roomUrl : 'https://chat.stackoverflow.com/rooms/1'
8
15
} ;
9
16
10
17
function once ( fn ) {
@@ -19,40 +26,106 @@ function once (fn) {
19
26
} ;
20
27
}
21
28
29
+ hound . drainQueue = function ( cb ) {
30
+ var self = hound ;
31
+
32
+ setTimeout ( next , 0 ) ;
33
+ function next ( err ) {
34
+ var item = self . queue . shift ( ) ;
35
+ if ( ! item ) {
36
+ cb && cb ( err , self ) ;
37
+ return ;
38
+ }
39
+
40
+ var method = item [ 0 ] ,
41
+ args = item [ 1 ] ;
42
+ args . push ( once ( next ) ) ;
43
+ method . apply ( self , args ) ;
44
+ }
45
+ } ;
46
+
47
+ function seLogin ( ) {
48
+ hound
49
+ . type ( '#se-login input[type="email"]' , config . email )
50
+ . type ( '#se-login input[type="password"]' , config . password )
51
+ . click ( '#se-login input[type="button"]' )
52
+ . wait ( )
53
+ . screenshot ( 'pics/login.png' ) ;
54
+ }
55
+ function injectToChat ( hound ) {
56
+ hound
57
+ . goto ( config . roomUrl )
58
+ . wait ( )
59
+ . screenshot ( 'pics/chat.png' )
60
+ . evaluate ( function ( ) {
61
+ var script = document . createElement ( 'script' ) ;
62
+ script . src = 'https://raw.github.com/Zirak/SO-ChatBot/master/master.js' ;
63
+ script . onload = function ( ) {
64
+ bot . activateDevMode ( ) ;
65
+ console . log ( 'Loaded bot' ) ;
66
+ bot . adapter . out . add ( 'I will derive!' ) ;
67
+ } ;
68
+ document . head . appendChild ( script ) ;
69
+ } , function ( ) {
70
+ console . log ( 'Injected chatbot.' ) ;
71
+ } ) ;
72
+ }
73
+
22
74
hound
23
- . goto ( 'https://stackoverflow.com /users/login/')
75
+ . goto ( config . siteUrl + ' /users/login/')
24
76
. screenshot ( 'pics/pre-login.png' )
25
- . type ( '#se-login input[type="email"]' , config . email )
26
- . type ( '#se-login input[type="password"]' , config . password )
27
- . click ( '#se-login input[type="button"]' )
28
- . wait ( )
29
- . screenshot ( 'pics/login.png' )
30
- . goto ( 'https://chat.stackoverflow.com/rooms/76070' )
31
- . screenshot ( 'pics/chat.png' )
32
- . evaluate ( function ( ) {
33
- var script = document . createElement ( 'script' ) ;
34
- script . src = 'https://raw.github.com/Zirak/SO-ChatBot/master/master.js' ;
35
- script . onload = function ( ) {
36
- console . log ( 'Loaded bot' ) ;
37
- } ;
38
- document . head . appendChild ( script ) ;
39
- } , function ( ) {
40
- console . log ( 'Injected chatbot.' ) ;
77
+ . wait ( 1000 )
78
+ . url ( function ( url ) {
79
+ if ( ! / l o g i n - a d d $ / . test ( url ) ) {
80
+ console . log ( 'Need to authenticate' ) ;
81
+ hound . use ( seLogin ) ;
82
+ }
83
+ else {
84
+ console . log ( 'Cool, already logged in' ) ;
85
+ }
86
+
87
+ hound . use ( injectToChat ) ;
88
+ hound . drainQueue ( function ( ) {
89
+ console . log ( 'Should be done loading stuff.' ) ;
90
+ hitTheRepl ( ) ;
91
+ } ) ;
41
92
} )
42
93
. setup ( function ( ) {
43
- var self = hound ;
44
-
45
- setTimeout ( next , 0 ) ;
46
- function next ( err ) {
47
- var item = self . queue . shift ( ) ;
48
- if ( ! item ) {
49
- console . log ( 'Should be done loading stuff.' ) ;
50
- return ;
51
- }
94
+ hound . drainQueue ( ) ;
95
+ } ) ;
52
96
53
- var method = item [ 0 ] ,
54
- args = item [ 1 ] ;
55
- args . push ( once ( next ) ) ;
56
- method . apply ( self , args ) ;
57
- }
97
+ function hitTheRepl ( ) {
98
+ var repl = readline . createInterface ( {
99
+ input : process . stdin ,
100
+ output : process . stdout
58
101
} ) ;
102
+
103
+ console . log ( 'You are now in a REPL with the remote page. Have fun!' ) ;
104
+
105
+ hound . on ( 'consoleMessage' , function ( msg ) {
106
+ console . log ( '<' , msg ) ;
107
+ } ) ;
108
+ hound . on ( 'error' , function ( msg ) {
109
+ console . log ( '! ' , msg ) ;
110
+ } ) ;
111
+
112
+ repl . on ( 'line' , function ( data ) {
113
+ hound . evaluate ( function ( code ) {
114
+ try {
115
+ return eval ( code ) ;
116
+ }
117
+ catch ( e ) {
118
+ return e . message ;
119
+ }
120
+ } , function ( res ) {
121
+ console . log ( '$' , res ) ;
122
+ repl . prompt ( ) ;
123
+ } , data ) . drainQueue ( ) ;
124
+ } ) ;
125
+ repl . on ( 'close' , function ( ) {
126
+ console . log ( 'Leaving the nightmare...' ) ;
127
+ hound . teardownInstance ( ) ;
128
+ } ) ;
129
+
130
+ repl . prompt ( ) ;
131
+ }
0 commit comments