From eeb8bbd5f6a01b23cb5f2b2a2abda013049f12da Mon Sep 17 00:00:00 2001 From: James Date: Thu, 11 Jan 2024 18:04:32 +0000 Subject: [PATCH] fix: mamba errors --- src/contents/relay.ts | 15 +++++++-------- src/contentsHelpers/ImageHandler.ts | 14 +++++++++++++- src/contentsHelpers/MambaJamba.ts | 27 ++++++++++++++------------- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/contents/relay.ts b/src/contents/relay.ts index e68c27c..543401c 100644 --- a/src/contents/relay.ts +++ b/src/contents/relay.ts @@ -1,42 +1,41 @@ import { relayMessage } from '@plasmohq/messaging'; - relayMessage( { name: 'pong', - } + }, ); relayMessage( { name: 'getImages', - } + }, ); relayMessage( { name: 'getImageInfo', - } + }, ); relayMessage( { name: 'sendAnalyticsEvent', - } + }, ); relayMessage( { name: 'getProfile', - } + }, ); relayMessage( { name: 'getPeople', - } + }, ); relayMessage({ name: 'bumbleID', -}) \ No newline at end of file +}); diff --git a/src/contentsHelpers/ImageHandler.ts b/src/contentsHelpers/ImageHandler.ts index 929bc4e..14ff1c6 100644 --- a/src/contentsHelpers/ImageHandler.ts +++ b/src/contentsHelpers/ImageHandler.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-await-in-loop */ /* eslint-disable class-methods-use-this */ /* eslint-disable consistent-return */ /* eslint-disable no-restricted-syntax */ @@ -47,6 +48,8 @@ class ImageHandler { site: Sites; + requestedImages: boolean = false; + /** * @param {Boolean} debug */ @@ -107,6 +110,7 @@ class ImageHandler { * Adds the images to the images array, then it prunes the old ones off (if the array gets to big) */ addNewImage(image: ImageType | ImageType[]) { + console.log('adding new image', image); if (!image) return; if (Array.isArray(image)) { this.images.push(...image); @@ -146,11 +150,19 @@ class ImageHandler { try { while (true) { // eslint-disable-next-line no-await-in-loop + + let complete = false; + + if (!this.requestedImages) { + complete = true; + this.requestedImages = true; + } + const imageData = await sendToBackground({ name: 'getImages', body: { site: this.site, - complete: this.images.length === 0, + complete, } as getImagesRequest, }); if (debug) console.log(`Successfully got images for ${Sites[this.site]}, ${imageData.images.length} images`); diff --git a/src/contentsHelpers/MambaJamba.ts b/src/contentsHelpers/MambaJamba.ts index cebe40c..22c1508 100644 --- a/src/contentsHelpers/MambaJamba.ts +++ b/src/contentsHelpers/MambaJamba.ts @@ -1,6 +1,7 @@ +/* eslint-disable class-methods-use-this */ import ImageHandler from '@/contentsHelpers/ImageHandler'; import { createMambaOverlayNode } from './Misc'; -import { Sites } from '@/misc/types'; +import { Sites, type ImageType } from '@/misc/types'; /** * This class handles the image overlay for mamba.ru @@ -37,28 +38,28 @@ class MambaJamba extends ImageHandler { const image = this.imagesOnPage[i]; const existingOverlay = image.parentElement.querySelector('div.overlayBox, div.topBox'); if (!existingOverlay) { - this.createOverlay(image); + const imageSrc = (image as HTMLImageElement).src; + const record = this.images.find((x) => x.url === imageSrc); + if (!record) { + console.log(`record not found in createOverlayNode ${imageSrc}`); + } else { + this.createOverlay(image, record); + } } else { // check to see if the overlay 'aria-url' matches the current image const overlayURL = existingOverlay.getAttribute('aria-label'); - const imageURL = this.images.find((x) => x.url === image.getAttribute('src'))?.url; - if (overlayURL !== imageURL) { + const record = this.images.find((x) => x.url === image.getAttribute('src')); + if (overlayURL !== record?.url && record) { // if it doesn't, then remove the overlay and create a new one existingOverlay.remove(); - this.createOverlay(image); + this.createOverlay(image, record); } } } } - async createOverlay(image: Element): Promise { - const imageSrc = image.getAttribute('src'); - const imageRecord = this.images.find((x) => x.url === imageSrc); - if (!imageRecord) { - console.log(`imageRecord not found in createOverlayNode ${imageSrc}`); - return null; - } - const overlayNode = createMambaOverlayNode(image, imageRecord); + async createOverlay(image: Element, record: ImageType): Promise { + const overlayNode = createMambaOverlayNode(image, record); // append the overlay to the parent of the image image.parentElement.appendChild(overlayNode);