@@ -36,53 +36,140 @@ function capture(name) {
36
36
} ) ;
37
37
}
38
38
39
+ function reportErrors ( fn ) {
40
+ return function ( ) {
41
+ try {
42
+ return fn . apply ( this , arguments ) ;
43
+ } catch ( e ) {
44
+ console . log ( e ) ;
45
+ throw e ;
46
+ }
47
+ }
48
+ }
49
+
50
+
51
+ ( reportErrors ( function take10s ( ) {
52
+ var generated = [ ] ,
53
+ alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" . split ( "" ) ,
54
+ i , j ;
39
55
56
+ for ( i = 0 ; i < alphabet . length ; i ++ ) {
57
+ for ( j = 0 ; j < alphabet . length ; j ++ ) {
58
+ generated . push ( {
59
+ code : "TAKE10" + alphabet [ i ] + alphabet [ j ] , description : "Generated TAKE10 code"
60
+ } ) ;
61
+ }
62
+ }
63
+
64
+ [ ] . push . apply ( codes , generated ) ;
65
+
66
+ } ) ( ) ) ;
40
67
41
68
function getCodes ( ) {
42
- var pageCodes = this . getElementsAttribute ( "input.voucherReveal-peel-bottom-code" , "value" ) ,
43
- pageDescs = this . getElementsInfo ( "p.thread-title-text" ) . map ( function ( info ) {
44
- return info . text ;
45
- } ) ,
69
+ var sections = _ . compact ( this . evaluate ( function ( ) {
70
+ return $ ( "article.thread--voucher" ) . toArray ( ) . map ( function ( el ) {
71
+ return $ ( el ) . find ( "[data-voucher-button]" ) . data ( ) && $ ( el ) . attr ( "id" ) || null ;
72
+ } ) ;
73
+ } ) ) ,
74
+ pageCodes = this . evaluate ( function ( sections ) {
75
+ return sections . map ( function ( id ) {
76
+ return {
77
+ id : $ ( "#" + id ) . find ( "[data-voucher-button]" ) . data ( ) . voucherButton . id ,
78
+
79
+ desc : $ ( "#" + id ) . find ( "strong.thread-title" ) . text ( ) . trim ( )
80
+ . replace ( / \s { 2 , } / g, " " ) . replace ( "INSTRUCTIONS: " , "" )
81
+ . replace ( " Read more" , "" )
82
+ } ;
83
+ } ) ;
84
+ } , sections ) ,
46
85
pageCodeData ;
47
- if ( pageCodes . length !== pageDescs . length ) {
48
- this . die (
49
- "Error: pageCodes.length !== pageDescs.length: " +
50
- pageCodes . length + " !== " + pageDescs . length
51
- ) ;
52
- }
53
86
54
- casper . echo ( "Got codes: " + pageCodes ) ;
55
-
56
- _ ( pageCodes ) . zip ( pageDescs ) . each ( function ( codeAndDesc ) {
57
- var multiCodes = codeAndDesc [ 0 ] . trim ( ) . split ( " " ) ;
58
- _ ( multiCodes ) . each ( function ( code ) {
59
- if ( code ) {
60
- codes . push ( {
61
- code : code ,
62
- description : codeAndDesc [ 1 ]
63
- } ) ;
64
- }
87
+ _ ( pageCodes ) . each ( function ( pageCode ) {
88
+
89
+ say ( "getting voucher for id " + pageCode . id ) ;
90
+
91
+ casper . then ( function ( ) {
92
+ this . evaluate ( function ( id ) {
93
+ $ . cookie ( "show_voucher" , id ) ;
94
+ } , pageCode . id ) ;
95
+ } ) ;
96
+
97
+ say ( "reloading to show popup" ) ;
98
+
99
+ casper . then ( function ( ) {
100
+ this . reload ( ) ;
101
+ } ) ;
102
+
103
+ say ( "waiting for popup with code for id " + pageCode . id ) ;
104
+
105
+ casper . waitForSelector ( "div.popover-content span.voucher-code" ) ;
106
+
107
+ say ( "scriaping popup with code for id " + pageCode . id ) ;
108
+
109
+ casper . then ( reportErrors ( function ( ) {
110
+ var code = casper . getElementInfo ( "div.popover-content span.voucher-code" ) . text ;
111
+ var multiCodes = code . trim ( ) . replace ( / " / g, "" ) . split ( " " ) ;
112
+ _ ( multiCodes ) . each ( function ( code ) {
113
+ if ( code ) {
114
+ codes . push ( {
115
+ code : code ,
116
+ description : pageCode . desc
117
+ } ) ;
118
+ }
119
+ } ) ;
120
+
121
+ this . echo (
122
+ "popup with code for id " + pageCode . id + " is '" + code + "' with description '" +
123
+ pageCode . desc + "'"
124
+ ) ;
125
+ } ) ) ;
126
+ } ) ;
127
+
128
+ say ( "removing cookie" ) ;
129
+
130
+ casper . then ( function ( ) {
131
+ this . evaluate ( function ( ) {
132
+ $ . removeCookie ( "show_voucher" ) ;
65
133
} ) ;
66
134
} ) ;
135
+
136
+ say ( "reloading to hide popup" ) ;
137
+
138
+ casper . then ( function ( ) {
139
+ this . reload ( ) ;
140
+ } ) ;
67
141
}
68
142
69
- casper . start ( "http://www.hotukdeals.com/vouchers/dominos.co.uk" ) ;
143
+ var pageNumSelector = "#pagination > nav > div > span.tGrid-cell.tGrid-cell--shrink" +
144
+ ".vAlign--all-m.space--h-4.space--v-1.text--color-brandPrimary > button > span.hide--toW2" ;
70
145
71
- say ( "on voucher page ") ;
146
+ casper . start ( "https://www.hotukdeals.com/vouchers/dominos.co.uk ") ;
72
147
73
- for ( currCodePage = 0 ; currCodePage < numCodePages ; currCodePage ++ )
74
- {
75
- capture ( "Voucher code page " + ( currCodePage + 1 ) + " of " + numCodePages ) ;
148
+ say ( "waiting for load" ) ;
76
149
77
- casper . then ( getCodes ) ;
150
+ casper . waitForSelector ( "[data-voucher-button]" ) ;
78
151
79
- say ( "--- going to next page" ) ;
152
+ casper . then ( reportErrors ( getCodes ) ) ;
80
153
81
- casper . thenClick ( "a.paginationButton--arrow-next" ) ;
154
+ for ( currCodePage = 1 ; currCodePage < numCodePages ; currCodePage ++ )
155
+ {
156
+ capture ( "Voucher code page " + ( currCodePage + 1 ) + " of " + numCodePages ) ;
82
157
83
- casper . waitForSelectorTextChange ( "span.paginationButton--current" ) ;
158
+ casper . then ( function ( ) {
159
+ say ( "going to next page" ) ;
160
+
161
+ if ( casper . visible ( pageNumSelector ) ) {
162
+ casper . thenClick ( "a[rel='next']" ) ;
163
+ casper . waitForSelectorTextChange ( pageNumSelector ) ;
164
+ casper . then ( reportErrors ( getCodes ) ) ;
165
+ } else {
166
+ casper . say ( "no more pages of codes, breaking early." ) ;
167
+ return ;
168
+ }
169
+ } ) ;
84
170
}
85
171
172
+
86
173
casper . thenOpen ( "https://www.dominos.co.uk" ) ;
87
174
88
175
capture ( "Dominos homepage" ) ;
@@ -91,11 +178,11 @@ casper.waitForSelector("#store-finder-search");
91
178
92
179
say ( "searching for store" ) ;
93
180
94
- casper . then ( function ( ) {
181
+ casper . then ( reportErrors ( function ( ) {
95
182
this . fillSelectors ( "#store-finder-search" , {
96
183
"input[type='text']" : POSTCODE
97
184
} ) ;
98
- } ) ;
185
+ } ) ) ;
99
186
100
187
capture ( "find store" ) ;
101
188
@@ -135,9 +222,9 @@ capture("enter vouchers");
135
222
136
223
say ( "entering codes..." ) ;
137
224
138
- casper . then ( function ( ) {
225
+ casper . then ( reportErrors ( function ( ) {
139
226
140
- codes . forEach ( function ( code ) {
227
+ codes . forEach ( reportErrors ( function ( code ) {
141
228
142
229
say ( "entering code: " + code . code ) ;
143
230
@@ -152,9 +239,9 @@ casper.then(function () {
152
239
this . then ( function ( ) {
153
240
code . status = this . fetchText ( "div.voucher-code-input > p.help-block" ) . trim ( ) ;
154
241
} ) ;
155
-
242
+
156
243
this . then ( function ( ) {
157
- // Clear the voucher, if it was successfully added, so subsequent vouchers can be
244
+ // Clear the voucher, if it was successfully added, so subsequent vouchers can be
158
245
// checked.
159
246
if ( this . exists ( "[data-voucher] .basket-product-actions button" ) ) {
160
247
say ( "voucher worked! clearing voucher" ) ;
@@ -167,20 +254,20 @@ casper.then(function () {
167
254
this . waitWhileSelector ( "[data-voucher] .basket-product-actions button" ) ;
168
255
}
169
256
} ) ;
170
- } . bind ( this ) ) ;
171
- } ) ;
257
+ } ) . bind ( this ) ) ;
258
+ } ) ) ;
172
259
173
260
capture ( "finish" ) ;
174
261
175
- casper . then ( function ( ) {
262
+ casper . then ( reportErrors ( function ( ) {
176
263
var working = codes . filter ( function ( code ) {
177
264
return ! / i n v a l i d | e x p i r e d | V o u c h e r U s e d | a l r e a d y b e e n u s e d / i. test ( code . status ) ;
178
265
} ) . map ( function ( code ) {
179
266
return code . description + " [" + code . code + "]" ;
180
267
} ) ;
181
268
182
269
this . echo ( codes . map ( function ( code ) {
183
- return code . description + " [ " + code . code + "] = \"" + code . status + "\"" ;
270
+ return "[" + code . code + "] " + code . description + " = \"" + code . status + "\"" ;
184
271
} ) . join ( "\n" ) ) ;
185
272
186
273
this . echo ( "\n\n=======Working vouchers (" + working . length + "/" + codes . length + ")========" ) ;
@@ -192,6 +279,6 @@ casper.then(function () {
192
279
} else {
193
280
casper . exit ( 100 ) ;
194
281
}
195
- } ) ;
282
+ } ) ) ;
196
283
197
284
casper . run ( ) ;
0 commit comments