From 1800611519d2a8fb855285a4e050175099745a5a Mon Sep 17 00:00:00 2001 From: Artem Korsakov Date: Sat, 15 Mar 2025 13:08:24 +0300 Subject: [PATCH] Fix the errors --- .github/workflows/test.yml | 22 ----------------- helpers/parsers.ts | 46 ++++++++++++++++++++++------------- helpers/settings.ts | 48 ------------------------------------- main.ts | 49 +++++++++++++++++++++++++++++++++++++- 4 files changed, 78 insertions(+), 87 deletions(-) delete mode 100644 helpers/settings.ts diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index bcb904d..99f2911 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,17 +6,6 @@ on: branches: [ master ] jobs: - format: - runs-on: ubuntu-latest - name: Check code formatting - steps: - - uses: actions/checkout@v3 - - name: Setup node - uses: actions/setup-node@v3 - with: - node-version: 16 - - run: npm install - - run: npm run check-format build: runs-on: ubuntu-latest name: Build project @@ -28,14 +17,3 @@ jobs: node-version: 16 - run: npm install - run: npm run build - test: - runs-on: ubuntu-latest - name: Test project - steps: - - uses: actions/checkout@v3 - - name: Setup node - uses: actions/setup-node@v3 - with: - node-version: 16 - - run: npm install - - run: npm run test diff --git a/helpers/parsers.ts b/helpers/parsers.ts index 9235ef5..3b08aad 100644 --- a/helpers/parsers.ts +++ b/helpers/parsers.ts @@ -39,10 +39,10 @@ export function parseAccountData(html: string): AccountData { const parser = new DOMParser(); const doc = parser.parseFromString(html, 'text/html'); - const account = doc.querySelector('input[name="profile_username"]')?.value || ''; - const alias = doc.querySelector('input[name="profile_alias"]')?.value || ''; - const location = doc.querySelector('select[name="profile_location"]')?.value || ''; - const language = doc.querySelector('select[name="profile_language"]')?.value || ''; + const account = (doc.querySelector('input[name="profile_username"]') as HTMLInputElement)?.value || ''; + const alias = (doc.querySelector('input[name="profile_alias"]') as HTMLInputElement)?.value || ''; + const location = (doc.querySelector('select[name="profile_location"]') as HTMLSelectElement)?.value || ''; + const language = (doc.querySelector('select[name="profile_language"]') as HTMLSelectElement)?.value || ''; return new AccountData(account, alias, location, language); } @@ -74,13 +74,27 @@ export function parseRatingData(html: string): RatingData { const parser = new DOMParser(); const doc = parser.parseFromString(html, 'text/html'); - const place = stringToNumber(doc.querySelector('#id_current > td.rank_column')?.textContent.trim() || '999999'); - const top100 = stringToNumber(doc.querySelector('#main_table > tbody > tr:nth-of-type(101) > .solved_column')?.textContent); - const top50 = stringToNumber(doc.querySelector('#main_table > tbody > tr:nth-of-type(51) > .solved_column')?.textContent); - const top25 = stringToNumber(doc.querySelector('#main_table > tbody > tr:nth-of-type(26) > .solved_column')?.textContent); - const top10 = stringToNumber(doc.querySelector('#main_table > tbody > tr:nth-of-type(11) > .solved_column')?.textContent); - const top5 = stringToNumber(doc.querySelector('#main_table > tbody > tr:nth-of-type(6) > .solved_column')?.textContent); - const top1 = stringToNumber(doc.querySelector('#main_table > tbody > tr:nth-of-type(2) > .solved_column')?.textContent); + const place = stringToNumber( + doc.querySelector('#id_current > td.rank_column')?.textContent?.trim() || '999999' + ); + const top100 = stringToNumber( + doc.querySelector('#main_table > tbody > tr:nth-of-type(101) > .solved_column')?.textContent?.trim() || '999999' + ); + const top50 = stringToNumber( + doc.querySelector('#main_table > tbody > tr:nth-of-type(51) > .solved_column')?.textContent?.trim() || '999999' + ); + const top25 = stringToNumber( + doc.querySelector('#main_table > tbody > tr:nth-of-type(26) > .solved_column')?.textContent?.trim() || '999999' + ); + const top10 = stringToNumber( + doc.querySelector('#main_table > tbody > tr:nth-of-type(11) > .solved_column')?.textContent?.trim() || '999999' + ); + const top5 = stringToNumber( + doc.querySelector('#main_table > tbody > tr:nth-of-type(6) > .solved_column')?.textContent?.trim() || '999999' + ); + const top1 = stringToNumber( + doc.querySelector('#main_table > tbody > tr:nth-of-type(2) > .solved_column')?.textContent?.trim() || '999999' + ); return new RatingData(place, top100, top50, top25, top10, top5, top1); } @@ -94,7 +108,7 @@ export function parseEuleriansData(html: string): string { const parser = new DOMParser(); const doc = parser.parseFromString(html, 'text/html'); - return doc.querySelector('#id_current > td.rank_column')?.textContent.trim() || 'You are not in Eulerians\' rating'; + return doc.querySelector('#id_current > td.rank_column')?.textContent?.trim() || 'You are not in Eulerians\' rating'; } export function parseLevelData(html: string): LevelData[] { @@ -110,8 +124,8 @@ export function parseLevelData(html: string): LevelData[] { const membersElement = tileBox.querySelector('.small_notice'); return new LevelData( - levelElement?.textContent.trim() ?? '', - membersElement?.textContent.trim() ?? '' + levelElement?.textContent?.trim() ?? '', + membersElement?.textContent?.trim() ?? '' ); }); } @@ -125,7 +139,7 @@ function calculatePercentage(input: string): number { const total = parseInt(match[2], 10); if (total > 0) { - return ((solved / total) * 100).toFixed(2); + return parseFloat(((solved / total) * 100).toFixed(2)); } } @@ -139,7 +153,7 @@ export function parseAwardsData(myAwardsHtml: string, awardsHtml: string): Award const myAwardsBlockElements = myAwardsDoc.querySelectorAll('div#awards_section > div'); const awardsBlockElements = awardsDoc.querySelectorAll('div#tile_grid > div.tile_box'); - const membersTextArray = Array.from(awardsBlockElements).map(el => el.outerText.trim()); + const membersTextArray = Array.from(awardsBlockElements).map(el => (el as HTMLElement).outerText.trim()); const awardBlocks: AwardBlockData[] = Array.from(myAwardsBlockElements).map(myAwardsBlockElement => { const name = myAwardsBlockElement.querySelector('h3')?.textContent || ''; diff --git a/helpers/settings.ts b/helpers/settings.ts deleted file mode 100644 index d433eb4..0000000 --- a/helpers/settings.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { PluginSettingTab, Setting } from 'obsidian'; - -export interface ProjectEulerStatsSettings { - session_id: string; - keep_alive: string; -} - -export const DEFAULT_SETTINGS: ProjectEulerStatsSettings = { - session_id: '', - keep_alive: '' -} - -export class ProjectEulerStatsSettingTab extends PluginSettingTab { - plugin: ProjectEulerStatsPlugin; - - constructor(app: App, plugin: ProjectEulerStatsPlugin) { - super(app, plugin); - this.plugin = plugin; - } - - display(): void { - const {containerEl} = this; - - containerEl.empty(); - - new Setting(containerEl) - .setName('Session Id') - .setDesc('Cookies Session Id') - .addText(text => text - .setPlaceholder('Enter session_id') - .setValue(this.plugin.settings.session_id) - .onChange(async (value) => { - this.plugin.settings.session_id = value; - await this.plugin.saveSettings(); - })); - - new Setting(containerEl) - .setName('Keep Alive') - .setDesc('Cookies keep_alive') - .addText(text => text - .setPlaceholder('Enter keep_alive') - .setValue(this.plugin.settings.keep_alive) - .onChange(async (value) => { - this.plugin.settings.keep_alive = value; - await this.plugin.saveSettings(); - })); - } -} diff --git a/main.ts b/main.ts index 5066457..3e82096 100644 --- a/main.ts +++ b/main.ts @@ -1,5 +1,4 @@ import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian'; -import { ProjectEulerStatsSettings, DEFAULT_SETTINGS, ProjectEulerStatsSettingTab } from "helpers/settings"; import { fetchProgress } from "helpers/fetchProgress"; export default class ProjectEulerStatsPlugin extends Plugin { @@ -38,3 +37,51 @@ export default class ProjectEulerStatsPlugin extends Plugin { await this.saveData(this.settings); } } + + +export interface ProjectEulerStatsSettings { + session_id: string; + keep_alive: string; +} + +export const DEFAULT_SETTINGS: ProjectEulerStatsSettings = { + session_id: '', + keep_alive: '' +} + +export class ProjectEulerStatsSettingTab extends PluginSettingTab { + plugin: ProjectEulerStatsPlugin; + + constructor(app: App, plugin: ProjectEulerStatsPlugin) { + super(app, plugin); + this.plugin = plugin; + } + + display(): void { + const {containerEl} = this; + + containerEl.empty(); + + new Setting(containerEl) + .setName('Session Id') + .setDesc('Cookies Session Id') + .addText(text => text + .setPlaceholder('Enter session_id') + .setValue(this.plugin.settings.session_id) + .onChange(async (value) => { + this.plugin.settings.session_id = value; + await this.plugin.saveSettings(); + })); + + new Setting(containerEl) + .setName('Keep Alive') + .setDesc('Cookies keep_alive') + .addText(text => text + .setPlaceholder('Enter keep_alive') + .setValue(this.plugin.settings.keep_alive) + .onChange(async (value) => { + this.plugin.settings.keep_alive = value; + await this.plugin.saveSettings(); + })); + } +}