You've already forked tf2wikipricing
refactor: move extractPageTitleFromURL to module, add tests
This commit is contained in:
28
__tests__/url.test.ts
Normal file
28
__tests__/url.test.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import { extractPageTitleFromURL } from '../src/content/utils/url';
|
||||||
|
|
||||||
|
describe('extractPageTitleFromURL', () => {
|
||||||
|
it('extracts simple title from URL', () => {
|
||||||
|
const url = 'https://wiki.teamfortress.com/wiki/Scattergun';
|
||||||
|
expect(extractPageTitleFromURL(url)).toBe('Scattergun');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('replaces underscores with spaces', () => {
|
||||||
|
const url = 'https://wiki.teamfortress.com/wiki/Flame_Thrower';
|
||||||
|
expect(extractPageTitleFromURL(url)).toBe('Flame Thrower');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('decodes URI components', () => {
|
||||||
|
const url = 'https://wiki.teamfortress.com/wiki/Dragon%27s_Fury';
|
||||||
|
expect(extractPageTitleFromURL(url)).toBe("Dragon's Fury");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles special characters', () => {
|
||||||
|
const url = 'https://wiki.teamfortress.com/wiki/Ze_%C3%9Cbermensch';
|
||||||
|
expect(extractPageTitleFromURL(url)).toBe("Ze Übermensch");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('removes language suffix', () => {
|
||||||
|
const url = 'https://wiki.teamfortress.com/wiki/Ze_%C3%9Cbermensch/pt-br';
|
||||||
|
expect(extractPageTitleFromURL(url)).toBe('Ze Übermensch');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -8,6 +8,7 @@ import { $T, extractLocaleFromURL } from './utils/localization'
|
|||||||
import { fetchPrice, fetchKeyPrice, ItemPriceData } from './priceService'
|
import { fetchPrice, fetchKeyPrice, ItemPriceData } from './priceService'
|
||||||
import { createPriceRow, createStoreButton } from './uiRenderer'
|
import { createPriceRow, createStoreButton } from './uiRenderer'
|
||||||
import { findFirstElement, findFirstChildElement } from './utils/dom'
|
import { findFirstElement, findFirstChildElement } from './utils/dom'
|
||||||
|
import { extractPageTitleFromURL } from './utils/url';
|
||||||
var itemSchema: ItemSchema | null;
|
var itemSchema: ItemSchema | null;
|
||||||
|
|
||||||
var pageLocale: string = 'en'
|
var pageLocale: string = 'en'
|
||||||
@@ -23,15 +24,6 @@ function getKeyByValue(object: any, value: string) {
|
|||||||
return Object.keys(object).find(key => object[key] === value);
|
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
|
// Main function
|
||||||
async function inject() {
|
async function inject() {
|
||||||
const itemInfobox = findFirstElement('.item-infobox');
|
const itemInfobox = findFirstElement('.item-infobox');
|
||||||
|
|||||||
8
src/content/utils/url.ts
Normal file
8
src/content/utils/url.ts
Normal 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('_', ' '));
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user