2025-03-08 05:42:30 +00:00
import { App , Editor , MarkdownView , Modal , Notice , Plugin , PluginSettingTab , Setting } from 'obsidian' ;
2025-03-22 09:37:42 +00:00
import { fetchProgress , tryToFetchAndSaveProgress } from "helpers/fetchProgress" ;
2025-03-22 06:50:58 +00:00
import { extractSources } from "helpers/sourceExtractor" ;
2025-03-08 05:42:30 +00:00
2025-03-08 08:03:21 +00:00
export default class ProjectEulerStatsPlugin extends Plugin {
settings : ProjectEulerStatsSettings ;
2025-03-08 05:42:30 +00:00
async onload() {
2025-03-08 08:03:21 +00:00
await this . loadSettings ( ) ;
2025-03-08 05:42:30 +00:00
2025-03-08 08:03:21 +00:00
this . addSettingTab ( new ProjectEulerStatsSettingTab ( this . app , this ) ) ;
2025-03-08 05:42:30 +00:00
2025-03-16 08:02:32 +00:00
this . registerMarkdownCodeBlockProcessor ( 'euler-stats' , async ( source , el , ctx ) = > {
2025-03-22 06:50:58 +00:00
const extractedSource = extractSources ( source ) ;
2025-03-28 06:05:39 +00:00
const stats = await fetchProgress ( this . settings . session_id , this . settings . keep_alive , extractedSource , this . settings . use_short_format ) ;
2025-03-10 17:07:33 +00:00
const container = el . createEl ( 'div' ) ;
2025-03-16 08:02:32 +00:00
container . appendChild ( stats ) ;
2025-03-10 17:07:33 +00:00
} ) ;
2025-03-22 09:37:42 +00:00
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.' ) ;
}
} ,
} ) ;
2025-03-08 05:42:30 +00:00
}
onunload() {
}
async loadSettings() {
this . settings = Object . assign ( { } , DEFAULT_SETTINGS , await this . loadData ( ) ) ;
}
async saveSettings() {
await this . saveData ( this . settings ) ;
}
}
2025-03-15 10:08:24 +00:00
export interface ProjectEulerStatsSettings {
session_id : string ;
keep_alive : string ;
2025-03-28 06:05:39 +00:00
use_short_format : boolean ;
2025-03-15 10:08:24 +00:00
}
export const DEFAULT_SETTINGS : ProjectEulerStatsSettings = {
session_id : '' ,
2025-03-28 06:05:39 +00:00
keep_alive : '' ,
use_short_format : false
2025-03-15 10:08:24 +00:00
}
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' )
2025-03-22 09:39:25 +00:00
. setDesc ( 'Cookies PHPSESSID' )
2025-03-15 10:08:24 +00:00
. addText ( text = > text
2025-03-22 09:39:25 +00:00
. setPlaceholder ( 'Enter PHPSESSID' )
2025-03-15 10:08:24 +00:00
. 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 ( ) ;
} ) ) ;
2025-03-28 06:05:39 +00:00
new Setting ( containerEl )
. setName ( 'Use compact format' )
. setDesc ( 'Enable to display data in a compact view' )
. addToggle ( toggle = > toggle
. setValue ( this . plugin . settings . use_short_format )
. onChange ( async ( value ) = > {
this . plugin . settings . use_short_format = value ;
await this . plugin . saveSettings ( ) ;
} ) ) ;
2025-03-15 10:08:24 +00:00
}
}