59 lines
1.7 KiB
TypeScript
59 lines
1.7 KiB
TypeScript
import { GalleryData } from "@/meta/data";
|
|
|
|
export const findVideoUrlInsideTagStringByFunctionNameAndExtension = (
|
|
tagBlock: string, functionName: string, extension: string): string | null => {
|
|
const start = tagBlock.indexOf(`html5player.${functionName}('`) + `html5player.${functionName}('`.length;
|
|
const end = tagBlock.toString().indexOf("'", start);
|
|
|
|
const substr = tagBlock.substring(start, end);
|
|
|
|
if (substr.includes(extension)) {
|
|
return substr
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
export const findRelatedVideos = (tagBlock: string): GalleryData[] | null => {
|
|
if (!(tagBlock.includes('video_related=['))) {
|
|
return null
|
|
}
|
|
|
|
// Trova l'inizio e la fine dell'array di oggetti nell'input
|
|
const start = tagBlock.indexOf('[{');
|
|
const end = tagBlock.lastIndexOf('}]') + 2;
|
|
|
|
// Estrai la sottostringa contenente l'array di oggetti
|
|
const jsonString = tagBlock.substring(start, end);
|
|
|
|
// Parsea la stringa JSON in un array di oggetti
|
|
const videoRelatedArray = JSON.parse(jsonString);
|
|
|
|
// Mappa ogni oggetto nell'array per rinominare le chiavi
|
|
//@ts-ignore
|
|
const parsedArray = videoRelatedArray.map(obj => ({
|
|
//@ts-ignore
|
|
videoUrl: obj.u,
|
|
imgUrl: obj.i,
|
|
text: obj.tf
|
|
}));
|
|
|
|
return parsedArray;
|
|
}
|
|
|
|
export const removeHttpS = (url: string): string => {
|
|
if (url.startsWith("http://")) {
|
|
return url.slice(7);
|
|
} else if (url.startsWith("https://")) {
|
|
return url.slice(8);
|
|
}
|
|
return url;
|
|
};
|
|
|
|
export const encodeVideoUrlPath = (input: string): string => {
|
|
return encodeURIComponent(input.replace(/^\/+/, ''))
|
|
};
|
|
|
|
export const decodeVideoUrlPath = (input: string): string => {
|
|
return `/${decodeURIComponent(input)}`;
|
|
}; |