From 132944f1391d44cc8a680354289ebb41d9676177 Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Sat, 6 Nov 2021 12:47:57 -0500 Subject: [PATCH] bugfix: handle variable number of listings (#25) * bugfix: handle variable number of listing detail rows Signed-off-by: Patrick Erichsen * feat: add comment to Signed-off-by: Patrick Erichsen --- src/content/utils/dom.ts | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/content/utils/dom.ts b/src/content/utils/dom.ts index f1b9eed..49cb776 100644 --- a/src/content/utils/dom.ts +++ b/src/content/utils/dom.ts @@ -4,10 +4,6 @@ export const OFFIE_NODE_ID_PREFIX = 'offie-node'; export const LISTINGS_FOOTER_SECTION_ID = 'EXPLORE_NUMBERED_PAGINATION'; export const ITEM_LIST_EL = 'itemListElement'; -const NUM_ROWS_WITH_BADGE = 6; -const NUM_ROWS_WITH_BUILDING_INFO = 5; -const NUM_ROWS_WITHOUT_BUILDING_INFO = 4; - export const getListingId = (listing: Element): string | null => { const urlMetaTag = listing.querySelector('meta[itemprop=url]'); @@ -138,6 +134,22 @@ export const insertBeforeBadge = ( badgeContainer.insertBefore(offieNode, displayBadge); }; +/** + * Checks if the `listingDetails` param has a `Rare find` badge. + * + * The hueristic we use is whether or not an `svg` element is found. + * + * The `Rare find` badge has a diamond SVG. A regular row containing either + * room info or building info only contains `span` elements. + */ +export const hasRareFindBadge = (listingDetails: HTMLElement): boolean => { + const secondToLast = listingDetails.children[ + listingDetails.childElementCount - 2 + ] as HTMLElement; + + return !!secondToLast.querySelector('svg'); +}; + export const getOffieNodeId = (listingId: string): string => { return `${OFFIE_NODE_ID_PREFIX}-${listingId}`; }; @@ -159,17 +171,10 @@ export const insertOffieNode = (listingId: string): void => { const offieNode = document.createElement('div'); offieNode.id = getOffieNodeId(listingId); - switch (listingDetails.childElementCount) { - case NUM_ROWS_WITH_BADGE: - insertBeforeBadge(listingDetails, offieNode); - break; - case NUM_ROWS_WITH_BUILDING_INFO || NUM_ROWS_WITHOUT_BUILDING_INFO: - insertNewDetailsRow(listingDetails, offieNode); - break; - default: - throw new Error( - `Failed to find expected number of rows for listing: ${listingId}` - ); + if (hasRareFindBadge(listingDetails)) { + insertBeforeBadge(listingDetails, offieNode); + } else { + insertNewDetailsRow(listingDetails, offieNode); } };