Advertisement
Guest User

gc export sleep data 2021-02-25g

a guest
Feb 25th, 2021
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getGCSleepData() {
  2.     // get sleep data from garmin connect
  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-sleepdata_modal").remove();
  17.  
  18.     let xhr = new XMLHttpRequest();
  19.     xhr.open('GET', 'https://connect.garmin.com/modern/currentuser-service/user/info');
  20.     xhr.setRequestHeader("NK", "NT")
  21.     xhr.onload = function () {
  22.         let obj = JSON.parse(xhr.response)
  23.         _getSleepData(obj.displayName);
  24.     };
  25.     xhr.send()
  26.  
  27.  
  28.     function _getSleepData(userID) {
  29.         let sleepURL = "https://connect.garmin.com/modern/sleep/"// + yyyy/mm/dd
  30.  
  31.         let defaultDate = new Date();
  32.         // defaultDate.setDate(defaultDate.getDate() - 1);
  33.         defaultDate = formatDate(defaultDate)
  34.  
  35.         if (loc.indexOf(sleepURL) == 0) {
  36.             defaultDate = loc.replace(sleepURL, "");
  37.         }
  38.  
  39.         let date = prompt(
  40. `────────────────────────
  41. Export Garmin Sleep Data
  42. ────────────────────────
  43. (run this script from a GC sleep page to use its date)
  44.  
  45. Enter date:`, defaultDate)
  46.  
  47.         if (!date) {
  48.             return;
  49.         }
  50.  
  51.         let xhr = new XMLHttpRequest();
  52.         xhr.open('GET', 'https://connect.garmin.com/modern/proxy/wellness-service/wellness/dailySleepData/' + userID + '?date=' + date);
  53.         xhr.setRequestHeader("NK", "NT")
  54.         xhr.onload = function () {
  55.             let obj = JSON.parse(xhr.response)
  56.             let output = JSON.stringify(obj, null, 2);
  57.             addDialog(date, output)
  58.         };
  59.         xhr.send()
  60.     }
  61.  
  62.     function formatDate(date) {
  63.         let d = new Date(date),
  64.             month = '' + (d.getMonth() + 1),
  65.             day = '' + d.getDate(),
  66.             year = d.getFullYear();
  67.    
  68.         if (month.length < 2)
  69.             month = '0' + month;
  70.         if (day.length < 2)
  71.             day = '0' + day;
  72.    
  73.         return [year, month, day].join('-');
  74.     }
  75.    
  76.     function addDialog(date, output) {
  77.         addCSS();
  78.         jQuery("#_gc-sleepdata_modal").remove();
  79.         jQuery('body').append(`
  80.             <div id="_gc-sleepdata_modal" class="_gc-sleepdata-modalDialog">
  81.                 <div><a href="#" title="Close" id="_gc-sleepdata-close" class="_gc-sleepdata-close">X</a>
  82.    
  83.                         <h2>Sleep Data for ${date}</h2>
  84.    
  85.                     <textarea readonly id="_gc-sleepdata_textarea" rows="20" style="width:100%" spellcheck="false"
  86.                     >${output}</textarea>
  87.                     <br>
  88.                     <br>
  89.                     <button class="_gc-sleepdata-btn" id="_gc-sleepdata_copy">Copy to Clipboard</button>
  90.                     <span id="_gc-sleepdata-copied" style="display:none">Sleep data copied to clipboard 👍</span>
  91.                 </div>
  92.             </div>
  93.     `);
  94.         jQuery("#_gc-sleepdata-close").click(function() {
  95.             jQuery("#_gc-sleepdata_modal").remove();
  96.             return false;
  97.         });
  98.         jQuery("#_gc-sleepdata_copy").click(function() {
  99.             let el = document.getElementById("_gc-sleepdata_textarea");
  100.             el.select();
  101.             document.execCommand('copy');
  102.             jQuery("#_gc-sleepdata-copied").show();
  103.             return false;
  104.         });
  105.     }
  106.  
  107.     function addCSS() {
  108.         // based on https://jsfiddle.net/kumarmuthaliar/GG9Sa/1/
  109.         let styles = `
  110.     ._gc-sleepdata-modalDialog {
  111.         position: fixed;
  112.         font-family: Arial, Helvetica, sans-serif;
  113.         top: 0;
  114.         right: 0;
  115.         bottom: 0;
  116.         left: 0;
  117.         background: rgba(0, 0, 0, 0.8);
  118.         z-index: 99999;
  119.         // opacity:0;
  120.         -webkit-transition: opacity 400ms ease-in;
  121.         -moz-transition: opacity 400ms ease-in;
  122.         transition: opacity 400ms ease-in;
  123.     }
  124.    
  125.     ._gc-sleepdata-modalDialog > div {
  126.         width: 400px;
  127.         position: relative;
  128.         margin: 10% auto;
  129.         padding: 5px 20px 13px 20px;
  130.         border-radius: 10px;
  131.         background: #eee;
  132.         /*background: -moz-linear-gradient(#fff, #999);
  133.         background: -webkit-linear-gradient(#fff, #999);
  134.         background: -o-linear-gradient(#fff, #999);*/
  135.     }
  136.     ._gc-sleepdata-close {
  137.         background: #606061;
  138.         color: #FFFFFF;
  139.         line-height: 25px;
  140.         position: absolute;
  141.         right: -12px;
  142.         text-align: center;
  143.         top: -10px;
  144.         width: 24px;
  145.         text-decoration: none;
  146.         font-weight: bold;
  147.         -webkit-border-radius: 12px;
  148.         -moz-border-radius: 12px;
  149.         border-radius: 12px;
  150.         -moz-box-shadow: 1px 1px 3px #000;
  151.         -webkit-box-shadow: 1px 1px 3px #000;
  152.         box-shadow: 1px 1px 3px #000;
  153.     }
  154.     ._gc-sleepdata-close:hover {
  155.         background: #00d9ff;
  156.     }
  157.    
  158.     ._gc-sleepdata-btn {
  159.         color: #fff;
  160.         background-color: #337ab7;
  161.         border-color: #2e6da4;
  162.    
  163.         display: inline-block;
  164.         margin-bottom: 0;
  165.         font-weight: 400;
  166.         text-align: center;
  167.         white-space: nowrap;
  168.         vertical-align: middle;
  169.         -ms-touch-action: manipulation;
  170.         touch-action: manipulation;
  171.         cursor: pointer;
  172.         background-image: none;
  173.         border: 1px solid transparent;
  174.         border-top-color: transparent;
  175.         border-right-color: transparent;
  176.         border-bottom-color: transparent;
  177.         border-left-color: transparent;
  178.         padding: 6px 12px;
  179.         font-size: 14px;
  180.         line-height: 1.42857143;
  181.         border-radius: 4px;
  182.     }
  183.     `
  184.    
  185.         jQuery("#_gc-sleepdata_styles").remove();
  186.         let styleSheet = document.createElement("style")
  187.         styleSheet.type = "text/css"
  188.         styleSheet.id = "_gc-sleepdata_styles"
  189.         styleSheet.innerText = styles
  190.         document.head.appendChild(styleSheet);
  191.    
  192.     }    
  193. }
  194.  
  195. getGCSleepData();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement