1+ <!DOCTYPE html>
2+ < html lang ="zh-CN ">
3+
4+ < head >
5+ < meta charset ="UTF-8 ">
6+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
7+ < title > 狗屁不通文章生成器</ title >
8+ < link rel ="stylesheet " href ="style.css ">
9+ </ head >
10+
11+ < body >
12+ < header >
13+ < div class ="options ">
14+ < div > 最小字数:< input id ="min " type ="range " min ="500 " max ="5000 " step ="100 " value ="2000 "> < span > 2000</ span > </ div >
15+ < div > 最大字数:< input id ="max " type ="range " min ="1000 " max ="10000 " step ="100 " value ="5000 "> < span > 5000</ span >
16+ </ div >
17+ </ div >
18+ < div class ="ref "> < a href ="https://github.com/menzi11/BullshitGenerator "> 原版</ a > </ div >
19+ < div class ="title "> < span > 标题:</ span > < input id ="title " type ="text " value ="">
20+ < button id ="generate "> 生成</ button >
21+ < button id ="anotherTitle "> 换一个</ button >
22+ </ div >
23+ </ header >
24+ < main >
25+ < article > </ article >
26+ </ main >
27+ < script type ="module ">
28+ import { generate } from './lib/generator.js' ;
29+ import { createRandomPicker } from './lib/random.js' ;
30+
31+ const options = document . querySelector ( '.options' ) ;
32+ const config = { min : 2000 , max : 5000 } ;
33+ options . addEventListener ( 'change' , ( { target } ) => {
34+ const num = Number ( target . value ) ;
35+ config [ target . id ] = num ;
36+ target . parentNode . querySelector ( 'input + span' ) . innerHTML = num ;
37+ } ) ;
38+ const generateButton = document . getElementById ( 'generate' ) ;
39+ const anotherTitleButton = document . getElementById ( 'anotherTitle' ) ;
40+ const article = document . querySelector ( 'article' ) ;
41+ const titleEl = document . getElementById ( 'title' ) ;
42+
43+ ( async function ( ) {
44+ const corpus = await ( await fetch ( './corpus/data.json' ) ) . json ( ) ;
45+ const pickTitle = createRandomPicker ( corpus . title ) ;
46+ titleEl . value = pickTitle ( ) ;
47+ generateButton . addEventListener ( 'click' , ( ) => {
48+ const text = generate ( titleEl . value , { corpus, ...config } ) ;
49+ article . innerHTML = `<section>${ text . join ( '</section><section>' ) } </section>` ;
50+ } ) ;
51+ anotherTitleButton . addEventListener ( 'click' , ( ) => {
52+ titleEl . value = pickTitle ( ) ;
53+ if ( article . innerHTML ) generateButton . click ( ) ;
54+ } ) ;
55+ } ( ) ) ;
56+ </ script >
57+ </ body >
58+
59+ </ html >
0 commit comments