Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //v5
- function gcDeleteGolf() {
- 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;
- }
- const header =
- `Garmin Connect - Golf Scorecards Deletion Tool
- ────────────────────────────────────────────────────
- `;
- jQuery.ajax({
- beforeSend: function(request) {
- request.setRequestHeader("NK", "NT");
- },
- dataType: "json",
- url: `https://connect.garmin.com/modern/proxy/gcs-golfcommunity/api/v2/scorecard/summary?per-page=10000`,
- success: function(data) {
- onGolfList(data);
- },
- error: function() {
- alert(`${header}Unable to retrieve list of golf scorecards.`);
- }
- });
- 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, end) {
- 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;
- if (!end) {
- d = new Date(match[1], match[2]-1, match[3]);
- } else {
- d = new Date(match[1], match[2]-1, match[3], 23, 59, 59);
- }
- // console.log(d)
- return d;
- }
- }
- function onGolfList(data) {
- let earliestScorecard;
- let latestScorecard;
- const scorecardSummaries = data.scorecardSummaries || [];
- if (scorecardSummaries.length === 0) {
- alert(`${header}No scorecards found.`)
- return;
- }
- scorecardSummaries.forEach(s => {
- const activityTime = new Date(s.startTime);
- if (!earliestScorecard || activityTime < earliestScorecard) {
- earliestScorecard = activityTime;
- }
- if (!latestScorecard || activityTime > latestScorecard) {
- latestScorecard = activityTime;
- }
- })
- const dateHeader =
- `${header}You have ${scorecardSummaries.length} scorecards from ${formatDate(earliestScorecard)} to ${formatDate(latestScorecard)}.
- `;
- const startDeleteDate = promptDate(`${dateHeader}Enter start date for deletion (YYYY-MM-DD):`, formatDate(earliestScorecard))
- if (!startDeleteDate) {
- return;
- }
- const endDeleteDate = promptDate(`${dateHeader}Enter end date for deletion (YYYY-MM-DD):`, formatDate(latestScorecard), true)
- if (!endDeleteDate) {
- return;
- }
- const activitiesToDelete = [];
- scorecardSummaries.forEach(s => {
- const activityTime = new Date(s.startTime);
- if (activityTime >= startDeleteDate && activityTime <= endDeleteDate) {
- activitiesToDelete.push(s.id);
- }
- });
- // console.log("Activities to delete:")
- // console.log(toDelete)
- if (activitiesToDelete.length === 0) {
- alert(`${header}No scorecards found between ${formatDate(startDeleteDate)} and ${formatDate(endDeleteDate)}.`)
- return;
- }
- const conf = prompt(
- `${header}❗❗❗ Are you sure you want to permanently delete ${activitiesToDelete.length} scorecards from ${formatDate(startDeleteDate)} to ${formatDate(endDeleteDate)}? ❗❗❗
- If you are sure you want to delete scorecards, type "OK" (without quotes).
- `);
- if (conf !== "OK") {
- return;
- }
- let pendingRequests = activitiesToDelete.length;
- let deletedRequests = 0;
- activitiesToDelete.forEach(
- function(scorecardId) {
- jQuery.ajax({
- beforeSend: function(request) {
- request.setRequestHeader("NK", "NT");
- },
- dataType: "text",
- method: "DELETE",
- url: `https://connect.garmin.com/modern/proxy/gcs-golfcommunity/api/v2/scorecard/${scorecardId}`,
- success: function(data) {
- pendingRequests--;
- deletedRequests++;
- onDelete();
- },
- error: function() {
- console.log(`Failed to delete scorecard with ID ${scorecardId}`);
- pendingRequests--;
- onDelete();
- }
- });
- }
- )
- function onDelete() {
- if (pendingRequests > 0) {
- return;
- }
- alert(`${header}Deleted ${deletedRequests} scorecards from ${formatDate(startDeleteDate)} to ${formatDate(endDeleteDate)}.`)
- if (loc.includes("/scorecards")) {
- location.reload();
- }
- }
- }
- }
- gcDeleteGolf();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement