-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
172 lines (155 loc) · 4.78 KB
/
background.js
File metadata and controls
172 lines (155 loc) · 4.78 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
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
addToCart = (tabId) => {
var obj = JSON.parse(localStorage["data"]);
var data = Object.keys(obj).length;
var index = Object.keys(obj)[0];
if (data > 1){
var next_url = 'http://www.supremenewyork.com/shop/all/' + obj[Object.keys(obj)[1]]["category"];
}
chrome.tabs.executeScript(tabId, { file: "src/jquery-3.2.1.min.js"});
chrome.tabs.executeScript(tabId, { file: "scripts/addtocart.js"}, function(){
chrome.tabs.executeScript(tabId, {
code: 'submitForm('+tabId+','+data+');'
});
});
if (data > 0){
delete obj[index];
localStorage["data"] = JSON.stringify(obj);
}
}
var listenerHandle = function() {
return { cancel: true }
}
function removeImages() {
chrome.webRequest.onBeforeRequest.addListener(
listenerHandle,
{
urls: [
'*://*.cloudfront.net/*.jpg',
'*://*.cloudfront.net/*.png'
]
},
['blocking']
)
}
updateTab = (tabId, url, callback) => {
chrome.tabs.update(tabId, { url: url }, () => {
chrome.tabs.onUpdated.addListener(function listenTab(tabnumber, info, tab) {
if (tab.url.indexOf(url) > -1 && info.status == "complete") {
if (JSON.parse(localStorage["customer_data"])["removeImages"] == true){
// chrome.tabs.executeScript(tabId, { file: "scripts/replaceImages.js"});
removeImages();
}
else{
chrome.webRequest.onBeforeRequest.removeListener(listenerHandle);
}
chrome.tabs.onUpdated.removeListener(listenTab);
callback(tabId);
}
})
})
};
check = (tabId) => {
data = localStorage["customer_data"];
chrome.tabs.executeScript(tabId, { file: "scripts/checkout.js"}, function(){
chrome.tabs.executeScript(tabId, {
code: 'checkout('+data+');'
});
});
};
item = (tabId) => {
var obj = JSON.parse(localStorage["data"]);
var index = Object.keys(obj)[0];
items_size = obj[index]["size"];
chrome.tabs.executeScript(tabId, { file: "src/jquery-3.2.1.min.js"});
chrome.tabs.executeScript(tabId, { file: "scripts/choose.js"}, function(){
chrome.tabs.executeScript(tabId, {
code: 'found('+'"'+items_size+'"'+');'
});
});
};
find = (tabId) => {
var obj = JSON.parse(localStorage["data"]);
var index = Object.keys(obj)[0];
var data = JSON.stringify(obj[index]);
chrome.tabs.executeScript(tabId, { file: "scripts/finditem.js"}, function(){
chrome.tabs.executeScript(tabId, {
code: 'finditem('+data+');'
});
});
items_size = obj[index]["size"];
chrome.tabs.executeScript(tabId, { file: "scripts/choose.js"}, function(){
chrome.tabs.executeScript(tabId, {
code: 'found('+'"'+items_size+'"'+');'
});
});
};
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse) {
switch(request.msg) {
case 'checkout':
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
tabId = tabs[0].id;
});
updateTab(tabId, request.url, check);
sendResponse(request.url);
break
case 'goitem':
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
tabId = tabs[0].id;
});
updateTab(tabId, request.url, item);
sendResponse(request.url);
break
case 'add':
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
tabId = tabs[0].id;
});
addToCart(tabId);
sendResponse(request.url);
break
case 'go':
tabId = request.id;
if (localStorage["data"] == undefined){
chrome.runtime.sendMessage({msg:'error', error: 'you have no items'}, function submitForm(par){
console.log(par);
});
}
else{
var obj = JSON.parse(localStorage["data"]);
var index = Object.keys(obj).length;
if (index > 3){
localStorage["data"] = JSON.stringify({});
}
else{
if (index !== 0){
localStorage["repeat"] = JSON.stringify(0);
var url = 'http://www.supremenewyork.com/shop/all/' + obj[Object.keys(obj)[0]]["category"];
updateTab(tabId, url, find);
}
}
}
sendResponse();
break
case 'repeat':
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
tabId = tabs[0].id;
});
var i = JSON.parse(localStorage["repeat"]);
var obj = JSON.parse(localStorage["data"]);
if (i < 3){
i = i + 1;
localStorage["repeat"] = JSON.stringify(i);
var url = 'http://www.supremenewyork.com/shop/all/' + obj[Object.keys(obj)[0]]["category"];
updateTab(tabId, url, find);
}
else{
chrome.runtime.sendMessage({msg:'error', error: 'No item!'}, function sendResponse(error){
console.log("No item");
alert("No item");
});
}
case 'error':
sendResponse(request.error);
break
}
});