Skip to content

Commit 13465c2

Browse files
fraz3alpharboyatt
andauthored
Record breaker average location (#345)
* Add record breaker challenge. Co-authored-by: Russell Boyatt <[email protected]>
1 parent b328a60 commit 13465c2

File tree

9 files changed

+748
-155
lines changed

9 files changed

+748
-155
lines changed

browser-extensions/common/js/content-scripts/content-script-parkrunner.js

Lines changed: 124 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ function parse_results_table() {
201201
// Return the results in reverse chronological order, which is the natural
202202
// way to process it, as the older parkruns will be hit first when iterating
203203
// over the list
204+
205+
// TODO: This should be ordered by date, rather than assuming the order. We've had at least one report of this
206+
// causing issues, but I haven't yet recreated any other ordering other than the default
204207
return parkruns_completed.reverse()
205208
}
206209

@@ -491,124 +494,132 @@ function add_challenge_results(div_id, data) {
491494
set_progress_message("Added challenge results")
492495
}
493496

494-
// What all our container divs are called
495-
var id_map = {
496-
"messages": "running_challenges_messages_div",
497-
"badges": "running_challenges_badges_div",
498-
"flags": "running_challenges_flags_div",
499-
"main": "running_challenges_main_div",
500-
"stats": "running_challenges_stats_div"
501-
}
497+
function create_page() {
502498

503-
create_skeleton_elements(id_map)
504-
505-
var loaded_user_data = {}
506-
var loaded_geo_data = undefined
507-
var parsed_volunteer_data = undefined
508-
var parsed_results_data = undefined
509-
510-
set_progress_message("Parsing Athlete Info")
511-
var parsedPageAthleteInfo = parsePageAthleteInfo()
512-
set_progress_message("Parsed Athlete Info")
513-
514-
set_progress_message("Parsing Results")
515-
parsed_results_data = parse_results_table()
516-
set_progress_message("Parsed Results")
517-
518-
set_progress_message("Loading saved data")
519-
browser.storage.local.get(["home_parkrun_info", "athlete_number"]).then((items) => {
520-
set_progress_message("Loaded saved data")
521-
loaded_user_data = items
522-
// console.log("Here is the stored items, fetched with a promise:")
523-
// console.log(items)
524-
525-
// Now lets fetch the geo data
526-
set_progress_message("Loading geo data")
527-
return browser.runtime.sendMessage({data: "geo"});
528-
}).then((results) => {
529-
set_progress_message("Loaded geo data")
530-
console.log('Loaded geo data was:')
531-
console.log(results.geo)
532-
// The return packet will normally be valid even if the geo data is not contained
533-
// within, so we do some sanity check here
534-
if (results.geo && results.geo.data) {
535-
loaded_geo_data = results.geo
536-
} else {
537-
console.log('Geo data rejected')
499+
// What all our container divs are called
500+
var id_map = {
501+
"messages": "running_challenges_messages_div",
502+
"badges": "running_challenges_badges_div",
503+
"flags": "running_challenges_flags_div",
504+
"main": "running_challenges_main_div",
505+
"stats": "running_challenges_stats_div"
538506
}
539507

540-
set_progress_message("Loading volunteer data")
541-
// Now lets fetch the volunteer information
542-
return $.ajax({
543-
// If we translate this URL into the local one, not only do we have to
544-
// parse the page separately (no big deal), but we need to add every
545-
// domain into our CSP, which is a bit annoying, but would maybe
546-
// turn out to be more efficient for the user in a country far away
547-
// from the UK (depending on where parkrun host these servers)
548-
url: 'https://' + location.host + '/parkrunner/'+get_athlete_id(),
549-
dataType: 'html'})
550-
}).then((results) => {
551-
set_progress_message("Loaded volunteer data")
552-
// console.log("Here is the volunteer data, fetched with a promise:")
553-
// console.log(results)
554-
set_progress_message("Parsing volunteer data")
555-
parsed_volunteer_data = parse_volunteer_table(results)
556-
set_progress_message("All done")
557-
558-
data = {
559-
'parkrun_results': parsed_results_data,
560-
'volunteer_data': parsed_volunteer_data,
561-
'geo_data': loaded_geo_data,
562-
'user_data': loaded_user_data,
563-
'info': {}
564-
}
508+
create_skeleton_elements(id_map)
565509

566-
// Now add some supplemental information
567-
// Is the page we are looking at the one for the user who has configured the plugin?
568-
// - this will help us hide the 'home parkrun' and data based on that from
569-
// user profiles it does not belong to
510+
var loaded_user_data = {}
511+
var loaded_geo_data = undefined
512+
var parsed_volunteer_data = undefined
513+
var parsed_results_data = undefined
570514

571-
updateSummaryInfo(data, get_athlete_id())
572-
573-
data.challenge_results = {
574-
"running_results": generate_running_challenge_data(data, parsedPageAthleteInfo),
575-
"volunteer_results": generate_volunteer_challenge_data(data)
576-
}
577-
// Update info with booleans for the presence of results
578-
data.info.has_challenge_results = (data.challenge_results !== undefined)
579-
data.info.has_challenge_running_results = (data.info.has_challenge_results && data.challenge_results.running_results !== undefined)
580-
data.info.has_challenge_volunteer_results = (data.info.has_challenge_results && data.challenge_results.volunteer_results !== undefined)
581-
data.info.has_volunteer_data = (data.volunteer_data !== undefined)
582-
583-
data.stats = generate_stats(data)
584-
// Update info with boolean for the presence of stats
585-
data.info.has_stats = (data.stats !== undefined)
586-
587-
console.log(data)
588-
589-
// Use the acquired data to add all the additional information to the page
590-
add_badges(id_map["badges"], data)
591-
add_flags(id_map["flags"], data)
592-
add_challenge_results(id_map["main"], data)
593-
add_stats(id_map["stats"], data)
594-
595-
var errors = []
596-
if (!has_geo_data(data)) {
597-
errors.push('! Unable to fetch parkrun event location data: Stats, Challenges, and Maps requiring locations are not available !')
598-
}
599-
if (data.info.has_geo_technical_event_data == false) {
600-
errors.push('! Unable to fetch parkrun event status data: Stats and Challenges, e.g. Regionnaire, may include events that haven\'t started yet !')
515+
set_progress_message("Parsing Athlete Info")
516+
var parsedPageAthleteInfo = parsePageAthleteInfo()
517+
set_progress_message("Parsed Athlete Info")
518+
519+
set_progress_message("Parsing Results")
520+
parsed_results_data = parse_results_table()
521+
if (parsed_results_data === undefined || parsed_results_data.length == 0) {
522+
set_progress_message("No results detected, no challenge data will be compiled")
523+
return
601524
}
525+
set_progress_message("Parsed Results")
526+
527+
set_progress_message("Loading saved data")
528+
browser.storage.local.get(["home_parkrun_info", "athlete_number"]).then((items) => {
529+
set_progress_message("Loaded saved data")
530+
loaded_user_data = items
531+
// console.log("Here is the stored items, fetched with a promise:")
532+
// console.log(items)
533+
534+
// Now lets fetch the geo data
535+
set_progress_message("Loading geo data")
536+
return browser.runtime.sendMessage({data: "geo"});
537+
}).then((results) => {
538+
set_progress_message("Loaded geo data")
539+
console.log('Loaded geo data was:')
540+
console.log(results.geo)
541+
// The return packet will normally be valid even if the geo data is not contained
542+
// within, so we do some sanity check here
543+
if (results.geo && results.geo.data) {
544+
loaded_geo_data = results.geo
545+
} else {
546+
console.log('Geo data rejected')
547+
}
548+
549+
set_progress_message("Loading volunteer data")
550+
// Now lets fetch the volunteer information
551+
return $.ajax({
552+
// If we translate this URL into the local one, not only do we have to
553+
// parse the page separately (no big deal), but we need to add every
554+
// domain into our CSP, which is a bit annoying, but would maybe
555+
// turn out to be more efficient for the user in a country far away
556+
// from the UK (depending on where parkrun host these servers)
557+
url: 'https://' + location.host + '/parkrunner/'+get_athlete_id(),
558+
dataType: 'html'})
559+
}).then((results) => {
560+
set_progress_message("Loaded volunteer data")
561+
// console.log("Here is the volunteer data, fetched with a promise:")
562+
// console.log(results)
563+
set_progress_message("Parsing volunteer data")
564+
parsed_volunteer_data = parse_volunteer_table(results)
565+
set_progress_message("All done")
566+
567+
data = {
568+
'parkrun_results': parsed_results_data,
569+
'volunteer_data': parsed_volunteer_data,
570+
'geo_data': loaded_geo_data,
571+
'user_data': loaded_user_data,
572+
'info': {}
573+
}
602574

575+
// Now add some supplemental information
576+
// Is the page we are looking at the one for the user who has configured the plugin?
577+
// - this will help us hide the 'home parkrun' and data based on that from
578+
// user profiles it does not belong to
603579

604-
// Add our final status message
605-
set_complete_progress_message(errors)
580+
updateSummaryInfo(data, get_athlete_id())
581+
582+
data.challenge_results = {
583+
"running_results": generate_running_challenge_data(data, parsedPageAthleteInfo),
584+
"volunteer_results": generate_volunteer_challenge_data(data)
585+
}
586+
// Update info with booleans for the presence of results
587+
data.info.has_challenge_results = (data.challenge_results !== undefined)
588+
data.info.has_challenge_running_results = (data.info.has_challenge_results && data.challenge_results.running_results !== undefined)
589+
data.info.has_challenge_volunteer_results = (data.info.has_challenge_results && data.challenge_results.volunteer_results !== undefined)
590+
data.info.has_volunteer_data = (data.volunteer_data !== undefined)
591+
592+
data.stats = generate_stats(data)
593+
// Update info with boolean for the presence of stats
594+
data.info.has_stats = (data.stats !== undefined)
595+
596+
console.log(data)
597+
598+
// Use the acquired data to add all the additional information to the page
599+
add_badges(id_map["badges"], data)
600+
add_flags(id_map["flags"], data)
601+
add_challenge_results(id_map["main"], data)
602+
add_stats(id_map["stats"], data)
603+
604+
var errors = []
605+
if (!has_geo_data(data)) {
606+
errors.push('! Unable to fetch parkrun event location data: Stats, Challenges, and Maps requiring locations are not available !')
607+
}
608+
if (data.info.has_geo_technical_event_data == false) {
609+
errors.push('! Unable to fetch parkrun event status data: Stats and Challenges, e.g. Regionnaire, may include events that haven\'t started yet !')
610+
}
611+
612+
613+
// Add our final status message
614+
set_complete_progress_message(errors)
606615

607-
}).catch(error => {
608-
console.log(error)
609-
console.error(`An error occurred: ${error}`);
610-
set_progress_message(`Error: ${error}`)
611-
});
616+
}).catch(error => {
617+
console.log(error)
618+
console.error(`An error occurred: ${error}`);
619+
set_progress_message(`Error: ${error}`)
620+
});
621+
622+
}
612623

613624
function get_athlete_id() {
614625
// Very basic method to get only the parameter we care about
@@ -636,4 +647,7 @@ function modifyStyle(img) {
636647
}
637648
}
638649

639-
}
650+
}
651+
652+
// Run our code and render the page
653+
create_page()

0 commit comments

Comments
 (0)