|
| 1 | +import { followRedirect, runSmartLinkImporter, type ServiceElement } from '~/lib/smart-link-importer'; |
| 2 | +import { isIgnoredService, isPhysicalMediaLink, normalizeServiceName } from '~/lib/smart-link-importer/logic'; |
| 3 | + |
| 4 | +import { extractFanlinkServiceDataFromScript, type FanlinkServiceData } from './logic'; |
| 5 | + |
| 6 | +function readServiceData(): FanlinkServiceData[] { |
| 7 | + for (const script of document.scripts) { |
| 8 | + const links = extractFanlinkServiceDataFromScript(script.textContent); |
| 9 | + if (links.length > 0) return links; |
| 10 | + } |
| 11 | + return []; |
| 12 | +} |
| 13 | + |
| 14 | +function serviceFromElement(element: HTMLElement): string { |
| 15 | + const imageUrl = element.querySelector<HTMLImageElement>('.link-option-row-img')?.src; |
| 16 | + if (!imageUrl) return ''; |
| 17 | + |
| 18 | + try { |
| 19 | + const filename = new URL(imageUrl).pathname.split('/').pop() ?? ''; |
| 20 | + return normalizeServiceName(filename.replace(/\.[^.]+$/, '')); |
| 21 | + } catch { |
| 22 | + return ''; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +function collectServiceElements(): ServiceElement[] { |
| 27 | + const dataByService = new Map<string, FanlinkServiceData[]>(); |
| 28 | + for (const data of readServiceData()) { |
| 29 | + const services = dataByService.get(data.service) ?? []; |
| 30 | + services.push(data); |
| 31 | + dataByService.set(data.service, services); |
| 32 | + } |
| 33 | + |
| 34 | + const counters = new Map<string, number>(); |
| 35 | + const elements: ServiceElement[] = []; |
| 36 | + for (const element of document.querySelectorAll<HTMLElement>('.link-options a.link-option-row')) { |
| 37 | + const service = serviceFromElement(element); |
| 38 | + const data = dataByService.get(service)?.shift(); |
| 39 | + const action = element.querySelector<HTMLElement>('.link-option-row-action')?.textContent.trim() || ''; |
| 40 | + if (!data || isIgnoredService(service) || isPhysicalMediaLink(service, action)) continue; |
| 41 | + |
| 42 | + const count = (counters.get(service) ?? 0) + 1; |
| 43 | + counters.set(service, count); |
| 44 | + elements.push({ |
| 45 | + cacheKey: count === 1 ? service : `${service}:${count}`, |
| 46 | + element, |
| 47 | + service, |
| 48 | + label: element.querySelector<HTMLImageElement>('img[alt]')?.alt || data.label, |
| 49 | + action, |
| 50 | + sourceUrl: data.sourceUrl, |
| 51 | + }); |
| 52 | + } |
| 53 | + return elements; |
| 54 | +} |
| 55 | + |
| 56 | +void runSmartLinkImporter({ |
| 57 | + id: 'fanlink', |
| 58 | + siteName: 'Fanlink', |
| 59 | + collectServiceElements, |
| 60 | + resolveDestination: element => followRedirect(element.sourceUrl).catch(() => element.sourceUrl), |
| 61 | +}); |
0 commit comments