From 9ca710fd98dc64bf2c94707fe6f65b458d386be2 Mon Sep 17 00:00:00 2001 From: Mara-Li Date: Wed, 3 Jan 2024 02:09:15 +0100 Subject: [PATCH] Update misc.js A tester --- src/js/misc.js | 45 +++++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/src/js/misc.js b/src/js/misc.js index 15aaffd..f1d0957 100644 --- a/src/js/misc.js +++ b/src/js/misc.js @@ -23,42 +23,31 @@ if (header_links) { * @param {string} alt * @param {number} originalWidth * @param {number} originalHeight - * @returns number[] + * @returns {width: number, height: number} */ function getHeightWidth(alt, originalWidth, originalHeight) { - const heightXwidthReg = new RegExp("\\d+x\\d+"); const widthReg = new RegExp("\\d+"); - if (alt.match(heightXwidthReg)) { - const width = parseInt(alt.split("x")[0]); - const height = parseInt(alt.split("x")[1]); - return [width, height]; - } else if (alt.match(widthReg)) { + if (alt.match(/\d+x\d+/i)) { + let width = parseInt(alt.split("x")[0]); + width = width > 0 ? width : originalWidth + let height = parseInt(alt.split("x")[1]); + height = height > 0 ? height : originalHeight + return {width, height}; + } else if (alt.match(/\d+/i)) { const width = parseInt(alt.match(widthReg)[0]); - return [width, originalHeight]; - } else { - return [originalWidth, originalHeight]; - } + return {width, height: originalHeight]; + } + return {width: originalWidth, height: originalHeight}; } for (const i of document.querySelectorAll("img")) { - const regAlt = new RegExp("\\|"); const alt = i.alt; - if (alt.match(regAlt)) { - const altSplitted = alt.split("|"); - for (const part of altSplitted) { - if (part.match(new RegExp("\\d+", "g"))) { - var size = getHeightWidth(part, i.width, i.height); - i.width = size[0] > 0 ? size[0] : i.width; - i.height = size[1] > 0 ? size[1] : i.height; - var partReg = new RegExp(`\\${part}`); - i.alt = alt.replace(partReg, ""); - } - } - } else if (alt !== i.title && alt.match(/^\d+(x\d+)?|\|\d+(x\d+)/g)) { - const size = getHeightWidth(alt, i.width, i.height); - i.width = size[0] > 0 ? size[0] : i.width; - i.height = size[1] > 0 ? size[1] : i.height; - i.alt = alt.replace(/^\d+(x\d+)?|\|\d+(x\d+)?/gi, ""); + const resize = /^\d+(x\d+)?|\|\d+(x\d+)/gi + if (alt.match(resize) && alt != i.title) { + const {width, height} = getHeightWidth(part, i.width, i.height); + i.width = width; + i.height = height; + i.alt = alt.replace(resize, ""); } }