Advertisement
Guest User

gc-export-golfscores

a guest
Mar 18th, 2021
1,701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* v3 (2021-03-18) */
  2. function gcExportGolfScores() {
  3.     let loc = window.location.href
  4.  
  5.     let connectURL = "https://connect.garmin.com";
  6.     if (loc.indexOf(connectURL) != 0 || typeof jQuery === "undefined") {
  7.         alert(
  8. `You must be logged into Garmin Connect to run this script.
  9.  
  10. (Your current tab must also be a Garmin Connect page with
  11. URL starting with: ${connectURL})`);
  12.         return;
  13.     }
  14.  
  15.     // Garmin Connect uses jQuery, so it's available for this script
  16.     jQuery("#_gc-golfdata_modal").remove();
  17.  
  18.     alert("Press OK to export Garmin golf data (this may take a while)");
  19.  
  20.     const details = [];
  21.     let summary = {};
  22.     let pendingRequests = 0;
  23.     jQuery.getJSON(
  24.         `https://connect.garmin.com/modern/proxy/gcs-golfcommunity/api/v2/scorecard/summary?per-page=10000&user-locale=en&_=1615882865621`,
  25.         function(_summary) {
  26.             summary =_summary;
  27.             const scorecardSummaries = (summary.scorecardSummaries || []);
  28.             pendingRequests = scorecardSummaries.length;
  29.             if (scorecardSummaries.length === 0) {
  30.                 alert("No golf scorecards found");
  31.                 return;
  32.             }
  33.             scorecardSummaries.forEach(
  34.                 function(cardSummary) {
  35.                     jQuery.getJSON(
  36.                         `https://connect.garmin.com/modern/proxy/gcs-golfcommunity/api/v2/scorecard/detail?scorecard-ids=${cardSummary.id}&include-longest-shot-distance=true`,
  37.                         function(cardDetails) {
  38.                             details.push({
  39.                                 startTime: getCardField(cardDetails, "startTime"),
  40.                                 formattedStartTime: getCardField(cardDetails, "formattedStartTime"),
  41.                                 ...cardDetails
  42.                             }
  43.                                 );
  44.                             pendingRequests--;
  45.                             onReceive();
  46.                         }
  47.                     )
  48.                     .fail(function() {
  49.                         console.log(`Failed to retrieve scorecard with ID ${cardSummary.id}`);
  50.                         pendingRequests--;
  51.                         onReceive();
  52.                     });
  53.                 }
  54.             )
  55.         }
  56.     ).fail(() => {
  57.         alert("Failed to get scorecard summary list")
  58.     })
  59.  
  60.     function onReceive() {
  61.         if (pendingRequests > 0) {
  62.             return;
  63.         }
  64.         const sortedDetails = details.sort((a,b) => ((a,b) => (a > b) - (a < b))(getCardField(a, "startTime"), getCardField(b, "startTime")))
  65.         addDialog(summary, sortedDetails);
  66.     }
  67.  
  68.     const getCardField = (cardInfo, field) => {
  69.         const scorecard = cardInfo.scorecardDetails.find(element => element && element.scorecard !== undefined)
  70.         if (scorecard) {
  71.             return scorecard.scorecard[field];
  72.         }
  73.         return "";
  74.     }
  75.  
  76.     function addDialog(summary, details) {
  77.         addCSS();
  78.         jQuery("#_gc-golfdata_modal").remove();
  79.  
  80.         const data = {
  81.             summary,
  82.             details,
  83.         }
  84.  
  85.         const output = JSON.stringify(data, null, 2)
  86.  
  87.         jQuery('body').append(`
  88.             <div id="_gc-golfdata_modal" class="_gc-golfdata-modalDialog">
  89.                 <div><a href="#" title="Close" id="_gc-golfdata-close" class="_gc-golfdata-close">X</a>
  90.    
  91.                         <h2>Garmin Golf Scorecards</h2>
  92.    
  93.                     <textarea readonly id="_gc-golfdata_textarea" rows="20" style="width:100%" spellcheck="false"
  94.                     >${output}</textarea>
  95.                     <br>
  96.                     <br>
  97.                     <div>
  98.                         <div style="float:left">
  99.                             <button class="_gc-golfdata-btn" id="_gc-golfdata_copy">Copy to Clipboard</button>
  100.                             <span id="_gc-golfdata-copied" style="display:none">Golf data copied to clipboard 👍</span>
  101.                         </div>
  102.                         <div style="float:right">
  103.                             <a class="_gc-golfdata-btn" download='golf-export.txt' href='data:text/plain;charset=utf-8,${escape(output)}'>Download</a>
  104.                         </div>
  105.                     </div>
  106.                     <div style="clear:both"></div>
  107.                 </div>
  108.             </div>
  109.     `);
  110.         jQuery("#_gc-golfdata-close").click(function() {
  111.             jQuery("#_gc-golfdata_modal").remove();
  112.             return false;
  113.         });
  114.         jQuery("#_gc-golfdata_copy").click(function() {
  115.             let el = document.getElementById("_gc-golfdata_textarea");
  116.             el.select();
  117.             document.execCommand('copy');
  118.             jQuery("#_gc-golfdata-copied").show();
  119.             return false;
  120.         });
  121.     }
  122.  
  123.     function addCSS() {
  124.         // based on https://jsfiddle.net/kumarmuthaliar/GG9Sa/1/
  125.         let styles = `
  126.     ._gc-golfdata-modalDialog {
  127.         position: fixed;
  128.         font-family: Arial, Helvetica, sans-serif;
  129.         top: 0;
  130.         right: 0;
  131.         bottom: 0;
  132.         left: 0;
  133.         background: rgba(0, 0, 0, 0.8);
  134.         z-index: 99999;
  135.         // opacity:0;
  136.         -webkit-transition: opacity 400ms ease-in;
  137.         -moz-transition: opacity 400ms ease-in;
  138.         transition: opacity 400ms ease-in;
  139.     }
  140.    
  141.     ._gc-golfdata-modalDialog > div {
  142.         width: 600px;
  143.         position: relative;
  144.         margin: 10% auto;
  145.         padding: 5px 20px 13px 20px;
  146.         border-radius: 10px;
  147.         background: #eee;
  148.         /*background: -moz-linear-gradient(#fff, #999);
  149.         background: -webkit-linear-gradient(#fff, #999);
  150.         background: -o-linear-gradient(#fff, #999);*/
  151.     }
  152.     ._gc-golfdata-close {
  153.         background: #606061;
  154.         color: #FFFFFF;
  155.         line-height: 25px;
  156.         position: absolute;
  157.         right: -12px;
  158.         text-align: center;
  159.         top: -10px;
  160.         width: 24px;
  161.         text-decoration: none;
  162.         font-weight: bold;
  163.         -webkit-border-radius: 12px;
  164.         -moz-border-radius: 12px;
  165.         border-radius: 12px;
  166.         -moz-box-shadow: 1px 1px 3px #000;
  167.         -webkit-box-shadow: 1px 1px 3px #000;
  168.         box-shadow: 1px 1px 3px #000;
  169.     }
  170.     ._gc-golfdata-close:hover {
  171.         background: #00d9ff;
  172.     }
  173.    
  174.     ._gc-golfdata-btn, ._gc-golfdata-btn:hover, ._gc-golfdata-btn:visited, ._gc-golfdata-btn:active {
  175.         color: #fff;
  176.         background-color: #337ab7;
  177.         border-color: #2e6da4;
  178.         text-decoration:none;
  179.    
  180.         display: inline-block;
  181.         margin-bottom: 0;
  182.         font-weight: 400;
  183.         text-align: center;
  184.         white-space: nowrap;
  185.         vertical-align: middle;
  186.         -ms-touch-action: manipulation;
  187.         touch-action: manipulation;
  188.         cursor: pointer;
  189.         background-image: none;
  190.         border: 1px solid transparent;
  191.         border-top-color: transparent;
  192.         border-right-color: transparent;
  193.         border-bottom-color: transparent;
  194.         border-left-color: transparent;
  195.         padding: 6px 12px;
  196.         font-size: 14px;
  197.         line-height: 1.42857143;
  198.         border-radius: 4px;
  199.     }
  200.  
  201.     #_gc-golfdata_textarea {
  202.         font-family: "Lucida Console", Monaco, Monospace
  203.     }
  204.     `
  205.    
  206.         jQuery("#_gc-golfdata_styles").remove();
  207.         let styleSheet = document.createElement("style")
  208.         styleSheet.type = "text/css"
  209.         styleSheet.id = "_gc-golfdata_styles"
  210.         styleSheet.innerText = styles
  211.         document.head.appendChild(styleSheet);
  212.    
  213.     }    
  214. }
  215.  
  216. gcExportGolfScores();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement