-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProduct.js
192 lines (155 loc) · 5.06 KB
/
Product.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
class Product {
name;
image;
price;
rate;
number_of_rates;
seller;
url;
update_product;
in_stock=true;
#token;
storage_promise;
constructor() {
this.storage_promise= this.get_storage_data()
}
/**
* urls on the server api.
* @returns {string}
*/
update_url () {
return `${this.url}/api/products/update`
};
get_product () {
return `${this.url}/api/products/get`
};
create_product () {
return `${this.url}/api/products/create`
};
/**
* get the data saved in the browser
* @returns {Promise<void>}
*/
async get_storage_data(){
var result= await chrome.storage.sync.get()
this.#token= result.token;
this.url=result.url;
this.update_product=result.update_product;
}
async update_server_product(){
await this.storage_promise
if (!this.update_product)
return
fetch(this.update_url() ,{
method: 'POST',
headers:{
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${this.#token}`,
},
body:JSON.stringify({
url : window.location.href,
current_price: self.price
})
})
.catch(error => {
// Handle errors
console.log("Error:", error);
});
}
async get_product_data(){
await this.storage_promise
return fetch(this.get_product() ,{
method: 'POST',
headers:{
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${this.#token}`,
},
body:JSON.stringify({
url : window.location.href
})
})
.then((response) => {
return response.json();
})
.catch(error => {
// product doesn't exist in the system
});
}
submit_form(){
document.body.querySelector("#gray_layout").classList.add("show_flex")
fetch(this.create_product(), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${this.#token}`,
},
body: JSON.stringify({
url: window.location.href ,
name: this.name,
image: this.image,
notify_price: document.getElementById("notify_price").value,
official_seller: document.getElementById("official_seller").checked,
favourite: document.getElementById("favourite").checked,
stock_available: document.getElementById("stock_available").checked,
lowest_within: document.getElementById("lowest_within").value,
number_of_rates:this.number_of_rates ?? 0,
price:this?.price ?? 0,
})
})
.then(response => {
return response.json();
})
.then(data => {
if (data.errors)
add_notification_to_page('danger' , 'Something wrong Happened')
else
add_notification_to_page('success' ,
`<p> ${ data.message}</p>
<p>You can check it from the following link </p>
<p>
<a href='${data.link} '> ${data.link} </a>
</p>`
)
})
.catch(error => {
add_notification_to_page('danger' , 'Something wrong Happened here')
});
}
get_dom_product_details(){}
populate_dom_with_charts(){}
refresh_dom_all(){
product.get_dom_product_details()
product.update_server_product().then(response => {
console.log("updated server")
})
document
.querySelectorAll("#gray_layout , #discount_bandit_show, #chart , #all_stores_cards")
?.forEach((elem)=>{
elem.remove()
})
product.populate_dom_with_charts()
setTimeout(() => {
document.getElementById("submit_discount_form")
.addEventListener("click" , function (){
product.submit_form()
})
}, 2000);
product.get_product_data().then(response => {
})
}
}
let previousUrl = null;
const observer = new MutationObserver(function(mutations) {
current_url=new URL(location.href)
if (current_url.pathname !== previousUrl?.pathname) {
previousUrl = new URL(location.href);
product.refresh_dom_all()
}
});
const config = {subtree: true, childList: true};
observer.observe(document, config);
//share the url across all classes
var current_url = new URL(window.location.href);