Skip to content

Commit

Permalink
bugfix: handle variable number of listings (#25)
Browse files Browse the repository at this point in the history
* bugfix: handle variable number of listing detail rows

Signed-off-by: Patrick Erichsen <patrick.a.erichsen@gmail.com>

* feat: add comment to

Signed-off-by: Patrick Erichsen <patrick.a.erichsen@gmail.com>
  • Loading branch information
Patrick-Erichsen authored Nov 6, 2021
1 parent 4f79e71 commit 132944f
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/content/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]');

Expand Down Expand Up @@ -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}`;
};
Expand All @@ -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);
}
};

Expand Down

0 comments on commit 132944f

Please # to comment.