From 2ca3aa7eca6854d61959b92a9270d4fb89613f4c Mon Sep 17 00:00:00 2001 From: André Oliveira <37463445+SpecialAro@users.noreply.github.com> Date: Tue, 12 Jul 2022 23:35:31 +0100 Subject: Change isImage function to link instead of URL (#470) * Change isImage function * Fix comments * Fix last comments and lint --- src/webview/lib/RecipeWebview.ts | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'src/webview') diff --git a/src/webview/lib/RecipeWebview.ts b/src/webview/lib/RecipeWebview.ts index 5701ca9c5..a896f1b6e 100644 --- a/src/webview/lib/RecipeWebview.ts +++ b/src/webview/lib/RecipeWebview.ts @@ -2,7 +2,9 @@ import { ipcRenderer } from 'electron'; import { BrowserWindow } from '@electron/remote'; import { pathExistsSync, readFileSync, existsSync } from 'fs-extra'; -const debug = require('../../preload-safe-debug')('Ferdium:Plugin:RecipeWebview'); +const debug = require('../../preload-safe-debug')( + 'Ferdium:Plugin:RecipeWebview', +); class RecipeWebview { badgeHandler: any; @@ -93,12 +95,27 @@ class RecipeWebview { } /** - * Find if url contains image + * Find if link contains image * * @param {string | number | undefined | null} text to be parsed */ - isImage(url): boolean { - return /\.(jpg|jpeg|png|webp|avif|gif|svg)$/.test(url.split(/[#?]/)[0]); + isImage(link): boolean { + if (typeof link === 'undefined') { + return false; + } + + const { role } = link.dataset; + + if (typeof role !== 'undefined') { + const roles = ['img']; + return roles.includes(role); + } + + const url = link.getAttribute('href'); + + const regex = /\.(jpg|jpeg|png|webp|avif|gif|svg)($|\?|:)/; + + return regex.test(url.split(/[#?]/)[0]); } /** -- cgit v1.2.3-54-g00ecf