Amtrak Lowest Fare Default with Dropdown

Automatically opens the 'Sort/Filter' dropdown and selects the 'Lowest Fare' option on Amtrak's ticket search results.

  1. // ==UserScript==
  2. // @name Amtrak Lowest Fare Default with Dropdown
  3. // @namespace namespace_amtrak_lowest_fare_dropdown
  4. // @version 1.0
  5. // @description Automatically opens the 'Sort/Filter' dropdown and selects the 'Lowest Fare' option on Amtrak's ticket search results.
  6. // @author Andrew Lakkis
  7. // @license MIT
  8. // @match https://www.amtrak.com/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. var dropdown;
  16. var currentButton;
  17.  
  18. // Function to click the 'Sort/Filter' dropdown
  19. const openSortFilterDropdown = () => {
  20. const sortOptionsInterval = setInterval(() => {
  21. dropdown = document.querySelector('.ng-star-inserted.p-3.align-items-center.d-flex.sort-and-filter-btn');
  22. currentButton = document.querySelector('[amt-auto-test-id="search-results-current-sortby-filter"]');
  23.  
  24. if (dropdown && !(currentButton.innerText === 'Lowest Fare')) {
  25. dropdown.click(); // Simulate a click to open the dropdown
  26. setTimeout(selectLowestFare, 500); // Adjust time as needed based on site behavior
  27. clearInterval(sortOptionsInterval); // Stop the interval
  28. }
  29. }, 100);
  30.  
  31. };
  32.  
  33.  
  34. // Function to select the 'Lowest Fare' option
  35. const selectLowestFare = () => {
  36. // Wait for the sort options to be available in the DOM after opening the dropdown
  37. const sortOptionsInterval = setInterval(() => {
  38. const sortOptions = document.querySelector('mat-radio-group[formcontrolname="sortBy"]');
  39. if (sortOptions) {
  40. clearInterval(sortOptionsInterval);
  41. const lowestFareOption = sortOptions.querySelector('[for="mat-radio-5-input"]');
  42. if (lowestFareOption) {
  43. // Simulate a click on the 'Lowest Fare' radio button
  44. lowestFareOption.click();
  45. dropdown.click();
  46. }
  47. }
  48. }, 100);
  49. };
  50.  
  51.  
  52.  
  53. // Run the function when the DOM is fully loaded
  54. const dropdownOpened = openSortFilterDropdown();
  55. if (dropdownOpened) {
  56. // Wait a brief moment for dropdown animations and options to become visible
  57. setTimeout(selectLowestFare, 500); // Adjust time as needed based on site behavior
  58. }
  59. })();