visit
So let's get right into it.
For the purpose of this post, we'd be automating this //invoice-generator.com/ website that allows you to create invoices online.
We will open our DevTools to be able to inspect elements and get a JS selector to access any element we want to work on. For instance, to get the invoice number
input element, we right-click on the element in the Elements tab in DevTools and select copy JS path copy
List of elements we will be interacting with
const invoiceNumber = document.querySelector(
"div.papers > div div.title > div > div > input"
);
const billTo = document.querySelector(
"div.papers > div > div:nth-child(1) > div.col.contact-info > div.row.bill-to-details > div:nth-child(1) textarea"
);
const invoiceFrom = document.querySelector(
"div.papers > div > div:nth-child(1) > div.col.contact-info > div.contact.from textarea"
);
// date format "Dec 9,2021"
const dateInput = document.querySelector("#dp82");
const itemDescription = document.querySelector(
"div.papers div.items-holder > div.items-table div.name textarea"
);
const itemQuantity = document.querySelector(
"div.papers div.items-holder > div.items-table div.quantity > input"
);
const itemRate = document.querySelector(
"div.papers div.items-holder > div.items-table div.unit_cost input"
);
Then we'd create functions to perform the input value operations
function promptsForUser() {
invoiceNumber.value = prompt("Enter the invoic number");
itemQuantity.value = prompt("Total hours worked");
itemQuantity.dispatchEvent(new Event("change"))
}
function setConstantValue() {
let d = new Date();
const months = ["Jan","Feb","Mar","April","May","June","Aug","Sept","Oct","Nov","Dec",];
dateInput.value = `${months[d.getMonth() - 1]} ${d.getDate()}, ${d.getFullYear()}`;
invoiceFrom.value = "Ichigo Kurosaki,\nSubstitute Soul Reaper";
billTo.value = "XYZ Limited,\nMugen Train District";
itemDescription.value = "Bankai night shift";
itemRate.value = "10";
}
promptsForUser()
setConstantValue()
The setConstantConstant
function changes the value of the input elements to values that stay constant mostly through the use of the script and the promptsForUser
function to retrieve some information from the script user.
Also, the total amount in the invoice listens to a change
event from the quantity so we manually dispatched a change
event.
We'd find the Snippet editor from the Sources tab in DevTools
Create a new snippet and paste all our code within.
You can inspect Local Storage, Cookies, pause the javascript execution and play around with the debugger.