|
| 1 | +// This code is rushed more as a proof of concept than a finished product. |
| 2 | +// It is not optimized and could be improved in many ways. |
| 3 | +// It is also not tested on all possible cases and could break in some situations. |
| 4 | + |
| 5 | +const informationRowSelector = ".row.information-row"; |
| 6 | +const fareCellDetailsSelector = ".fare-cell-details-information"; |
| 7 | +const priceSelector = ".amount-grp.fare-value"; |
| 8 | +const statusCreditSelector = |
| 9 | + ".e2e-fCondition-statuscredits .e2e-statuscredits-amount"; |
| 10 | + |
| 11 | +// Check if double status credits are enabled from the popover.js |
| 12 | +let doubleStatusEnabled = false; |
| 13 | +chrome.storage.local.get(["doubleStatus"], (result) => { |
| 14 | + doubleStatusEnabled = result.doubleStatus || false; |
| 15 | +}); |
| 16 | + |
| 17 | +// Listen for changes to the dom for when flight information is expanded showing the status credits |
| 18 | +const observer = new MutationObserver((mutations) => { |
| 19 | + mutations.forEach((mutation) => { |
| 20 | + mutation.addedNodes.forEach((node) => { |
| 21 | + if (node.nodeType !== Node.ELEMENT_NODE) return; |
| 22 | + if (node.matches(informationRowSelector) == false) return; |
| 23 | + addInformationToFlight(node); |
| 24 | + }); |
| 25 | + }); |
| 26 | +}); |
| 27 | + |
| 28 | +// Logic for querying the status credit, and price doms. |
| 29 | +function addInformationToFlight(node) { |
| 30 | + const detailItems = node.querySelectorAll(fareCellDetailsSelector); |
| 31 | + |
| 32 | + // Get all prices for this row |
| 33 | + const container = node.closest("upsell-itinerary-avail"); |
| 34 | + if (!container) return; |
| 35 | + |
| 36 | + const prices = Array.from(container.querySelectorAll(priceSelector)); |
| 37 | + // Process each details item |
| 38 | + detailItems.forEach((detailItem, index) => { |
| 39 | + const statusElement = detailItem.querySelector(statusCreditSelector); |
| 40 | + if (statusElement == false && prices[index] == false) return; |
| 41 | + |
| 42 | + let price = getPrice(prices[index]); |
| 43 | + let statusCredits = parseInt(statusElement.innerText); |
| 44 | + // Check if statusCredits is a valid number |
| 45 | + if (isNaN(statusCredits)) return; |
| 46 | + |
| 47 | + if (doubleStatusEnabled) { |
| 48 | + statusCredits *= 2; |
| 49 | + updateDoubleStatusHTML(statusCredits, statusElement); |
| 50 | + } |
| 51 | + |
| 52 | + if (!isNaN(price) && statusCredits) { |
| 53 | + setConversionHTML(statusElement, price, statusCredits); |
| 54 | + } |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +// Show the final calculation as a new div under the original status credits centre aligned. |
| 59 | +function setConversionHTML(statusElement, price, statusCredits) { |
| 60 | + const dollarPerCredit = (price / statusCredits).toFixed(2); |
| 61 | + |
| 62 | + let calculationDiv = document.createElement("div"); |
| 63 | + calculationDiv.style.fontSize = "14px"; |
| 64 | + calculationDiv.style.fontWeight = "bold"; |
| 65 | + calculationDiv.style.color = "#666"; |
| 66 | + calculationDiv.textContent = `$${dollarPerCredit}/SC`; |
| 67 | + |
| 68 | + statusElement.parentNode.insertBefore( |
| 69 | + calculationDiv, |
| 70 | + statusElement.nextSibling |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +// If double status credits are enabled then show some UI changes |
| 75 | +function updateDoubleStatusHTML(statusCredits, statusElement) { |
| 76 | + // Create a span for the old value with strikethrough |
| 77 | + const oldSpan = document.createElement("span"); |
| 78 | + oldSpan.style.textDecoration = "line-through"; |
| 79 | + oldSpan.textContent = statusElement.innerText; |
| 80 | + |
| 81 | + // Create a new span for the doubled value |
| 82 | + const doubledSpan = document.createElement("span"); |
| 83 | + doubledSpan.style.color = "red"; |
| 84 | + doubledSpan.textContent = ` ${statusCredits}`; |
| 85 | + |
| 86 | + // Clear the current content of statusElement |
| 87 | + statusElement.innerHTML = ""; |
| 88 | + |
| 89 | + // Append the old and new spans to the statusElement |
| 90 | + statusElement.appendChild(oldSpan); |
| 91 | + statusElement.appendChild(doubledSpan); |
| 92 | +} |
| 93 | + |
| 94 | +// Get the price of a flight from the element, factoring in for flights that have no pricing. |
| 95 | +function getPrice(node) { |
| 96 | + // Check if no seats are available |
| 97 | + if (node.querySelector(".no-seat")) { |
| 98 | + return NaN; |
| 99 | + } |
| 100 | + // Get the price of the flight |
| 101 | + const price = node.querySelector(".amount.cash").innerText; |
| 102 | + return parseFloat(price.replace(/[^0-9.]/g, "")); |
| 103 | +} |
| 104 | + |
| 105 | +// Observe the document for changes |
| 106 | +observer.observe(document, { childList: true, subtree: true }); |
0 commit comments