forked from kton/Safeway-Just-for-u
-
Notifications
You must be signed in to change notification settings - Fork 0
/
src.js
59 lines (55 loc) · 2.35 KB
/
src.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
"use strict";
var promises = [];
var allcoupons = Object.values(JSON.parse(localStorage.getItem("abJ4uCoupons"))["objCoupons"]);
var coupons = allcoupons.filter(function(x){return x.status==="U";}).filter(function(y){return y.deleted!==0;});
if (coupons.length > 0) {
window.alert("clipping " + coupons.length + " of " + allcoupons.length + " coupons");
coupons.forEach(function(item){
var data = {"items":[]}, clip = {}, list = {};
clip.clipType="C";clip.itemId=item.offerId;clip.itemType=item.offerPgm;
list.clipType="L";list.itemId=item.offerId;list.itemType=item.offerPgm;
data.items.push(clip);data.items.push(list);
var request = new Request(window.AB.couponClipPath + "?storeId\x3d" + window.AB.userInfo.j4u.storeId, {
method: 'POST',
mode: 'cors',
redirect: 'error',
headers: new Headers(window.AB.j4uHttpOptions),
body: JSON.stringify(data)
});
var promise = fetch(request).then(function(response) {
return response.json();
})
.then(function(itemjson) {
if (itemjson.items[0]["status"] === 1) {
var wtf = JSON.parse(localStorage.getItem("abJ4uCoupons"));
if (wtf.objCoupons[item.offerId] && wtf.objCoupons[item.offerId]["offerId"] === item.offerId) {
wtf.objCoupons[item.offerId].status = "C";
localStorage.setItem("abJ4uCoupons", JSON.stringify(wtf));
} else {
var index = wtf.objCoupons.findIndex(function(obj){return obj.offerId === item.offerId});
if (index !== -1) {
wtf.objCoupons[index].status = "C";
localStorage.setItem("abJ4uCoupons", JSON.stringify(wtf));
}
}
}
});
promises.push(promise);
});
Promise.all(promises).then(function(){
if (Object.values(JSON.parse(localStorage.getItem("abJ4uCoupons"))["objCoupons"]).filter(function(x){return x.status==="U";}).filter(function(y){return y.deleted!==0;}).length > 0) {
window.alert("there are still some unclipped coupons - something probably broke this script");
} else {
window.alert("all coupons clipped - reloading page");
}
localStorage.removeItem("abCoupons");
localStorage.removeItem("abJ4uCoupons");
location.reload();
});
} else {
if (allcoupons.length > 0) {
window.alert("no clippable coupons");
} else {
window.alert("no coupons detected");
}
}