0
0
mirror of https://github.com/Maks1mS/free-ozon-dpr.git synced 2024-12-24 19:04:44 +03:00
free-ozon-dpr/scripts/utils.js

66 lines
1.4 KiB
JavaScript
Raw Normal View History

2024-06-06 19:38:01 +03:00
import { JSDOM } from "jsdom";
async function resolveVKcc(url) {
const response = await fetch(url);
const text = await response.text();
const match = new RegExp(/value=["']([^"']+)["']/).exec(text);
if (match && match.length > 1) {
return match[1];
} else {
return;
}
}
2024-06-05 14:56:19 +03:00
export async function getFinalURL(url) {
2024-06-06 19:38:01 +03:00
if (url?.startsWith('https://vk.cc')) {
url = await resolveVKcc(url);
}
if (!url) {
return;
}
let response = await fetch(url, {
method: "HEAD",
redirect: "manual",
});
while (
response.status >= 300 &&
response.status < 400 &&
response.headers.get("location") &&
!url.startsWith('https://ozon.ru/point/')
) {
url = response.headers.get("location");
if (!url.startsWith("http")) {
const baseUrl = new URL(response.url);
url = `${baseUrl.protocol}//${baseUrl.host}${url}`;
}
response = await fetch(url, {
2024-06-05 14:56:19 +03:00
method: "HEAD",
redirect: "manual",
});
2024-06-06 19:38:01 +03:00
}
return url;
}
export async function getTelegramMessage(input) {
const url = new URL(input);
url.search = "?embed=1&mode=tme";
const res = await fetch(url);
const htmlText = await res.text();
const dom = new JSDOM(htmlText);
const document = dom.window.document;
const message = document.querySelector(".tgme_widget_message");
return {
text: message.querySelector("div.tgme_widget_message_text.js-message_text")
.innerHTML,
};
}