From 1dd48de3edbc8f546a78d0425ba3c7e88cdef485 Mon Sep 17 00:00:00 2001 From: YNWA Fawzy <38886749+Cybrarist@users.noreply.github.com> Date: Fri, 23 Aug 2024 10:28:03 +0400 Subject: [PATCH 1/4] V2.0 --- Product.js | 170 +++++++++++ Stores/Amazon.js | 69 +++++ Stores/Argos.js | 88 ++++++ Stores/Ebay.js | 71 +++++ Stores/Noon.js | 72 +++++ functions.js | 148 +++++++++ manifest.json | 31 +- options.html | 16 + resources/css/style.css | 194 ++++++++++-- resources/images/bandit_square.png | Bin 0 -> 30186 bytes resources/images/stores/amazon.png | Bin 0 -> 2170 bytes resources/images/stores/amazon_ae.png | Bin 0 -> 2492 bytes resources/images/stores/amazon_ca.png | Bin 0 -> 2451 bytes resources/images/stores/amazon_co_jp.png | Bin 0 -> 2717 bytes resources/images/stores/amazon_co_uk.png | Bin 0 -> 2706 bytes resources/images/stores/amazon_com_au.png | Bin 0 -> 2874 bytes resources/images/stores/amazon_com_br.png | Bin 0 -> 2839 bytes resources/images/stores/amazon_com_mx.png | Bin 0 -> 2885 bytes resources/images/stores/amazon_com_tr.png | Bin 0 -> 2787 bytes resources/images/stores/amazon_de.png | Bin 0 -> 18374 bytes resources/images/stores/amazon_eg.png | Bin 0 -> 2510 bytes resources/images/stores/amazon_es.png | Bin 0 -> 2471 bytes resources/images/stores/amazon_fr.png | Bin 0 -> 2378 bytes resources/images/stores/amazon_in.png | Bin 0 -> 2325 bytes resources/images/stores/amazon_it.png | Bin 0 -> 2335 bytes resources/images/stores/amazon_nl.png | Bin 0 -> 2336 bytes resources/images/stores/amazon_pl.png | Bin 0 -> 12462 bytes resources/images/stores/amazon_sa.png | Bin 0 -> 2460 bytes resources/images/stores/amazon_se.png | Bin 0 -> 2483 bytes resources/images/stores/amazon_sg.png | Bin 0 -> 2473 bytes resources/images/stores/argos.png | Bin 0 -> 13668 bytes resources/images/stores/costco.png | Bin 0 -> 36274 bytes resources/images/stores/currys.png | Bin 0 -> 2937 bytes resources/images/stores/diy_com.svg | 1 + resources/images/stores/ebay.png | Bin 0 -> 6903 bytes resources/images/stores/noon.svg | 20 ++ resources/images/stores/walmart.png | Bin 0 -> 3908 bytes resources/js/options.js | 2 + stores.js | 350 ++++++++++++++++++++++ 39 files changed, 1197 insertions(+), 35 deletions(-) create mode 100644 Product.js create mode 100644 Stores/Amazon.js create mode 100644 Stores/Argos.js create mode 100644 Stores/Ebay.js create mode 100644 Stores/Noon.js create mode 100644 resources/images/bandit_square.png create mode 100644 resources/images/stores/amazon.png create mode 100644 resources/images/stores/amazon_ae.png create mode 100644 resources/images/stores/amazon_ca.png create mode 100644 resources/images/stores/amazon_co_jp.png create mode 100644 resources/images/stores/amazon_co_uk.png create mode 100644 resources/images/stores/amazon_com_au.png create mode 100644 resources/images/stores/amazon_com_br.png create mode 100644 resources/images/stores/amazon_com_mx.png create mode 100644 resources/images/stores/amazon_com_tr.png create mode 100644 resources/images/stores/amazon_de.png create mode 100644 resources/images/stores/amazon_eg.png create mode 100644 resources/images/stores/amazon_es.png create mode 100644 resources/images/stores/amazon_fr.png create mode 100644 resources/images/stores/amazon_in.png create mode 100644 resources/images/stores/amazon_it.png create mode 100644 resources/images/stores/amazon_nl.png create mode 100644 resources/images/stores/amazon_pl.png create mode 100644 resources/images/stores/amazon_sa.png create mode 100644 resources/images/stores/amazon_se.png create mode 100644 resources/images/stores/amazon_sg.png create mode 100644 resources/images/stores/argos.png create mode 100644 resources/images/stores/costco.png create mode 100644 resources/images/stores/currys.png create mode 100644 resources/images/stores/diy_com.svg create mode 100644 resources/images/stores/ebay.png create mode 100644 resources/images/stores/noon.svg create mode 100644 resources/images/stores/walmart.png create mode 100644 stores.js diff --git a/Product.js b/Product.js new file mode 100644 index 0000000..196b165 --- /dev/null +++ b/Product.js @@ -0,0 +1,170 @@ +class Product { + name; + image; + price; + rate; + number_of_rates; + url; + update_product; + #token; + + storage_promise; + + constructor() { + this.storage_promise= this.get_storage_data() + } + + update_url () { + return `${this.url}/api/products/update` + }; + get_product () { + return `${this.url}/api/products/get` + }; + create_product () { + return `${this.url}/api/products/create` + }; + + async get_storage_data(){ + var result= await browser.storage.sync.get() + this.#token= result.token; + this.url=result.url; + this.update_product=result.update_product; + + console.log(this.#token) + } + + async update_server_product(){ + await this.storage_promise + if (!self.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 => { + // Handle errors + console.log("Error:", error); + }); + } + + 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, + price:this?.price ?? 0, + }) + }) + .then(response => { + return response.json(); + }) + .then(data => { + console.log(data); + if (data.errors) + add_notification_to_page('danger' , 'Something wrong Happened') + else + add_notification_to_page('success' , + `
${ data.message}
+You can check it from the following link
++ ${data.link} +
` + ) + + }) + .catch(error => { + add_notification_to_page('danger' , 'Something wrong Happened') + }); + } + + 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() + + document.getElementById("submit_discount_form") + .addEventListener("click" , function (){ + product.submit_form() + }) + + product.get_product_data().then(response => { + + }) + } + +} + + +let previousUrl = ''; +const observer = new MutationObserver(function(mutations) { + if (location.href !== previousUrl) { + previousUrl = location.href; + product.refresh_dom_all() + } +}); +const config = {subtree: true, childList: true}; +observer.observe(document, config); + + + diff --git a/Stores/Amazon.js b/Stores/Amazon.js new file mode 100644 index 0000000..a0d28cf --- /dev/null +++ b/Stores/Amazon.js @@ -0,0 +1,69 @@ +if (window.location.href.includes('amazon.') ){ + + class Amazon extends Product { + constructor() { + super(); + } + + get_dom_product_details(){ + this.name= document.getElementById("productTitle").textContent.trim() + this.price= document.getElementById("twister-plus-price-data-price").value + this.image= document.querySelector(".imgTagWrapper img").src + this.number_of_rates=document.getElementById("acrCustomerReviewText") + .textContent.split(" ")[0] + .replaceAll(",","") + .replaceAll("." , "") + } + + populate_dom_with_charts() { + + document.body.insertAdjacentHTML("afterbegin" , `+ + ${current_store.currency} ${data.highest_price.toLocaleString()} +
++ + ${current_store.currency} ${data.lowest_price.toLocaleString()} +
+gL(=zS~7tUi}z=%R`W1EnEBbpN4T9(e?B