scripts/fontScript/index.js

34 lines
1.3 KiB
JavaScript
Raw Normal View History

2024-11-28 21:07:52 +01:00
(async () => {
2024-11-29 08:34:38 +01:00
// Use window.location.origin or fallback to "https://website.tld"
const website = window.location.origin || "https://website.tld";
2024-11-28 21:07:52 +01:00
try {
2024-11-29 08:34:38 +01:00
// Find the first <link> tag with an href ending in ".css"
const linkElement = Array.from(document.querySelectorAll('link'))
.find(link => link.href.endsWith('.css')); // Adjust condition if needed
if (!linkElement) {
console.log("No CSS file found on the page.");
return;
}
const cssUrl = linkElement.href; // Get the full URL of the CSS file
console.log(`Fetching CSS from: ${cssUrl}`);
2024-11-28 21:07:52 +01:00
// Fetch the CSS file
const response = await fetch(cssUrl);
const cssText = await response.text();
// Use a regular expression to extract the font file path
2024-11-28 21:10:40 +01:00
const fontMatch = cssText.match(/url\(['"]?(.*?\.(otf|ttf|woff|woff2|eot))['"]?\)/);
2024-11-28 21:07:52 +01:00
if (fontMatch && fontMatch[1]) {
const resolvedFontPath = new URL(fontMatch[1], cssUrl).href; // Resolve relative path
console.log(`Found font URL: ${resolvedFontPath}`);
} else {
console.log('Font not found in the CSS file.');
}
} catch (error) {
console.error('Error fetching or parsing the CSS file:', error);
}
})();