Skip to content

Commit

Permalink
v7.0: Add support for bulk orders
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-mous committed Dec 21, 2020
1 parent 7b74108 commit 89bc547
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 57 deletions.
120 changes: 86 additions & 34 deletions PrinterPiExtension/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,76 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
{
let parseEbayRegular = () => {
let address = document.querySelector("#shipToAddress").innerText;
let shipping = document.querySelectorAll("._1nhS6BoY")[3].children[1].innerText;
shipping = parseFloat(shipping.substring(shipping.indexOf("$")+1)); //Shipping as a float
let grandTotal = document.querySelectorAll("._1nhS6BoY")[2].children[1].innerText; //Shipping+items+tax
grandTotal = parseFloat(grandTotal.substring(grandTotal.indexOf("$")+1));
let itemTotal = 0; //Total cost of items
let items = document.querySelectorAll(".-CkHbnvR");
let item_arr = [];
for (let i=0; i<items.length; i++) { //Iterate through the items
let itm = items[i].children[1];
let price = itm.children[3].innerText;
price = parseFloat(price.substring(price.indexOf("$")+1));
itemTotal += price;
item_arr.push({
desc: itm.children[0].innerText,
sku: itm.children[1].innerText.slice(5),
qty: itm.children[2].innerText.slice(5),
price: price
});
}
return {
to: address,
shipping: shipping,
subtotal: itemTotal,
tax: grandTotal-(shipping+itemTotal),
items: item_arr
}
}

try { //Try eBay
let address = document.querySelector("#shipToAddress").innerText;
let shipping = document.querySelectorAll("._1nhS6BoY")[3].children[1].innerText;
shipping = parseFloat(shipping.substring(shipping.indexOf("$")+1)); //Shipping as a float
let grandTotal = document.querySelectorAll("._1nhS6BoY")[2].children[1].innerText; //Shipping+items+tax
grandTotal = parseFloat(grandTotal.substring(grandTotal.indexOf("$")+1));
let itemTotal = 0; //Total cost of items
let items = document.querySelectorAll(".-CkHbnvR");
let item_arr = [];
for (let i=0; i<items.length; i++) { //Iterate through the items
let itm = items[i].children[1];
let price = itm.children[3].innerText;
price = parseFloat(price.substring(price.indexOf("$")+1));
itemTotal += price;
item_arr.push({
desc: itm.children[0].innerText,
sku: itm.children[1].innerText.slice(5),
qty: itm.children[2].innerText.slice(5),
price: price
const parseEbayBulk = () => {
let orders = [];
document.querySelectorAll(".orders-list__item__details").forEach((order) => {
let address = order.querySelector("address").innerText;
let items = [];
order.querySelectorAll(".item__description").forEach(item => {
items.push({
desc: item.children[0].innerText,
sku: item.querySelector(".item__details").children[0].innerText.slice(5),
qty: item.querySelector(".item__details").children[1].innerText.slice(5),
price: parseFloat(item.querySelector(".item__details").children[2].innerText.slice(11))
});
});
let shipping = order.querySelector(".buyer-paid-service").children[0].innerText.slice(1);

let itemTotal = items.length > 1 ? items.reduce((a, b) => (a .price|| a) + (b.price || b)) : items[0].price;

orders.push({
to: address,
shipping: shipping,
subtotal: itemTotal,
tax: 0, //No tax field available
items: items
})
});
if (orders.length == 0) {
throw new Error("No orders found");
}
return orders;
}
chrome.runtime.sendMessage({ //Send the first data
to: address,
shipping: shipping,
subtotal: itemTotal,
tax: grandTotal-(shipping+itemTotal),
items: item_arr
});
} catch (errA) {
try { //Otherwise, try PayPal

let parsePayPalRegular = () => {
//Get the items
let transaction = document.querySelector("#td_purchaseDetailsSeller").parentElement; //The main transaction purchase details
let items_arr = [];
let items = document.querySelectorAll(".item");
for (let i=1; i<items.length; i++) {
try {
console.log(items[i])
console.log(items[i])
let desc = items[i].children[0].children[0].innerText; //Description is first field
let price = items[i].children[0].children[1].innerText; //Price is second field
items_arr.push({
Expand Down Expand Up @@ -102,15 +133,36 @@
let addr_block = document.querySelector("#td_sellerShipAddress").parentElement;
let addr = addr_block.children[1].innerText + "\n" + addr_block.children[2].children[1].innerText; //Combine the name and address

chrome.runtime.sendMessage({
return {
to: addr,
shipping: shipping,
tax: tax,
subtotal: total,
items: items_arr
}
}
console.log("[PrinterPi] Parsing data...");
try { //Try eBay Regular
let data = parseEbayRegular();
chrome.runtime.sendMessage({
orders: [data]
});
} catch (errB) {
console.log("Error while trying to parse both eBay and PayPal pages. eBay:", errA, "PayPal:", errB);
chrome.runtime.sendMessage({error: "Not a valid page to parse", errMsg: [errA, errB]});
} catch (errA) {
try { //Otherwise, try eBay Bulk
let orders = parseEbayBulk();
chrome.runtime.sendMessage({
orders: orders
});
} catch (errB) { //Finally, try PayPal
try {
let data = parsePayPalRegular();
chrome.runtime.sendMessage({
orders: [data]
});
} catch (errC) {
console.log("[PrinterPi] Error while trying to parse both all pages. eBay Regular:", errA, "eBay Bulk:", errB, "PayPal Regular:", errC);
chrome.runtime.sendMessage({error: "Not a valid page to parse", errMsg: [errA, errB, errC]});
}
}
}
}
}
95 changes: 78 additions & 17 deletions PrinterPiExtension/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@
* @param {string} resError Response error (if any)
*/

const VERSION_NO = 6.0; //Current version
const VERSION_NO = 7.0; //Current version

let orders = []; //Orders received from background script

/*
Data format to send to PrinterPi (NOTE: item descs and skus MUST NOT CONTAIN ~ and NO EXTRA SPACES ARE ALLOWED before/after parameters):
Expand All @@ -88,9 +90,11 @@ const VERSION_NO = 6.0; //Current version
* @param {onFinish} onFinish On finish callback
*/
let readStorageData = (onFinish) => {
chrome.storage.local.get(["data"], (res) => {
if (res.data) {
setData(res.data);
chrome.storage.local.get(["orders"], (res) => {
if (res.orders) {
orders = res.orders;
showOrders();
selectOrder(0);
document.getElementById("more-info").innerHTML = "Data read from storage";
} else {
document.getElementById("more-info").innerHTML = "No data in storage";
Expand All @@ -109,7 +113,7 @@ let readStorageData = (onFinish) => {
*/
let setStorageData = () => {
if (validateInputs()) {
chrome.storage.local.set({data: getData()}, () => {
chrome.storage.local.set({orders: orders}, () => {
document.getElementById("more-info").innerHTML = "Data saved";
});
}
Expand Down Expand Up @@ -329,6 +333,46 @@ let getData = () => { //Read the data from the HTML page
return data;
}

/**
* Show a (global) order based on it's index
*
* @param {number} orderIndex
*/
let selectOrder = (orderIndex) => {
console.log(orders);
setData({
to: orders[orderIndex]?.to || "",
shipping: orders[orderIndex]?.shipping || "0",
tax: orders[orderIndex]?.tax || "0",
subtotal: orders[orderIndex]?.subtotal || "0",
items: orders[orderIndex]?.items || [],
});
validateInputs();
}

/**
* Remove the current order (from global orders)
*/
const removeCurrentOrder = () => {
orders.splice(document.getElementById("order-select").value, 1);
showOrders();
selectOrder(0);
}

/**
* Show the (global) orders in the select
*/
const showOrders = () => {
document.getElementById('order-select').innerHTML = "";
orders.forEach((_, i) => {
let opt = document.createElement("OPTION");
opt.value = i;
opt.innerHTML = `Order ${i+1}`;
opt.selected = i==0;
document.getElementById('order-select').appendChild(opt);
});
}

/**
* Get the complete data Packet
*
Expand Down Expand Up @@ -456,12 +500,14 @@ let validateInputs = () => {

if (err_msg) { //Display any errors and disable the submit button
document.getElementById("print-button").disabled = true;
document.getElementById("print-all-button").disabled = true;
let done_msg = document.getElementById("done-msg");
done_msg.innerHTML = err_msg;
done_msg.classList = "text-danger";
return false;
} else { //No errors - remove any lock on the submit button
document.getElementById("print-button").disabled = false;
document.getElementById("print-all-button").disabled = false;
let done_msg = document.getElementById("done-msg");
done_msg.innerHTML = "Not connected";
done_msg.classList = "text-info";
Expand Down Expand Up @@ -552,14 +598,16 @@ let parseFile = (ev) => {
if (data.version >= 6.0) { //Tax support added in version 6.0
tax = data.tax
}
setData({ //Set the data packet
orders = [];
orders.push({ //Set the data packet
to: data.to,
shipping: data.shipping,
tax: tax,
subtotal: data.subtotal,
items: data.items,
});
validateInputs();
showOrders();
selectOrder(0);
document.getElementById("more-info").innerHTML = "Data loaded from file"; //Show the error message
}
reader.readAsText(files[0]);
Expand All @@ -574,6 +622,13 @@ window.onload = () => { //Add event listeners, etc.
let pkt = getPacket(settings);
if (pkt) sendData(pkt);
});
document.getElementById('print-all-button').addEventListener('click', () => { //Get the data packet and send it
for (let orderIndex in orders) {
selectOrder(orderIndex);
let pkt = getPacket(settings);
if (pkt) sendData(pkt);
}
});
document.getElementById('envelope-button').addEventListener('click', () => { //Get the data packet and print it
let pkt = getPacket(settings);
if (pkt) printEnvelope(pkt);
Expand All @@ -590,6 +645,8 @@ window.onload = () => { //Add event listeners, etc.
done_msg.innerHTML = "Please configure the printer settings in the Setting page (via the button PrinterPi Settings below)";
done_msg.classList = "text-danger";
});
document.getElementById('order-select').addEventListener("change", (e) => selectOrder(e.target.value));
document.getElementById('remove-button').addEventListener("click", removeCurrentOrder);
document.getElementById('save-button').addEventListener('click', setStorageData);
document.getElementById('parse-button').addEventListener('click', parsePage); //Execute the background parsing script
document.getElementById('file-button').addEventListener('click', () => document.getElementById('file-dialog').click()); //Parse a file for the receipt
Expand All @@ -611,14 +668,18 @@ chrome.runtime.onMessage.addListener((msg) => { //Listen for messages and set th
document.getElementById("more-info").innerHTML += ". Not a valid page to parse."; //Show the error message
});
document.getElementById('parse-button').disabled = true;
} else {
setData({ //Set the data packet
to: msg.to,
shipping: msg.shipping,
tax: msg.tax,
subtotal: msg.subtotal,
items: msg.items,
});
validateInputs();
} else if (msg.orders) {
if (msg.orders.length >= 1) {
setData({ //Set the data packet
to: msg.orders[0].to,
shipping: msg.orders[0].shipping,
tax: msg.orders[0].tax,
subtotal: msg.orders[0].subtotal,
items: msg.orders[0].items,
});
orders = msg.orders;
showOrders();
validateInputs();
}
}
});
});
14 changes: 10 additions & 4 deletions PrinterPiExtension/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ <h5 class="text-center h5">
</h5>
<hr class="py-1 my-1">
<h6 class="text-info text-center" id="more-info"></h6>
<div class="px-5 py-1 row">
<button class="btn btn-primary col mx-4" id='print-button'>Print Receipt</button>
<div class="px-3 py-1 row">
<button class="btn btn-primary col mx-2" id='print-button'>Print Receipt</button>
<button class="btn btn-primary col mx-2" id='print-all-button'>Print All Receipts</button>
</div>
<div class="px-5 py-1 row">
<button class="btn btn-sm btn-primary col mx-4" id='envelope-button'>Print Envelope</button>
<div class="px-3 py-1 row">
<button class="btn btn-sm btn-primary col mx-2" id='envelope-button'>Print Envelope</button>
</div>
<div class="px-3 pt-2 pb-1 row">
<button class="btn btn-sm btn-warning col mx-2" id='parse-button'>Parse Page</button>
Expand All @@ -31,6 +32,11 @@ <h6 class="text-info text-center" id="more-info"></h6>
</div>
<input type="file" style="display: none" id="file-dialog" accept=".json" />

<hr class="py-1 my-1">
<h5 class="text-center p-0 pt-1 m-0">Current Order</h5>
<select class="form-control" id="order-select">
</select>
<button class="btn btn-sm btn-warning col mx-2" id='remove-button'>Remove order</button>
<hr class="py-1 my-1">
<h5 class="text-center p-0 pt-1 m-0">Address</h5>
<textarea class="form-control h-100" rows="4" id="Address"></textarea>
Expand Down
2 changes: 1 addition & 1 deletion PrinterPiExtension/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "PrinterPi",
"version": "6.2",
"version": "7.0",
"description": "Parse eBay Print Shipping Label or PayPal Activity pages and send the data to an application that controls a receipt printer",

"options_page": "options.html",
Expand Down
2 changes: 1 addition & 1 deletion PrinterPiExtension/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<form class="container py-2" style="max-width: 450px" id="settings-form">
<h3 class="title text-center font-weight-bold">PrinterPi Settings</h2>
<hr class="py-1 my-1">
<h6 class="text-center text-muted">Version: 6.0</h6>
<h6 class="text-center text-muted">Version: 7.0</h6>
<hr class="py-1 my-1">
<h6 class="text-center"><span id="info-msg">Loading...</span></h6>
<hr class="py-1 my-1">
Expand Down

0 comments on commit 89bc547

Please sign in to comment.