30 lines
1.2 KiB
JavaScript
30 lines
1.2 KiB
JavaScript
// Utilities for scraping data from the Lodestone
|
|
|
|
import {regExpEscape} from '../util/regexp.js';
|
|
|
|
/**
|
|
* Creates a regular expression that matches a link to the database entry of the
|
|
* given type and name, and captures its EDB ID from the matched link's `href`
|
|
* attribute.
|
|
* @param {'item' | 'quest'} type
|
|
* @param {string} name
|
|
* @returns {RegExp}
|
|
*/
|
|
const edbEntryLinkRegExp = (type, name) => new RegExp(`<a href="/lodestone/playguide/db/${regExpEscape(type)}/(?<id>[a-z0-9]+)[^"]+"[^>]*>(?<name>${regExpEscape(name)})</a>`, 'i');
|
|
|
|
/**
|
|
* Gets the ID of the Eorzea Database entry with the given type and name.
|
|
* @param {'item' | 'quest'} type
|
|
* @param {string} name
|
|
* @returns {Promise<string | null>}
|
|
*/
|
|
export async function findEDBEntryID (type, name) {
|
|
// execute a search for the entry's name and type
|
|
const searchURL = `https://na.finalfantasyxiv.com/lodestone/playguide/db/${type}/?q=${encodeURIComponent(name.replace(/\([^)]+\)|[&-]/g, ' '))}`;
|
|
const response = await fetch(searchURL);
|
|
const body = await response.text();
|
|
// find an `<a>` in the HTML response whose text exactly matches the name
|
|
const match = body.match(edbEntryLinkRegExp(type, name));
|
|
// return the ID parsed from the URL in the `href` attribute
|
|
return match?.groups.id;
|
|
}
|