Directly download Wikiloc GPX files

This is a script that helps you directly download GPX file from WikiLoc.

  1. // ==UserScript==
  2. // @name Directly download Wikiloc GPX files
  3. // @namespace https://www.wikiloc.com/
  4. // @version 2025-04-01
  5. // @description This is a script that helps you directly download GPX file from WikiLoc.
  6. // @author WikiLoc
  7. // @match *://*.wikiloc.com/*
  8. // @icon https://sc.wklcdn.com/favicon.ico
  9. // @grant none
  10. // @require https://code.jquery.com/jquery-3.7.1.slim.min.js
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. function extractIdFromUrl(url) {
  18. // Check if the URL contains "download.do?id="
  19. if (url && url.includes('download.do?id=')) {
  20. // Use regex to extract the ID
  21. const match = url.match(/download\.do\?id=(\d+)/);
  22. if (match && match[1]) {
  23. return match[1];
  24. }
  25. }
  26. return null;
  27. }
  28.  
  29. // Function to initiate download with the extracted ID
  30. function initiateDownload(id) {
  31.  
  32. /*
  33. POST TO: /wikiloc/downloadToFile.do
  34. id: 163518627
  35. event: download
  36. format: gpx
  37. selFormat: gpx
  38. filter: simplified500
  39. */
  40.  
  41. console.log('Initiating download for ID:', id);
  42.  
  43. // Create form data for the POST request
  44. const formData = new FormData();
  45. formData.append('id', id);
  46. formData.append('event', 'download');
  47. formData.append('format', 'gpx');
  48. formData.append('selFormat', 'gpx');
  49. formData.append('filter', 'simplified500');
  50.  
  51. // Get the base URL to ensure we're posting to the correct domain
  52. const baseUrl = window.location.origin;
  53. const downloadUrl = `${baseUrl}/wikiloc/downloadToFile.do`;
  54.  
  55. console.log('Sending POST request to:', downloadUrl);
  56.  
  57. // Make the POST request
  58. fetch(downloadUrl, {
  59. method: 'POST',
  60. body: formData,
  61. credentials: 'include' // Include cookies for authentication
  62. })
  63. .then(response => {
  64. if (response.ok) {
  65. console.log('Download request successful');
  66. // For file downloads, we need to handle the response as a blob
  67. return response.blob();
  68. } else {
  69. throw new Error('Download request failed with status: ' + response.status);
  70. }
  71. })
  72. .then(blob => {
  73. // Create a download link for the blob
  74. const url = window.URL.createObjectURL(blob);
  75. const a = document.createElement('a');
  76. a.style.display = 'none';
  77. a.href = url;
  78.  
  79. // Try to get filename from Content-Disposition header, or use a default
  80. const filename = `wikiloc_track_${id}.gpx`;
  81. a.download = filename;
  82.  
  83. // Append to the document, click it, and clean up
  84. document.body.appendChild(a);
  85. a.click();
  86. window.URL.revokeObjectURL(url);
  87. document.body.removeChild(a);
  88.  
  89. console.log('Download initiated for file:', filename);
  90. })
  91. .catch(error => {
  92. console.error('Error during download:', error);
  93. alert('Download failed: ' + error.message);
  94. });
  95. }
  96.  
  97. $(document).on('click', '.btn-download', function(e) {
  98. e.preventDefault();
  99. e.stopPropagation();
  100.  
  101. const url = $(this).attr('href');
  102.  
  103. if (url) {
  104. const id = extractIdFromUrl(url);
  105. if (id) {
  106. console.log('Wikiloc Download ID:', id);
  107. initiateDownload(id);
  108. }
  109. }
  110. });
  111.  
  112. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址