-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamazon.js
204 lines (164 loc) · 6.64 KB
/
amazon.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
function show_form_discount_bandit(){
document.body.querySelector(".gray_layout").style.display="flex"
}
//Variables
let product_name = document.getElementById("productTitle").textContent.trim()
//Populate Elements on the page
//Chart
let header = document.createElement("div");
header.setAttribute("id" , "chart")
document.body.querySelector("#ppd").after(header)
//Button To Show Form To Add / Edit
let add_to_system= document.createElement("img")
add_to_system.setAttribute( "src" , "https://raw.githubusercontent.com/Cybrarist/Discount-Bandit/master/storage/app/public/bandit.png")
add_to_system.setAttribute( "id" , "discount_bandit_show")
add_to_system.style.maxWidth= "50px"
add_to_system.addEventListener("click", function (){
show_form_discount_bandit()
})
document.body.querySelector("#productTitle").after(add_to_system)
//background layout
let gray_layout = document.createElement("div")
gray_layout.setAttribute("class" , "gray_layout")
let messages=`
<div class="success-message">
</div>
<div class="danger-message">
Error Has Happened, please check the logs
</div>`
document.body.insertAdjacentHTML("beforebegin" , messages)
let form_string=`
<div class="form_background">
<div class="form_field">
<label> Product Name </label>
<input name="product_name" id="product_name" type="text" value="${product_name}">
</div>
<div class="form_field">
<label> Notify Price </label>
<input name="notify_price" id="notify_price" type="number" value="" min="0" >
</div>
<div class="form_field">
<label> Official Seller Only </label>
<input name="official_seller" id="official_seller" type="checkbox" value="">
</div>
<div class="form_field">
<label> Add To Favourite </label>
<input name="favourite" id="favourite" type="checkbox" value="">
</div>
<div class="form_field">
<label> Alert When Stock Available </label>
<input name="stock_available" id="stock_available" type="checkbox" value="">
</div>
<div class="form_field">
<label> Alert If Product Is lowest Within </label>
<input name="lowest_within" id="lowest_within" type="number" value="" step="1">
</div>
<button class="submit_amazon"> Save </button>
</div>
`
gray_layout.innerHTML = form_string
gray_layout.addEventListener("click" , function (e){
if (e.target === this)
this.style.display="none"
})
document.body.prepend(gray_layout)
var currentURL = window.location.href;
//Get the current product pricing chart
chrome.storage.sync.get().then(function (result) {
fetch(`${result.url}/api/products/get-product` ,{
method: 'POST',
headers:{
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${result.token}`,
},
body:JSON.stringify({
url : currentURL
})
})
.then(response => {
return response.json();
})
.then(data => {
var options = {
chart:data.chart,
series: data.series,
xaxis: data.xaxis,
stroke: {
curve: 'smooth'
},
dataLabels: {
enabled: false,
},
legend: {
position: 'top'
}
}
// var options = {
// series: data.series,
// chart: data.chart,
// colors: data.colors,
//
//
// };
var chart = new ApexCharts(document.querySelector("#chart"), options);
chart.render();
document.getElementById("corePrice_feature_div").insertAdjacentHTML("afterbegin" ,
`
<div class="lowest_price" > Lowest Price ${data.prices.price_min / 100}</div>
<div class="max_price" "> Lowest Price ${data.prices.price_max / 100}</div>
`
)
})
.catch(error => {
// Handle errors
console.log("Error:", error);
});
}, function (error){
console.log(`Error: ${error}`);
})
//events
document.querySelector(".submit_amazon").addEventListener("click" , function (){
chrome.storage.sync.get().then(function (result) {
document.getElementsByClassName("success-message")[0].style.display="none"
document.getElementsByClassName("danger-message")[0].style.display="none"
document.getElementsByClassName("gray_layout")[0].style.display="none"
fetch(`${result.url}/api/products/create/amazon`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${result.token}`,
},
body: JSON.stringify({
url: currentURL ,
product_name: document.getElementById("product_name").value,
product_image: document.getElementById("landingImage").src,
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: document.getElementById("acrCustomerReviewText")
.textContent.split(" ")[0]
.replaceAll(",","")
.replaceAll("." , ""),
})
})
.then(response => {
return response.json();
})
.then(data => {
if (data.errors)
document.getElementsByClassName("danger-message")[0].style.display="flex"
else{
document.getElementsByClassName("success-message")[0].innerHTML=
`<p> ${ data.message} </p> <p>You can check it from the following link </p> <p><a href='${data.link} '> ${data.link} </a></p>`
document.getElementsByClassName("success-message")[0].style.display="flex"
}
})
.catch(error => {
document.getElementsByClassName("danger-message")[0].style.display="flex"
});
})
})