Advertisement
Guest User

gc-export-golfscores

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