artemkorsakov_project-euler.../main.ts
2025-03-22 12:37:42 +03:00

94 lines
2.8 KiB
TypeScript

import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
import { fetchProgress, tryToFetchAndSaveProgress } from "helpers/fetchProgress";
import { extractSources } from "helpers/sourceExtractor";
export default class ProjectEulerStatsPlugin extends Plugin {
settings: ProjectEulerStatsSettings;
async onload() {
await this.loadSettings();
this.addSettingTab(new ProjectEulerStatsSettingTab(this.app, this));
this.registerMarkdownCodeBlockProcessor('euler-stats', async (source, el, ctx) => {
const extractedSource = extractSources(source);
const stats = await fetchProgress(this.settings.session_id, this.settings.keep_alive, extractedSource);
const container = el.createEl('div');
container.appendChild(stats);
});
this.addCommand({
id: 'sync-with-project-euler',
name: 'Sync with Project Euler',
callback: async () => {
try {
const fetchedData = await tryToFetchAndSaveProgress(this.settings.session_id, this.settings.keep_alive);
const message = fetchedData ? 'Successfully synced with Project Euler.' : 'Failed to sync with Project Euler. Please update your cookies in the settings and try again.';
new Notice(message);
} catch (error) {
console.error('Error during sync:', error);
new Notice('Error during sync. Please check the console for details.');
}
},
});
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
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();
}));
}
}