Advertisement
Guest User

gc-delete-golf-scorecards

a guest
Mar 11th, 2021
1,191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //v5
  2.  
  3. function gcDeleteGolf() {
  4.     let loc = window.location.href
  5.  
  6.     let connectURL = "https://connect.garmin.com";
  7.     if (loc.indexOf(connectURL) != 0 || typeof jQuery === "undefined") {
  8.         alert(
  9. `You must be logged into Garmin Connect to run this script.
  10.  
  11. (Your current tab must also be a Garmin Connect page with
  12. URL starting with: ${connectURL})`);
  13.         return;
  14.     }
  15.  
  16.     const header =
  17. `Garmin Connect - Golf Scorecards Deletion Tool
  18. ────────────────────────────────────────────────────
  19.  
  20. `;
  21.  
  22.     jQuery.ajax({
  23.         beforeSend: function(request) {
  24.             request.setRequestHeader("NK", "NT");
  25.         },
  26.         dataType: "json",
  27.         url: `https://connect.garmin.com/modern/proxy/gcs-golfcommunity/api/v2/scorecard/summary?per-page=10000`,
  28.         success: function(data) {
  29.             onGolfList(data);
  30.         },
  31.         error: function() {
  32.             alert(`${header}Unable to retrieve list of golf scorecards.`);
  33.         }
  34.     });
  35.  
  36.     function formatDate(date) {
  37.         let d = new Date(date),
  38.             month = '' + (d.getMonth() + 1),
  39.             day = '' + d.getDate(),
  40.             year = d.getFullYear();
  41.  
  42.         if (month.length < 2)
  43.             month = '0' + month;
  44.         if (day.length < 2)
  45.             day = '0' + day;
  46.  
  47.         return [year, month, day].join('-');
  48.     }
  49.  
  50.     function promptDate(str, def, end) {
  51.         while (true) {
  52.             const val = prompt(str, def);
  53.             if (!val) {
  54.                 return val;
  55.             }
  56.  
  57.             const dateRegExp = /^(\d\d\d\d)-(\d\d)-(\d\d)$/;
  58.             const match = val.match(dateRegExp);
  59.             if (!match || match.length == 0) {
  60.                 continue;
  61.             }
  62.             let d;
  63.             if (!end) {
  64.                 d = new Date(match[1], match[2]-1, match[3]);
  65.             } else {
  66.                 d = new Date(match[1], match[2]-1, match[3], 23, 59, 59);
  67.             }
  68.             // console.log(d)
  69.             return d;
  70.         }
  71.     }
  72.  
  73.     function onGolfList(data) {
  74.         let earliestScorecard;
  75.         let latestScorecard;
  76.         const scorecardSummaries = data.scorecardSummaries || [];
  77.  
  78.         if (scorecardSummaries.length === 0) {
  79.             alert(`${header}No scorecards found.`)
  80.             return;
  81.         }
  82.  
  83.         scorecardSummaries.forEach(s => {
  84.             const activityTime = new Date(s.startTime);
  85.             if (!earliestScorecard || activityTime < earliestScorecard) {
  86.                 earliestScorecard = activityTime;
  87.             }
  88.             if (!latestScorecard || activityTime > latestScorecard) {
  89.                 latestScorecard = activityTime;
  90.             }
  91.         })
  92.  
  93.         const dateHeader =
  94. `${header}You have ${scorecardSummaries.length} scorecards from ${formatDate(earliestScorecard)} to ${formatDate(latestScorecard)}.
  95.  
  96. `;
  97.  
  98.         const startDeleteDate = promptDate(`${dateHeader}Enter start date for deletion (YYYY-MM-DD):`, formatDate(earliestScorecard))
  99.         if (!startDeleteDate) {
  100.             return;
  101.         }
  102.  
  103.         const endDeleteDate = promptDate(`${dateHeader}Enter end date for deletion (YYYY-MM-DD):`, formatDate(latestScorecard), true)
  104.         if (!endDeleteDate) {
  105.             return;
  106.         }
  107.  
  108.         const activitiesToDelete = [];
  109.         scorecardSummaries.forEach(s => {
  110.             const activityTime = new Date(s.startTime);
  111.             if (activityTime >= startDeleteDate && activityTime <= endDeleteDate) {
  112.                 activitiesToDelete.push(s.id);
  113.             }
  114.         });
  115.         // console.log("Activities to delete:")
  116.         // console.log(toDelete)
  117.  
  118.         if (activitiesToDelete.length === 0) {
  119.             alert(`${header}No scorecards found between ${formatDate(startDeleteDate)} and ${formatDate(endDeleteDate)}.`)
  120.             return;
  121.         }
  122.  
  123.         const conf = prompt(
  124. `${header}❗❗❗ Are you sure you want to permanently delete ${activitiesToDelete.length} scorecards from ${formatDate(startDeleteDate)} to ${formatDate(endDeleteDate)}? ❗❗❗
  125.  
  126. If you are sure you want to delete scorecards, type "OK" (without quotes).
  127. `);
  128.         if (conf !== "OK") {
  129.             return;
  130.         }
  131.  
  132.         let pendingRequests = activitiesToDelete.length;
  133.         let deletedRequests = 0;
  134.         activitiesToDelete.forEach(
  135.             function(scorecardId) {
  136.                 jQuery.ajax({
  137.                     beforeSend: function(request) {
  138.                         request.setRequestHeader("NK", "NT");
  139.                     },
  140.                     dataType: "text",
  141.                     method: "DELETE",
  142.                     url: `https://connect.garmin.com/modern/proxy/gcs-golfcommunity/api/v2/scorecard/${scorecardId}`,
  143.                     success: function(data) {
  144.                         pendingRequests--;
  145.                         deletedRequests++;
  146.                         onDelete();
  147.                     },
  148.                     error: function() {
  149.                         console.log(`Failed to delete scorecard with ID ${scorecardId}`);
  150.                         pendingRequests--;
  151.                         onDelete();
  152.                     }
  153.                 });
  154.             }
  155.         )
  156.  
  157.         function onDelete() {
  158.             if (pendingRequests > 0) {
  159.                 return;
  160.             }
  161.             alert(`${header}Deleted ${deletedRequests} scorecards from ${formatDate(startDeleteDate)} to ${formatDate(endDeleteDate)}.`)
  162.             if (loc.includes("/scorecards")) {
  163.                 location.reload();
  164.             }
  165.         }
  166.     }
  167. }
  168.  
  169. gcDeleteGolf();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement