diff --git a/bell.m4a b/bell.m4a new file mode 100644 index 0000000..886b812 Binary files /dev/null and b/bell.m4a differ diff --git a/index.html b/index.html new file mode 100644 index 0000000..51c30b4 --- /dev/null +++ b/index.html @@ -0,0 +1,20 @@ + + + + + + + TCS Watchman - Watchman exists to watch tracking info when they actually change. It can also trigger actions (playing ring sound). + + +
+ + +
+
+
+
+
+ + + \ No newline at end of file diff --git a/script.js b/script.js new file mode 100644 index 0000000..136120f --- /dev/null +++ b/script.js @@ -0,0 +1,117 @@ +const $shipmentDetails = document.querySelector('#shipmentDetails'); +const $checkpoints = document.querySelector('#checkpoints'); + +const $trackingForm = document.querySelector('#tracking'); + + +const $trackingNo = $trackingForm.querySelector('input'); + +$trackingForm.addEventListener('submit',formSubmitHandler); + +let latestCheckpoint = null; + +function shipmentDetailsHTML(trackingInfo) { + return `
+
Shipment Details:
+

Agent Reference Number : ${trackingInfo.referenceNo}

+

Origin : ${trackingInfo.origin}

+

Destination : ${trackingInfo.destination}

+

Booking Date : ${trackingInfo.bookingDate}

+

Shipper : ${trackingInfo.shipper}

+

Consignee : ${trackingInfo.consignee}

+
`; +} + +function trackHistoryHTML(checkpoints) { + return `
+
Track History:
+ + + + + + + + ${checkpoints.reduce((str, next) => { + return `${str}`; + }, '')} + +
Date TimeStatusLocation
${next.dateTime}${next.status}${next.recievedBy}
+
` +} + +async function watch() { + console.log('Watching ', new Date()) + const trackingNo = $trackingNo.value; + try { + const result = await track(trackingNo); + + $shipmentDetails.innerHTML = shipmentDetailsHTML(result['TrackDetailReply']['TrackInfo'][0]); + $checkpoints.innerHTML = trackHistoryHTML(result['TrackDetailReply']['Checkpoints']); + const isUpdated = setLatestCheckpoint(result['TrackDetailReply']['Checkpoints'][0]); + + if(isUpdated) { + console.log('Done ', new Date()) + playSound('bell.m4a'); + } else { + setTimeout(watch, 120 * 1000); + } + } catch (error) { + console.log(error); + watch(); + } + +} + + +function setLatestCheckpoint(checkpoint) { + let updated = latestCheckpoint !== null && JSON.stringify(latestCheckpoint) !== JSON.stringify(checkpoint); + latestCheckpoint = checkpoint; + + return updated; +} +function playSound(url) { + var a = new Audio(url); + a.play(); +} +async function formSubmitHandler(e) { + e.preventDefault(); + watch(); + return false; +} + + + +async function track(trackingNo) { + var myHeaders = new Headers(); + myHeaders.append("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0"); + myHeaders.append("Accept", "application/json, text/javascript, */*; q=0.01"); + myHeaders.append("Accept-Language", "en-US,en;q=0.5"); + myHeaders.append("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); + myHeaders.append("X-Requested-With", "XMLHttpRequest"); + myHeaders.append("Origin", "https://www.tcsexpress.com"); + myHeaders.append("Connection", "keep-alive"); + myHeaders.append("Referer", "https://www.tcsexpress.com/Tracking/"); + myHeaders.append("Cookie", "ASP.NET_SessionId=bkqes1zlizkzeneoi20lmbra"); + myHeaders.append("Sec-Fetch-Dest", "empty"); + myHeaders.append("Sec-Fetch-Mode", "cors"); + myHeaders.append("Sec-Fetch-Site", "same-origin"); + myHeaders.append("DNT", "1"); + myHeaders.append("Sec-GPC", "1"); + myHeaders.append("TE", "trailers"); + + var raw = `TrackingNumber=${trackingNo}`; + + var requestOptions = { + method: 'POST', + headers: myHeaders, + body: raw, + redirect: 'follow' + }; + + const response = await fetch("https://cors.bridged.cc/https://www.tcsexpress.com/Tracking/GetData", requestOptions); + return await response.json(); + // .then(response => response.text()) + // .then(result => console.log(result)) + // .catch(error => console.log('error', error)); +} \ No newline at end of file