-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathireserve.js
319 lines (291 loc) · 8.13 KB
/
ireserve.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//iPhoneModel 0 : iPhone 6
//iPhoneModel 1 : iPhone 6 Plus
var iphoneModel = 1
//capacity = [16G, 64G, 128G]
var capacity = [0,0,1]
//Select Color Priority: 0 = Silver, 1 = Gold, 2 = Space Grey
var colorPos = [1,0,2];
//Select Color Priority: 1 = CWB, 2 = FW, 3 = IFC
var shopPriority = [1,3,2];
//User Information
var firstname = "Kwan Yat";
var lastname = "Wong";
var email = "[email protected]";
var phone = "61780236";
var govId = "R1512012";
/* Global variables */
var mainProcess;
var shopIdx = 0;
var store_hash = {};
$.ajax({
url:"availability.json",
dataType:"json",
success:function(data){
console.log(data.R120);
$("#text1").append("Updated at: "+data.updated+"<br>");
$.each(data, function (storeId, store_value) {
var product_hash = {};
$.each(store_value,function(productNo,value){
product_hash[productNo] = value;
/*
if (value == false) {
$("#text1").append(productConvertor(productNo)+": "+"false"+"<br>");
}else{
$("#text1").append(productConvertor(productNo)+": "+"true"+"<br>");
}
*/
});
store_hash[storeId] = product_hash
});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("");
}
// To use hashmap: var value = store_hash['storeId']['productNo'] <== true / false;
});
// R428 = IFC
// R409 = CWB
// R485 = KLN
// Prior by yourself
function PriorProduct(product_idx) {
if (product_idx >= product_list.length) return;
for (var i = 0; i < store_list.length; i++) {
if ( store_hash[store_list[i]][product_list[product_idx]] == "true" ) {
// do something
return;
}
}
PriorProduct(++product_idx);
}
function codeInjector(injection){
var injectedCode = injection;
var script = document.createElement('script');
script.appendChild(document.createTextNode('('+ injectedCode +')();'));
(document.body || document.head || document.documentElement).appendChild(script);
}
function setShop(){
var disabled = $("#productSelection li select[name='selectedStoreNumber'] option").closest("li").hasClass("disabled");
if(!disabled){
var shops = [];
$("#productSelection li select[name='selectedStoreNumber'] option").each(function(index){
shops[index] = $(this).val();
})
console.log('Set Shop = ' + shops[shopPriority[shopIdx]]);
$("#productSelection li select[name='selectedStoreNumber']").each(function(index){
codeInjector("$('#productSelection li select[name=\"selectedStoreNumber\"]').val('" + shops[shopPriority[shopIdx]] + "').trigger('change')");
})
return true
}else{
return false
}
}
function setIphone(){
var disabled = $("#productSelection #product").closest("li").hasClass("disabled");
if(!disabled){
codeInjector("$('#productSelection #product ul li:eq(" + iphoneModel + ") input').click()");
return true
}else{
return false
}
}
function setNoContract(){
var disabled = $("#productSelection #locked").closest("li").hasClass("disabled");
if(!disabled){
codeInjector('$(\"#productSelection #locked ul li:eq(0) input\").click()');
return true
}else{
return false
}
}
var i = 0;
var colorIndexing = [];
function setColor(){
if( i <= 2 ){
var disabled = $("#productSelection #color").closest("li").hasClass("disabled")
if( !disabled ){
$("#productSelection #color li:visible").each(function(index){
colorIndexing[index] = $(this).index();
})
codeInjector("$('#productSelection #color input:eq(\""+colorIndexing[colorPos[i]]+"\")').click()");
i++;
return true
}else{
return false
}
}else{
i = 0;
rollback(3);
return false
}
}
//var modelIndex = 0;
//return 0 = option disabled ,1 = No Stock, 2 = Selected
function setModel(){
var disabled = $("#productSelection #model").closest('li').hasClass('disabled');
if( !disabled ){
var modelSelected = 1;
$("#productSelection #model ul li:visible input").each(function(i){
if( $(this).attr("disabled") == undefined && capacity[i] == 1 ){
$(this).click().trigger('change');
modelSelected = 2;
return false
}
})
return modelSelected
}else{
return 0
}
}
function setQuan(){
var disabled = $("#productSelection li select[name='selectedQuantity']").closest('li').hasClass('disabled');
if( !disabled ){
$("#productSelection li select[name='selectedQuantity']").each(function(index){
$(this).val("2").trigger('change');
})
return true
}else{
return false
}
}
function setInfo(){
$("#productSelection #contact input[name='firstName']").val( firstname ).trigger('change');
$("#productSelection #contact input[name='lastName']").val( lastname ).trigger('change');
$("#productSelection #contact input[name='email']").val( email ).trigger('change');
codeInjector("$('#productSelection #contact input[name=\"phoneNumber\"]').val('" + phone + "').trigger('change').click()");
return true
}
function setGovId() {
$("#productSelection li select[name='selectedGovtIdType'] option").closest("li").hasClass("disabled");
var disabled = $("#productSelection li select[name=\"selectedGovtIdType\"] option").closest("li").hasClass("disabled");
if ( !disabled ) {
var gov_type = [];
$("#productSelection li select[name='selectedGovtIdType'] option").each(function(index) {
gov_type[index] = $(this).val();
});
console.log('Gov Type = ' + gov_type);
console.log('Type = ' + gov_type[1]);
$("#productSelection li select[name='selectedGovtIdType']").each(function(index) {
codeInjector("$('#productSelection li select[name=\"selectedGovtIdType\"]').val('" + gov_type[1] + "').trigger('change')");
});
codeInjector("$('#productSelection #govt-id input[name=\"govtId\"]').val('" + govId + "').trigger('change').click()");
return true;
} else {
return false;
}
}
function setTimeslot(){
var exsist = $("#productSelection li select[name='selectedTimeSlotId'] option").closest('li');
if( exsist != undefined){
var timeslot = [];
$("#productSelection li select[name='selectedTimeSlotId'] option").each(function(index){
timeslot[index] = $(this).val();
})
codeInjector("$('#productSelection li select[name=\"selectedTimeSlotId\"]\').each(function(index){ $(this).val('"+timeslot[9]+"').trigger('change') })");
return true
}else{
return false
}
}
function submit(){
$("#globalfooter").nextAll('script');
$(".button-container a.button").click();
$("#productSelection .button-container a").click();
stop();
}
function stop(){
clearInterval(mainProcess);
}
function rollback(currentStep){
console.log('rollback');
switch(step) {
case 1:
step--
if(shopIdx < shopPriority.length-1)
shopIdx++;
else
stop();
break;
case 3:
step = 0
if(shopIdx < shopPriority.length-1)
shopIdx++;
else
stop();
break;
}
}
var step = 0;
var lastStep = 0;
var stepCount = 0;
var modelAttepted = 0;
function mainThread(){
console.log('step ' + step);
switch(step) {
case 0:
if(setShop())
step++;
break;
case 1:
if(setIphone())
step++;
break;
case 2:
if(setNoContract())
step++;
break;
case 3:
if(setColor())
step++;
break;
case 4:
var k = setModel()
if( k == 1)
step--;
else if( k == 2)
step++;
break;
case 5:
if(setQuan())
step++;
break;
case 6:
if(setInfo())
step++;
break;
case 7:
if(setGovId())
step++;
break;
case 8:
if(setTimeslot())
step++;
break;
case 9:
submit();
break;
}
if(step == lastStep)
stepCount++;
else
lastStep = step;
if(stepCount > 10 && step != 9){
rollback(step);
stepCount = 0;
}
}
function bootup(){
if($(".loading-container:visible").length == 0){
if($("#productSelection").length > 0 && $("#productSelection").attr("method") == "post"){
mainProcess = setInterval(function(){mainThread()},500);
}
return true
}
return false
}
var bootstrap = setInterval(function(){
var started = bootup();
if(started)
clearInterval(bootstrap);
else
console.log('waiting for the website...');
},500);