import { lang } from './Lang' ; import { DOM } from './DOM' ; import { filter } from './Filter' ; import { store } from './Store' ; import { log } from './Log' ; import { EVT } from './EVT' ; import { titleBar } from './TitleBar' ; import { saveData } from './SaveData' ; import { PostList , Post } from './CrawlResult.d' ; abstract class InitPageBase { protected init ( ) { this . appendCenterBtns ( ) ; this . appendElseEl ( ) ; this . initElse ( ) ; window . addEventListener ( EVT . events . destroy , ( ) => { this . destroy ( ) ; } ) ; } protected initElse ( ) { } protected destroy ( ) { DOM . clearSlot ( 'crawlBtns' ) ; } protected appendCenterBtns ( ) { } protected appendElseEl ( ) { } protected crawlNumber : number = 0 ; protected nextUrl : null | string = null ; protected async readyCrawl ( ) { if ( ! store . states . allowWork ) { window . alert ( lang . transl ( '_当前任务尚未完成2' ) ) ; return ; } EVT . fire ( EVT . events . crawlStart ) ; log . clear ( ) ; log . success ( lang . transl ( '_任务开始0' ) ) ; titleBar . change ( '↑' ) ; filter . init ( ) ; this . nextStep ( ) ; } protected nextStep ( ) { this . FetchPostList ( ) ; } protected abstract FetchPostList ( ) ; protected afterFetchPostList ( data ) { const items = data . body . items ; this . nextUrl = data . body . nextUrl ; for ( const item of items ) { saveData . receive ( item ) ; } if ( this . nextUrl ) { this . FetchPostList ( ) ; } else { this . crawlFinished ( ) ; } } protected abstract fetchPost ( ) ; protected afterFetchPost ( data ) { saveData . receive ( data . body ) ; this . crawlFinished ( ) ; } protected crawlFinished ( ) { if ( store . result . length === 0 ) { return this . noResult ( ) ; } this . nextUrl = null ; store . crawlCompleteTime = new Date ( ) ; log . log ( lang . transl ( '_抓取文件数量' , store . result . length . toString ( ) ) ) ; log . log ( lang . transl ( '_抓取完毕' ) , 2 ) ; EVT . fire ( EVT . events . crawlFinish ) ; } protected noResult ( ) { EVT . fire ( EVT . events . crawlEmpty ) ; titleBar . reset ( ) ; log . error ( lang . transl ( '_抓取结果为零' ) , 2 ) ; window . alert ( lang . transl ( '_抓取结果为零' ) ) ; } } export { InitPageBase } ;