Skip to content

Instantly share code, notes, and snippets.

@mogsdad
Last active January 9, 2019 03:31
Show Gist options
  • Save mogsdad/6390e44cae2fce8c7292a185bb8497be to your computer and use it in GitHub Desktop.
Save mogsdad/6390e44cae2fce8c7292a185bb8497be to your computer and use it in GitHub Desktop.
Pre-fill Ontario Parks reservation form to speed through reservation attempts. To customize for your situation, edit variables in "User Settings" block.
// ==UserScript==
// @name OntarioParksReservation-1-Home
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Pre-fill Ontario Parks reservation form
// @author Mogsdad, David Bingham
// @match https://reservations.ontarioparks.com/Home.aspx
// @match https://reservations.ontarioparks.com/BrowseMaps
// @match https://reservations.ontarioparks.com/OntarioParks%2fSoutheastParks%3fMap
// @match https://reservations.ontarioparks.com/Sandbanks%2fOutletRiverAandB%2fOutletRiverA%3fMap
// @match https://reservations.ontarioparks.com/Sandbanks/OutletRiverAandB/OutletRiverA?Map
// @match https://reservations.ontarioparks.com/Sandbanks/OutletRiverAandB?Map
// @match https://reservations.ontarioparks.com/Viewer.aspx
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Redirect to the campsite map
let campsiteURL = "https://reservations.ontarioparks.com/Sandbanks/OutletRiverAandB/OutletRiverA?Map";
if (window.location.href !== campsiteURL) {
window.location = campsiteURL;
return;
}
let $ = jQuery; // Avoid editor warnings; jQuery is provided in target site
// Extend Date object to facilitate math-with-dates
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
Date.prototype.addMonths = function (m) {
var d = new Date(this);
var years = Math.floor(m / 12);
var months = m - (years * 12);
if (years) d.setFullYear(d.getFullYear() + years);
if (months) d.setMonth(d.getMonth() + months);
return d;
}
let today = new Date();
today.setHours(0,0,0,0);
// Settings for form pre-population; edit these for your situation. <================== Settings
let myResType = 'Campsite',
myArrMth = new Date('June 2019'),
myArrDay = today.addMonths( 5 ), // Arrive 5 months from today
myNumNights = '23', // Max 23 nights
myLocation = 'Sandbanks',
myMap = 'Outlet River A and B',
myCampground = 'Outlet River A',
myEquipment = 'Trailer or RV up to 18ft',
myPartySize = '4',
mySite = '85';
// Fill in reservation settings
let myResTypeVal = findOption("selResType",myResType);
if ($("#selResType").val() !== myResTypeVal) $("#selResType").val(myResTypeVal).change();
setLatestQuickDate();
let myArrMthFull = myArrMth.toString();
if ($("#selArrMth").val() !== myArrMthFull) {
$("#selArrMth").val(myArrMthFull).change();
}
let myArrDayFull = myArrDay.toString();
if ($("#selArrDay").val() !== myArrDayFull) $("#selArrDay").val(myArrDayFull).change();
if ($("#selNumNights").val() !== myNumNights) $("#selNumNights").val(myNumNights).change();
let myLocationGUID = findOption("selLocation",myLocation) || findOption("MainContentPlaceHolder_LocationList",myLocation);
if ( myLocationGUID && $("#selLocation,#MainContentPlaceHolder_LocationList").val() !== myLocationGUID ) {
$("#selLocation,#MainContentPlaceHolder_LocationList").val(myLocationGUID).change();
}
let myMapGUID = findOption("selMap",myMap);
if (myMapGUID && $("#selMap").val() !== myMapGUID) $("#selMap").val(myMapGUID);
let myCampgroundGUID = findOption("MainContentPlaceHolder_MapList",myCampground);
if (myCampgroundGUID && $("#MainContentPlaceHolder_MapList").val() !== myCampgroundGUID) $("#MainContentPlaceHolder_MapList").val(myCampgroundGUID);
if ($("#selEquipmentSub").val() !== myEquipment) $("#selEquipmentSub").val(myEquipment);
if ($("#selPartySize").val() !== myPartySize) $("#selPartySize").val(myPartySize);
let mySiteGUID = findOption("selResource",mySite);
if ( $("#selResource").val() !== mySiteGUID ) $("#selResource").val(mySiteGUID).change();
})();
/**
* Return the option value corresponding to given optionText from given select element.
*
* @param {string} selList Element ID of select list to search
* @param {string} optionText HTML text to locate
*
* @returns {string} Option value if match found, otherwise null.
*/
function findOption( selList, optionText ) {
let selectList = selList && document.getElementById(selList);
let options = selectList && selectList.options;
if (! options ) {
if (selectList) console.log('Failed to find ' + optionText + ' in ' + selList);
return null;
}
var kvArray = Array.apply(null, options).map(function(el){return [el.text,el.value]});
var kvMap = new Map(kvArray);
return (kvMap.get(optionText));
}
/**
* Set the QuickDateList that is latest in the year.
*/
function setLatestQuickDate() {
let selectList = document.getElementById('MainContentPlaceHolder_QuickDateList');
let options = selectList && selectList.options;
if (! options ) {
console.log('No QuickDateList found');
return null;
}
var kvArray = Array.apply(null, options).map(function(el){return el.value});
$("#MainContentPlaceHolder_QuickDateList").val(kvArray[kvArray.length-1]).change();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment