您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Fetch map data via AJAX, generate KML for AllTrails pages
当前为
// ==UserScript== // @name AllTrails KML Downloader // @namespace http://tampermonkey.net/ // @version 1.1 // @license GNU GPLv3 // @description Fetch map data via AJAX, generate KML for AllTrails pages // @match https://www.alltrails.com/trail/* // @grant GM_xmlhttpRequest // @grant GM_download // @require https://cdnjs.cloudflare.com/ajax/libs/mapbox-polyline/1.1.1/polyline.min.js // ==/UserScript== (function() { 'use strict'; // Add the "Download KML" button to the page function createDownloadButton() { const button = document.createElement('button'); button.textContent = 'Download KML'; button.style.position = 'fixed'; button.style.top = '10px'; button.style.right = '10px'; button.style.zIndex = '9999'; button.style.padding = '10px 15px'; button.style.background = '#4CAF50'; button.style.color = 'white'; button.style.border = 'none'; button.style.borderRadius = '5px'; button.style.fontSize = '16px'; button.style.cursor = 'pointer'; button.onclick = function() { fetchMapPageAndGenerateKML(); }; document.body.appendChild(button); } // Function to fetch map data via AJAX from the map page URL function fetchMapPageAndGenerateKML() { const trailUrl = window.location.href; // e.g., "https://www.alltrails.com/trail/us/florida/lake-myakka-trail" const mapUrl = trailUrl.replace('/trail/', '/explore/trail/') + '?mobileMap=false&initFlyover=true&flyoverReturnToTrail'; console.log(`Fetching map data from: ${mapUrl}`); GM_xmlhttpRequest({ method: 'GET', url: mapUrl, onload: function(response) { const mapPageHtml = response.responseText; const mapDataMatch = mapPageHtml.match(/<div data-react-class="SearchApp" data-react-props="({.+?})"/); if (mapDataMatch && mapDataMatch[1]) { const mapDataJson = JSON.parse(mapDataMatch[1].replace(/"/g, '"')); generateKML(mapDataJson); } else { console.error('Failed to extract map data'); } }, onerror: function() { console.error('Error fetching map page.'); } }); } // Convert the map data to KML format function generateKML(mapData) { const trailName = mapData.initialExploreMap.name; const waypoints = mapData.initialExploreMap.waypoints; const routes = mapData.initialExploreMap.routes; let kmlContent = `<?xml version="1.0" encoding="UTF-8"?>\n<kml xmlns="http://www.opengis.net/kml/2.2">\n<Document>\n`; // Add routes (trails) routes.forEach(route => { route.lineSegments.forEach(segment => { const pointsData = segment.polyline.pointsData; // Decode the polyline (if pointsData is not already decoded) if (typeof pointsData === 'string') { const decodedPoints = polyline.decode(pointsData); let coordinates = decodedPoints.map(point => `${point[1]},${point[0]},0`).join(' '); kmlContent += ` <Placemark> <name>${trailName} Route</name> <LineString> <coordinates>${coordinates}</coordinates> </LineString> </Placemark> `; } else { console.error('pointsData is not a polyline string:', pointsData); } }); }); // Add waypoints (trail markers) waypoints.forEach(waypoint => { kmlContent += ` <Placemark> <name>${waypoint.name}</name> <Point> <coordinates>${waypoint.location.longitude},${waypoint.location.latitude},0</coordinates> </Point> <description><![CDATA[${waypoint.description || ''}]]></description> </Placemark> `; }); kmlContent += `</Document>\n</kml>`; // Trigger KML download const blob = new Blob([kmlContent], { type: 'application/vnd.google-earth.kml+xml' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `${trailName}.kml`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); } // Initialize the script createDownloadButton(); })();
QingJ © 2025
镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址