Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* v1 (2021-04-14) */
- function gcExportDailySteps() {
- let loc = window.location.href
- let connectURL = "https://connect.garmin.com";
- let dailyURL = "https://connect.garmin.com/modern/daily-summary/"
- if (loc.indexOf(connectURL) != 0 || typeof jQuery === "undefined") {
- alert(
- `You must be logged into Garmin Connect to run this script: : ${connectURL}`
- );
- return;
- }
- // Garmin Connect uses jQuery, so it's available for this script
- jQuery("#_gc-steps_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)
- _gcExportDailySteps(obj.displayName);
- };
- xhr.send()
- function _gcExportDailySteps(username) {
- let today = new Date();
- if (loc.indexOf(dailyURL) == 0) {
- let todayStr = loc.replace(dailyURL, "");
- const dateRegExp = /^(\d\d\d\d)-(\d\d)-(\d\d)$/;
- const match = todayStr.match(dateRegExp);
- if (match && match.length !== 0) {
- today = new Date(match[1], match[2]-1, match[3]);
- }
- }
- let defaultStartDate = formatDate(incrementDate(today, -6));
- let startDate = promptDate(
- `Garmin Steps"
- Get daily steps for date starting:
- `,
- defaultStartDate
- )
- if (!startDate) {
- return;
- }
- let defaultEndDate = formatDate(today, -6);
- let endDate = promptDate(
- `Garmin Steps"
- Get daily steps for date ending:
- `,
- defaultEndDate
- )
- if (!endDate) {
- return;
- }
- startDate = formatDate(startDate);
- endDate = formatDate(endDate);
- let xhr = new XMLHttpRequest();
- xhr.open('GET', `https://connect.garmin.com/modern/proxy/userstats-service/wellness/daily/${username}?fromDate=${startDate}&untilDate=${endDate}&metricId=29&metricId=38&grpParentActType=false`);
- xhr.setRequestHeader("NK", "NT")
- xhr.onload = function () {
- let obj = JSON.parse(xhr.response)
- addDialog(obj, startDate, endDate)
- };
- xhr.send()
- }
- function incrementDate(date, amount) {
- var tmpDate = new Date(date);
- tmpDate.setDate(tmpDate.getDate() + amount)
- return tmpDate;
- }
- 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 promptDate(str, def) {
- while (true) {
- const val = prompt(str, def);
- if (!val) {
- return val;
- }
- const dateRegExp = /^(\d\d\d\d)-(\d\d)-(\d\d)$/;
- const match = val.match(dateRegExp);
- if (!match || match.length == 0) {
- continue;
- }
- let d;
- d = new Date(match[1], match[2]-1, match[3], 0, 0, 0);
- // console.log(d)
- return d;
- }
- }
- function getCSV(data) {
- const metricsMap = data.allMetrics.metricsMap;
- const days = [
- "Sunday",
- "Monday",
- "Tuesday",
- "Wednesday",
- "Thursday",
- "Friday",
- "Saturday",
- ]
- let csv =
- `Date,Day of Week,Actual Steps,Goal Steps\n`;
- for (let i = 0; i < metricsMap.WELLNESS_TOTAL_STEPS.length; i++) {
- const date = metricsMap.WELLNESS_TOTAL_STEPS[i].calendarDate;
- const dateRegExp = /^(\d\d\d\d)-(\d\d)-(\d\d)$/;
- const match = date.match(dateRegExp);
- let day = "";
- if (match && match.length > 3) {
- let d;
- d = new Date(match[1], match[2]-1, match[3], 0, 0, 0);
- let dayNum = d.getDay();
- day = days[dayNum];
- }
- csv +=
- `${date},${day},${metricsMap.WELLNESS_TOTAL_STEPS[i].value},${metricsMap.WELLNESS_TOTAL_STEP_GOAL[i].value}\n`;
- }
- return csv;
- }
- function addDialog(data, startDate, endDate) {
- addCSS();
- jQuery("#_gc-steps_modal").remove();
- const output = JSON.stringify(data, null, 2)
- const csv = getCSV(data);
- jQuery('body').append(`
- <div id="_gc-steps_modal" class="_gc-steps-modalDialog">
- <div><a href="#" title="Close" id="_gc-steps-close" class="_gc-steps-close">X</a>
- <h2>Garmin Steps: ${startDate} to ${endDate}</h2>
- CSV:
- <textarea readonly id="_gc-steps-csv_textarea" rows="4" style="width:100%" spellcheck="false"
- >${csv}</textarea>
- <br>
- <br>
- <div>
- <div style="float:left">
- <button class="_gc-steps-btn" id="_gc-steps-csv_copy">Copy CSV to Clipboard</button>
- <span id="_gc-steps-csv-copied" style="display:none">CSV data copied to clipboard 👍</span>
- </div>
- <div style="float:right">
- <a class="_gc-steps-btn" download='steps-export.csv' href='data:text/plain;charset=utf-8,${escape(csv)}'>Download CSV</a>
- </div>
- <div style="clear:both"></div>
- <hr>
- JSON:
- <textarea readonly id="_gc-steps_textarea" rows="4" style="width:100%" spellcheck="false"
- >${output}</textarea>
- <br>
- <br>
- <div>
- <div style="float:left">
- <button class="_gc-steps-btn" id="_gc-steps_copy">Copy JSON to Clipboard</button>
- <span id="_gc-steps-copied" style="display:none">JSON data copied to clipboard 👍</span>
- </div>
- <div style="float:right">
- <a class="_gc-steps-btn" download='steps-export.txt' href='data:text/plain;charset=utf-8,${escape(output)}'>Download JSON</a>
- </div>
- </div>
- <div style="clear:both"></div>
- </div>
- </div>
- </div>
- `);
- jQuery("#_gc-steps-close").click(function() {
- jQuery("#_gc-steps_modal").remove();
- return false;
- });
- jQuery("#_gc-steps_copy").click(function() {
- let el = document.getElementById("_gc-steps_textarea");
- el.select();
- document.execCommand('copy');
- jQuery("#_gc-steps-copied").show();
- return false;
- });
- jQuery("#_gc-steps-csv_copy").click(function() {
- let el = document.getElementById("_gc-steps-csv_textarea");
- el.select();
- document.execCommand('copy');
- jQuery("#_gc-steps-csv-copied").show();
- return false;
- });
- }
- function addCSS() {
- // based on https://jsfiddle.net/kumarmuthaliar/GG9Sa/1/
- let styles = `
- ._gc-steps-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-steps-modalDialog > div {
- width: 600px;
- position: relative;
- margin: 20px 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-steps-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-steps-close:hover {
- background: #00d9ff;
- }
- ._gc-steps-btn, ._gc-steps-btn:hover, ._gc-steps-btn:visited, ._gc-steps-btn:active {
- color: #fff;
- background-color: #337ab7;
- border-color: #2e6da4;
- text-decoration:none;
- 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;
- }
- #_gc-steps_textarea,
- #_gc-steps-csv_textarea {
- font-family: "Lucida Console", Monaco, Monospace
- }
- `
- jQuery("#_gc-steps_styles").remove();
- let styleSheet = document.createElement("style")
- styleSheet.type = "text/css"
- styleSheet.id = "_gc-steps_styles"
- styleSheet.innerText = styles
- document.head.appendChild(styleSheet);
- }
- }
- gcExportDailySteps();
Advertisement
Add Comment
Please, Sign In to add comment