Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // v6: 2021-04-13
- function getGCIntensity() {
- // get intensity minutes from garmin connect
- 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-intensitydata_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)
- _getGCIntensity(obj.displayName);
- };
- xhr.send()
- function _getGCIntensity(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 mondayDelta = (t) => {
- let delta = 1 - t.getDay();
- if (delta == 1) {
- delta = -6;
- }
- return delta;
- }
- let defaultStartOfWeek = formatDate(incrementDate(today, mondayDelta(today)));
- let startOfWeek = promptDate(
- `Garmin Intensity Minutes ⚡
- Get Intensity Minutes for week of:
- `,
- defaultStartOfWeek
- )
- if (!startOfWeek) {
- return;
- }
- startOfWeek = incrementDate(startOfWeek, mondayDelta(startOfWeek));
- let endOfWeek = formatDate(incrementDate(startOfWeek, 6));
- startOfWeek = formatDate(startOfWeek);
- console.log(startOfWeek)
- console.log(endOfWeek)
- let xhr = new XMLHttpRequest();
- xhr.open('GET', `https://connect.garmin.com/modern/proxy/userstats-service/wellness/daily/${username}?fromDate=${startOfWeek}&untilDate=${endOfWeek}&metricId=51&metricId=52`);
- xhr.setRequestHeader("NK", "NT")
- xhr.onload = function () {
- let obj = JSON.parse(xhr.response)
- let output = JSON.stringify(obj, null, 2);
- addDialog(output, obj)
- };
- xhr.send()
- }
- function addDialog(output, obj) {
- addCSS();
- jQuery("#_gc-intensitydata_modal").remove();
- let rows = "";
- let vig = 0;
- let moderate = 0;
- let total = 0;
- const metricsMap = obj.allMetrics.metricsMap
- const days = [
- "Monday",
- "Tuesday",
- "Wednesday",
- "Thursday",
- "Friday",
- "Saturday",
- "Sunday",
- ]
- for (let i = 0; i < metricsMap.WELLNESS_MODERATE_INTENSITY_MINUTES.length; i++) {
- rows +=
- `
- <tr>
- <td>${metricsMap.WELLNESS_MODERATE_INTENSITY_MINUTES[i].calendarDate}</td>
- <td>${days[i]}</td>
- <td class="right">${metricsMap.WELLNESS_MODERATE_INTENSITY_MINUTES[i].value}</td>
- <td class="right">${metricsMap.WELLNESS_VIGOROUS_INTENSITY_MINUTES[i].value}</td>
- </tr>
- `;
- vig += metricsMap.WELLNESS_VIGOROUS_INTENSITY_MINUTES[i].value;
- moderate += metricsMap.WELLNESS_MODERATE_INTENSITY_MINUTES[i].value;
- }
- total = vig * 2 + moderate;
- rows +=
- `
- <tr>
- <td> </td>
- <td> </td>
- <td class="right"><b>${moderate}</b></td>
- <td class="right"><b>${vig}</b></td>
- </tr>
- `
- jQuery('body').append(`
- <div id="_gc-intensitydata_modal" class="_gc-intensitydata_modal-wrapper">
- <div class="_gc-intensitydata-modalDialog">
- <div><a href="#" title="Close" id="_gc-intensitydata-close" class="_gc-intensitydata-close">X</a>
- <h2>Garmin Intensity Minutes ⚡</h2>
- <div>
- <table class="gc-intensitydata-styled-table">
- <tr>
- <th>Date</th><th>Day of Week</th><th class="right">Moderate</th><th class="right">Vigorous</th>
- </tr>
- ${rows}
- </table>
- </div>
- <p><b>Total Intensity Minutes: ${total}</b></p>
- Raw Data:
- <textarea readonly id="_gc-intensitydata_textarea" rows="2" style="width:100%" spellcheck="false"
- >${output}</textarea>
- <br>
- <br>
- <button class="_gc-intensitydata-btn" id="_gc-intensitydata_copy">Copy to Clipboard</button>
- <span id="_gc-intensitydata-copied" style="display:none">Intensitydata copied to clipboard 👍</span>
- </div>
- </div>
- </div>
- `);
- jQuery("#_gc-intensitydata-close").click(function() {
- jQuery("#_gc-intensitydata_modal").remove();
- return false;
- });
- jQuery("#_gc-intensitydata_copy").click(function() {
- let el = document.getElementById("_gc-intensitydata_textarea");
- el.select();
- document.execCommand('copy');
- jQuery("#_gc-intensitydata-copied").show();
- return false;
- });
- }
- 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 addCSS() {
- // based on https://jsfiddle.net/kumarmuthaliar/GG9Sa/1/
- //https://dev.to/dcodeyt/creating-beautiful-html-tables-with-css-428l
- let styles = `
- .gc-intensitydata-styled-table {
- border-collapse: collapse;
- margin: 25px 0;
- font-size: 0.9em;
- font-family: sans-serif;
- min-width: 400px;
- box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
- text-align: left;
- }
- .gc-intensitydata-styled-table .right {
- text-align: right
- }
- .gc-intensitydata-styled-table thead tr {
- background-color: #009879;
- color: #ffffff;
- }
- .gc-intensitydata-styled-table th,
- .gc-intensitydata-styled-table td {
- padding: 12px 15px;
- }
- .gc-intensitydata-styled-table tbody tr {
- border-bottom: 1px solid #dddddd;
- }
- .gc-intensitydata-styled-table tbody tr:nth-of-type(even) {
- background-color: #f3f3f3;
- }
- .gc-intensitydata-styled-table tbody tr:last-of-type {
- border-bottom: 2px solid #009879;
- }
- .gc-intensitydata-styled-table tbody tr.active-row {
- font-weight: bold;
- color: #009879;
- }
- ._gc-intensitydata_modal-wrapper {
- }
- ._gc-intensitydata-modalDialog {
- position: fixed;
- overflow-y:scroll;
- 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-intensitydata-modalDialog > div {
- width: 420px;
- 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-intensitydata-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-intensitydata-close:hover {
- background: #00d9ff;
- }
- ._gc-intensitydata-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-intensitydata_styles").remove();
- let styleSheet = document.createElement("style")
- styleSheet.type = "text/css"
- styleSheet.id = "_gc-intensitydata_styles"
- styleSheet.innerText = styles
- document.head.appendChild(styleSheet);
- }
- }
- getGCIntensity();
Advertisement
Add Comment
Please, Sign In to add comment