11min = 1880
22weight = 50
3- now = ( new Date ( ) ) . getFullYear ( )
3+ now = new Date ( )
44var year
55var countries = [ ]
66var country
77var picks = [ ]
88var r = 0
99document . getElementById ( "score" ) . value = 0
10+ var random = module . exports ( now . getDate ( ) + now . getMonth ( ) + now . getYear ( ) )
1011getRandom = photos => {
1112 if ( ! photos ) { return "" }
12- i = Math . floor ( Math . random ( ) * photos . length )
13+ i = Math . floor ( random ( ) * photos . length )
1314 member = photos [ i ]
14- console . log ( "pick" , member . title )
1515 source = member ?. thumbnail ?. source
16+ console . log ( "pick" , member . title , source )
1617 if ( member . title . slice ( 0 , 4 ) == "File" && ! source ) {
1718 return getRandom ( photos )
1819 }
@@ -28,6 +29,10 @@ getPhoto = async (category, once) => {
2829 resp = await fetch ( `https://commons.wikimedia.org/w/api.php?action=query&format=json&origin=*&prop=pageimages&generator=categorymembers&formatversion=2&pithumbsize=1000&pilicense=free&gcmtitle=Category%3A${ category } &gcmtype=file|subcat&gcmlimit=max` )
2930 members = await resp . json ( )
3031 pick = getRandom ( members ?. query ?. pages )
32+ if ( pick == "" ) {
33+ year = ( curYear - Math . floor ( random ( ) * ( curYear - min ) ) )
34+ return getPhoto ( year + " photographs by country" )
35+ }
3136 if ( / [ 0 - 9 ] { 4 } p h o t o g r a p h s b y c o u n t r y / . test ( category ) ) {
3237 console . log ( "Main" )
3338 countries = members ?. query ?. pages ?. map ( x => x ?. title ?. slice ( 29 ) ?. replace ( / ^ t h e / , "" ) ) . sort ( )
@@ -40,7 +45,8 @@ getPhoto = async (category, once) => {
4045 return pick
4146}
4247getNext = async ( ) => {
43- year = ( now - Math . floor ( Math . random ( ) * ( now - min ) ) )
48+ curYear = now . getFullYear ( )
49+ year = ( curYear - Math . floor ( random ( ) * ( curYear - min ) ) )
4450 var photo
4551 if ( ! picks [ r ] ) {
4652 photo = await getPhoto ( year + " photographs by country" )
@@ -56,8 +62,8 @@ getNext = async () => {
5662 let range = document . createElement ( "input" )
5763 range . type = "range"
5864 range . min = min
59- range . max = now
60- range . value = ( min + Math . ceil ( ( now - min ) / 2 ) )
65+ range . max = curYear
66+ range . value = ( min + Math . ceil ( ( curYear - min ) / 2 ) )
6167 let text = document . createElement ( "input" )
6268 text . type = "text"
6369 text . value = range . value
@@ -122,26 +128,20 @@ getNext = async () => {
122128 document . body . appendChild ( document . createElement ( "br" ) )
123129 document . body . appendChild ( submit )
124130 if ( photo . round == "5/5" ) {
125- submit . value = "New"
126- let link = document . createElement ( "input" )
127- link . type = "text"
128- link . id = "link"
129- link . value = window . location . origin + window . location . pathname
130- link . value += "?p="
131- link . value += btoa ( picks . map ( x => [ x . pageid , x . country , x . year ] . join ( '.' ) ) . join ( '-' ) )
131+ submit . remove ( )
132132 let share = document . createElement ( "input" )
133133 share . value = "Share"
134134 share . type = "Submit"
135135 share . addEventListener ( "click" , ( ) => navigator . share ( {
136136 title : "Wiki When" ,
137- text : `I scored ${ score . value } at ${ document . getElementById ( "link" ) . value } ` ,
138- url : document . getElementById ( "link" ) . value
137+ text : `I scored ${ score . value } at ${ window . location . origin } }` ,
138+ url : window . location . origin
139139 } ) )
140140 if ( navigator . share ) {
141141 document . body . appendChild ( share )
142142 }
143143 document . body . appendChild ( document . createElement ( "br" ) )
144- document . body . append ( link )
144+ document . body . appendChild ( document . createElement ( "br" ) )
145145 } else {
146146 submit . value = "Next"
147147 }
0 commit comments