refactor: move extractPageTitleFromURL to module, add tests

This commit is contained in:
xenticore
2025-03-27 14:51:13 -04:00
parent 335e45096f
commit 7c7e58d0d2
3 changed files with 37 additions and 9 deletions

View File

@@ -8,6 +8,7 @@ import { $T, extractLocaleFromURL } from './utils/localization'
import { fetchPrice, fetchKeyPrice, ItemPriceData } from './priceService'
import { createPriceRow, createStoreButton } from './uiRenderer'
import { findFirstElement, findFirstChildElement } from './utils/dom'
import { extractPageTitleFromURL } from './utils/url';
var itemSchema: ItemSchema | null;
var pageLocale: string = 'en'
@@ -23,15 +24,6 @@ function getKeyByValue(object: any, value: string) {
return Object.keys(object).find(key => object[key] === value);
}
function extractPageTitleFromURL(url: string): string {
var split = url.substring(url.indexOf("/wiki/") + "/wiki/".length);
if (split.indexOf('/') != -1) {
// Remove language suffix (/es)
split = split.substring(0, split.indexOf('/'));
}
return decodeURIComponent(split.replaceAll('_', ' '));
}
// Main function
async function inject() {
const itemInfobox = findFirstElement('.item-infobox');

8
src/content/utils/url.ts Normal file
View File

@@ -0,0 +1,8 @@
export function extractPageTitleFromURL(url: string): string {
var split = url.substring(url.indexOf("/wiki/") + "/wiki/".length);
if (split.indexOf('/') != -1) {
// Remove language suffix (/es)
split = split.substring(0, split.indexOf('/'));
}
return decodeURIComponent(split.replaceAll('_', ' '));
}