Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getGCSleepData() {
- // get sleep data from garmin connect
- let loc = window.location.href
- let connectURL = "https://connect.garmin.com";
- if (loc.indexOf(connectURL) != 0 || typeof jQuery === "undefined") {
- alert(
- `You must be logged into Garmin Connect to run this script.
- (Your current tab must also be a Garmin Connect page with
- URL starting with: ${connectURL})`);
- return;
- }
- // Garmin Connect uses jQuery, so it's available for this script
- jQuery("#_gc-sleepdata_modal").remove();
- let xhr = new XMLHttpRequest();
- xhr.open('GET', 'https://connect.garmin.com/modern/currentuser-service/user/info');
- xhr.setRequestHeader("NK", "NT")
- xhr.onload = function () {
- let obj = JSON.parse(xhr.response)
- _getSleepData(obj.displayName);
- };
- xhr.send()
- function _getSleepData(userID) {
- let sleepURL = "https://connect.garmin.com/modern/sleep/"// + yyyy/mm/dd
- let defaultDate = new Date();
- // defaultDate.setDate(defaultDate.getDate() - 1);
- defaultDate = formatDate(defaultDate)
- if (loc.indexOf(sleepURL) == 0) {
- defaultDate = loc.replace(sleepURL, "");
- }
- let date = prompt(
- `────────────────────────
- Export Garmin Sleep Data
- ────────────────────────
- (run this script from a GC sleep page to use its date)
- Enter date:`, defaultDate)
- if (!date) {
- return;
- }
- let xhr = new XMLHttpRequest();
- xhr.open('GET', 'https://connect.garmin.com/modern/proxy/wellness-service/wellness/dailySleepData/' + userID + '?date=' + date);
- xhr.setRequestHeader("NK", "NT")
- xhr.onload = function () {
- let obj = JSON.parse(xhr.response)
- let output = JSON.stringify(obj, null, 2);
- addDialog(date, output)
- };
- xhr.send()
- }
- function formatDate(date) {
- let d = new Date(date),
- month = '' + (d.getMonth() + 1),
- day = '' + d.getDate(),
- year = d.getFullYear();
- if (month.length < 2)
- month = '0' + month;
- if (day.length < 2)
- day = '0' + day;
- return [year, month, day].join('-');
- }
- function addDialog(date, output) {
- addCSS();
- jQuery("#_gc-sleepdata_modal").remove();
- jQuery('body').append(`
- <div id="_gc-sleepdata_modal" class="_gc-sleepdata-modalDialog">
- <div><a href="#" title="Close" id="_gc-sleepdata-close" class="_gc-sleepdata-close">X</a>
- <h2>Sleep Data for ${date}</h2>
- <textarea readonly id="_gc-sleepdata_textarea" rows="20" style="width:100%" spellcheck="false"
- >${output}</textarea>
- <br>
- <br>
- <button class="_gc-sleepdata-btn" id="_gc-sleepdata_copy">Copy to Clipboard</button>
- <span id="_gc-sleepdata-copied" style="display:none">Sleep data copied to clipboard 👍</span>
- </div>
- </div>
- `);
- jQuery("#_gc-sleepdata-close").click(function() {
- jQuery("#_gc-sleepdata_modal").remove();
- return false;
- });
- jQuery("#_gc-sleepdata_copy").click(function() {
- let el = document.getElementById("_gc-sleepdata_textarea");
- el.select();
- document.execCommand('copy');
- jQuery("#_gc-sleepdata-copied").show();
- return false;
- });
- }
- function addCSS() {
- // based on https://jsfiddle.net/kumarmuthaliar/GG9Sa/1/
- let styles = `
- ._gc-sleepdata-modalDialog {
- position: fixed;
- font-family: Arial, Helvetica, sans-serif;
- top: 0;
- right: 0;
- bottom: 0;
- left: 0;
- background: rgba(0, 0, 0, 0.8);
- z-index: 99999;
- // opacity:0;
- -webkit-transition: opacity 400ms ease-in;
- -moz-transition: opacity 400ms ease-in;
- transition: opacity 400ms ease-in;
- }
- ._gc-sleepdata-modalDialog > div {
- width: 400px;
- position: relative;
- margin: 10% auto;
- padding: 5px 20px 13px 20px;
- border-radius: 10px;
- background: #eee;
- /*background: -moz-linear-gradient(#fff, #999);
- background: -webkit-linear-gradient(#fff, #999);
- background: -o-linear-gradient(#fff, #999);*/
- }
- ._gc-sleepdata-close {
- background: #606061;
- color: #FFFFFF;
- line-height: 25px;
- position: absolute;
- right: -12px;
- text-align: center;
- top: -10px;
- width: 24px;
- text-decoration: none;
- font-weight: bold;
- -webkit-border-radius: 12px;
- -moz-border-radius: 12px;
- border-radius: 12px;
- -moz-box-shadow: 1px 1px 3px #000;
- -webkit-box-shadow: 1px 1px 3px #000;
- box-shadow: 1px 1px 3px #000;
- }
- ._gc-sleepdata-close:hover {
- background: #00d9ff;
- }
- ._gc-sleepdata-btn {
- color: #fff;
- background-color: #337ab7;
- border-color: #2e6da4;
- display: inline-block;
- margin-bottom: 0;
- font-weight: 400;
- text-align: center;
- white-space: nowrap;
- vertical-align: middle;
- -ms-touch-action: manipulation;
- touch-action: manipulation;
- cursor: pointer;
- background-image: none;
- border: 1px solid transparent;
- border-top-color: transparent;
- border-right-color: transparent;
- border-bottom-color: transparent;
- border-left-color: transparent;
- padding: 6px 12px;
- font-size: 14px;
- line-height: 1.42857143;
- border-radius: 4px;
- }
- `
- jQuery("#_gc-sleepdata_styles").remove();
- let styleSheet = document.createElement("style")
- styleSheet.type = "text/css"
- styleSheet.id = "_gc-sleepdata_styles"
- styleSheet.innerText = styles
- document.head.appendChild(styleSheet);
- }
- }
- getGCSleepData();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement