Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name GPSies+
- // @namespace k2lab.org/gm/gpsies/
- // @description Add some features to GPSies.com (route annotation)
- // @author Kirill Kozlovskiy
- // @version 3.2
- // @downloadURL https://pastebin.com/raw/6TxYBihx
- // @match *://*.gpsies.com/map.do*
- // @match *://*.gpsies.com/editTrack.do*
- // @match *://*.gpsies.com/createTrack.do*
- // @match *://*.gpsies.com/mapOnly.do*
- // @match *://*.gpsies.com/mapUser.do*
- // @match *://*.gpsies.com/trackList.do*
- // @match *://*.gpsies.com/mapFolder.do*
- // @match *://*.gpsies.com/userList.do*
- // @match *://*.gpsies.com/upload.do*
- // @exclude file://*
- // @grant none
- // @require http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
- // ==/UserScript==
- /*
- This code is licenced under the GPL
- www.fsf.org/licensing/licenses/gpl.html
- */
- (function() {
- this.$ = this.jQuery = jQuery.noConflict(true);
- var LOG = function () {};
- LOG = (! "console" in window) ? function () {} : console.log;
- var __routeWaypoints = [ "Straight", "Left", "TSLL", "TSHL", "Right", "TSLR", "TSHR", "TU" ];
- var __routeWaypoints2RUS = { "Straight": "Прямо", "Left": "Налево", "TSLL": "Левее", "TSHL": "Резко налево", "Right": "Направо", "TSLR": "Правее", "TSHR": "Резко направо", "TU": "Разворот" };
- var __controlPointPrefix = "КП";
- function isRouteWaypoint(wpt) {
- for (var i = 0, j = __routeWaypoints.length; i < j; i++) {
- if (__routeWaypoints[i] == wpt.type) {
- return true;
- }
- }
- return false;
- }
- function isWptControlPoint(wptName) {
- return (wptName.indexOf(__controlPointPrefix) == 0)
- }
- function GPSiesRouteAnnotation() {
- var __result_odo_multiplier__ = 0.001;
- var __result_odo_round_to__ = 1;
- var __probe_radius__ = 500;
- var __start_hour__ = 8;
- function props(obj) {
- result = "";
- for (property in obj) {
- result += property + ':' + obj[property]+'; ';
- }
- return result;
- }
- function formatHours(someHours) {
- var hours = Math.floor(someHours)
- var minutes = Math.round(60 * (someHours - hours));
- var days = Math.floor(hours / 24);
- return (days > 0 ? (days+1)+"д."+(hours % 24) : hours ) + ':' + (minutes < 10 ? '0' : '') + minutes;
- }
- annotateRoute = function() {
- this.totalDistance = 0;
- advWaypoint = function(wpt) {
- // LOG("[GPSies+] adding wpt :", props(wpt));
- this.waypoint = wpt;
- this.distance = -1;
- this._latlng = new L.LatLng(wpt.lat, wpt.lon);
- this._closesPoint = null;
- this._closesDist = -1;
- this.checkDistance = function (latlng, odo = -1) {
- d = latlng.distanceTo(this._latlng);
- if (this._closesDist < 0 || d < this._closesDist) {
- this._closesDist = d;
- this._closesPoint = latlng;
- this.distance = odo;
- }
- }
- this.getLatLng = function() {
- return this._latlng;
- }
- return this;
- }
- function formatWaypoint(wpt) {
- var metricDistance = (wpt.distance * __result_odo_multiplier__)
- var metricTotalDistance = (totalDistance * __result_odo_multiplier__ )
- var distance = metricDistance.toFixed(__result_odo_round_to__);
- var ret;
- // 15-30 км/час для дистанций до 600 км и 13,5-30 км/час для дистанций от 600 до 1000 км
- // TODO: Magic Numbers !!!
- var minSpeed = 15;
- if (metricTotalDistance > 650) {
- minSpeed = 13.3;
- }
- // Оч. странный алгоритм. Суть - регрессия максимальной скорости от 35 на 0 до 30 на 1050
- var maxSpeed = 30 + 5 * (1050 - metricDistance) / 1050;
- ret = "[" + distance + "] " + wpt.waypoint.name;
- ret += ( isWptControlPoint(wpt.waypoint.name) ? " {"
- + (formatHours(__start_hour__ + (metricDistance / maxSpeed)))
- + "-"
- + (formatHours(__start_hour__ + (metricDistance / minSpeed))) +"}"
- : "");
- ret += (wpt.waypoint.desc.length > 0 ? ": " + wpt.waypoint.desc : "" );
- LOG("ret="+ret);
- return ret;
- }
- // Create array of waypoints to operate
- var _wpa = waypointsArray;
- var wayPoints = [];
- if (_wpa.size() <= 0) {
- LOG("[GPSies+] GPSiesRouteAnnotation - Nothing to do: no waypoints found!");
- return;
- }
- for (var wi=0; wi < _wpa.size(); wi++) {
- wayPoints.push( new this.advWaypoint(_wpa[wi]) );
- }
- // LOG("[GPSies+] wpa= ", props(wpa));
- // Let's get the closes point to each WPT and calculate some distance
- var polylineLL = polyline.getLatLngs();
- this.totalDistance = 0;
- var currPnt = null;
- var prevPnt = null;
- if (polylineLL.size() > 0) {
- currPnt = polylineLL[0];
- prevPnt = polylineLL[0];
- }
- for (var i=0; i < polylineLL.size(); i++) {
- currPnt = polylineLL[i];
- for (var wi=0; wi < wayPoints.length; wi++) {
- // // Chech for the nearest point
- // wpa[wi].checkDistance(currPnt, this.totalDistance);
- // Chech for the nearest projection
- var wpaPnt = wayPoints[wi].getLatLng();
- var projPnt = map.unproject ( L.LineUtil.closestPointOnSegment(
- map.project(wpaPnt),
- map.project(prevPnt),
- map.project(currPnt)
- ));
- var radius = wpaPnt.distanceTo(projPnt);
- if (radius <= __probe_radius__) {
- // LOG("Got it! i=",i,"; wi=",wi,"; totalDistance=", this.totalDistance + "; D=" + (this.totalDistance + prevPnt.distanceTo(projPnt)));
- wayPoints[wi].checkDistance(projPnt, (0.0 + this.totalDistance + prevPnt.distanceTo(projPnt)));
- }
- }
- this.totalDistance += currPnt.distanceTo(prevPnt);
- prevPnt = currPnt;
- }
- // Sorting by distance from start point
- wayPoints.sort(function(a, b) {
- return a.distance - b.distance;
- });
- result = [];
- for (var wi=0; wi < wayPoints.length; wi++) {
- // DEBUG
- // map.addLayer( new L.Marker(wpa[wi]._closesPoint, { title: wpa[wi].desc }) );
- if (wayPoints[wi].distance >= 0 ) {
- result.push( formatWaypoint(wayPoints[wi]) );
- }
- }
- return result;
- }
- updateAnnotation = function () {
- LOG("[GPSies+] updating annotation...");
- __result_odo_multiplier__ = parseFloat($("#odo_multiplier").val());
- __result_odo_round_to__ = parseFloat($("#odo_round_to").val());
- __probe_radius__ = parseFloat($("#probe_radius").val());
- __start_hour__ = parseInt($("#start_hour").val(), 10);;
- LOG("__result_odo_multiplier__ "+ __result_odo_multiplier__);
- LOG("__probe_radius__ " + __probe_radius__);
- $("#routeAnnotation").empty();
- $("#routeAnnotation").append('<p>'+this.annotateRoute().join('<br />')+'</p>');
- return false;
- }
- updateCUE = function () {
- LOG("[GPSies+] updating CUE...");
- for (var i = 0, j = waypointsArray.length; i < j; i++) {
- wpt = waypointsArray[i] ;
- if (isRouteWaypoint(wpt)) {
- wpt.name = __routeWaypoints2RUS[wpt.type];
- if (map.hasLayer(wpt.marker)) {
- map.removeLayer(wpt.marker);
- map.addLayer(wpt.marker);
- }
- }
- }
- return false;
- }
- updateInterface = function () {
- if ($("#contentboxRouteAnnotation").length > 0) {
- LOG("[GPSies+] GPSiesRouteAnnotation WTF?! Got old interface. Are you cheating me?");
- $("#contentboxRouteAnnotation").remove();
- }
- // Check for the page elements
- if ($("#contentboxFlyoutChart").length > 0 ) {
- LOG("[GPSies+] map edit on editTrack.do detected!");
- $("#contentboxFlyoutChart").after(
- '<div id="contentboxRouteAnnotation" class="panel panel-default panel-form"> \
- <div class="panel-heading"> \
- <a data-toggle="collapse" data-parent="#accordion" href="#routeAnnotationShow" class="collapsed">Route Annotation <small>(GPSies+)</small> \
- <i class="fa fa-caret-down pull-right"></i><i class="fa fa-caret-up pull-right"></i> \
- </a> \
- </div> \
- <div id="routeAnnotationShow" class="panel-collapse collapse"> \
- <div class="panel-body" id="routeAnnotationDiv"></div> \
- </div> \
- </div>');
- // $("#routeAnnotationShow").before('');
- } else if ($("#recommendFriend").length > 0 ) {
- LOG("[GPSies+] map view on map.do detected!");
- $("#recommendFriend").after(
- '<div id="contentboxRouteAnnotation" class="panel panel-default panel-form"> \
- <div class="panel-heading"> \
- <a data-toggle="collapse" data-parent="#accordion" href="#routeAnnotationShow" class="collapsed">Route Annotation <small>(GPSies+)</small> \
- <i class="fa fa-caret-down pull-right"></i><i class="fa fa-caret-up pull-right"></i> \
- </a> \
- </div> \
- <div id="routeAnnotationShow" class="panel-collapse collapse"> \
- <div class="panel-body" id="routeAnnotationDiv"></div> \
- </div> \
- </div>');
- }
- if (! $("#routeAnnotationDiv").length > 0 ) {
- LOG("[GPSies+] nothing to do - no known elements found!");
- return;
- }
- $("#routeAnnotationDiv").append(
- '<div class="row"> \
- <div class="col-sm-3"><div class="form-group"> \
- <label for="start_hour">Starting hour</label> \
- <input id="start_hour" class="form-control" type="text" onchange="" value="' + __start_hour__ + '" name="start_hour"> \
- </div></div> \
- <div class="col-sm-3"><div class="form-group"> \
- <label for="odo_multiplier">ODO Multiplier</label> \
- <input id="odo_multiplier" class="form-control" type="text" onchange="" value="' + __result_odo_multiplier__ + '" name="odo_multiplier"> \
- </div></div> \
- <div class="col-sm-3"><div class="form-group"> \
- <label for="odo_round_to">ODO Round</label> \
- <input id="odo_round_to" class="form-control" type="text" onchange="" value="' + __result_odo_round_to__ + '" name="odo_round_to"> \
- </div></div> \
- <div class="col-sm-3"><div class="form-group"> \
- <label for="probe_radius">Probe radius, m</label> \
- <input id="probe_radius" class="form-control" type="text" onchange="" value="' + __probe_radius__ + '" name="probe_radius"> \
- </div></div> \
- </div> \
- ');
- $("#routeAnnotationDiv").append('<div class="row"><div class="col-sm-6"> \
- <ul class="list-inline"> \
- <li><a id="runUpdateAnnotation" class="btn btn-default" onclick="return false;" href="#">Create/Update Annotation</a></li> \
- <li><a id="runUpdateCUE" class="btn btn-default" onclick="return false;" href="#">Simplify CUE</a></li> \
- </ul> \
- </div></div> \
- ' );
- $("#runUpdateAnnotation").click(function() {
- updateAnnotation();
- return false;
- });
- $("#runUpdateCUE").click(function() {
- updateCUE();
- return false;
- });
- $("#routeAnnotationDiv").append('<div class="row"><div class="col-sm-12"><b>Annotation:</b><br /><br /> <div id="routeAnnotation"><i>Nothing yet...</i></div> </div></div>');
- }
- this.run = function() {
- // LOG ("[GPSies+] GPSiesRouteAnnotation.run");
- updateInterface();
- };
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////
- function GPSiesWaypointToggler() {
- toggleRouteWaypoints = function () {
- LOG("[GPSies+] toggleRouteWaypoints ()");
- if (isWaypoints()) {
- for (var i = 0, j = waypointsArray.length; i < j; i++) {
- if (isRouteWaypoint(waypointsArray[i])) {
- if (map.hasLayer(waypointsArray[i].marker)) {
- map.removeLayer(waypointsArray[i].marker)
- } else if (!map.hasLayer(waypointsArray[i].marker)) {
- map.addLayer(waypointsArray[i].marker)
- }
- }
- }
- } else {
- LOG("[GPSies+] No waypoints found to toggle!");
- }
- };
- toggleNonControlPoints = function () {
- LOG("[GPSies+] toggleNonControlPoints ()");
- if (isWaypoints()) {
- for (var i = 0, j = waypointsArray.length; i < j; i++) {
- // LOG(props(waypointsArray[i]));
- if (! isWptControlPoint(waypointsArray[i].name)) {
- if (map.hasLayer(waypointsArray[i].marker)) {
- map.removeLayer(waypointsArray[i].marker)
- } else if (!map.hasLayer(waypointsArray[i].marker)) {
- map.addLayer(waypointsArray[i].marker)
- }
- }
- }
- } else {
- LOG("[GPSies+] No waypoints found to toggle!");
- }
- };
- updateInterface = function () {
- if ($("#wptTogglerPane").length > 0) {
- LOG("[GPSies+] GPSiesWaypointToggler - WTF?! Got old interface. Are you cheating me?");
- $("#wptTogglerPane").remove();
- }
- // Check for the navigation pane
- if ($("#navMore").length > 0 ) {
- LOG("[GPSies+] map view detected!");
- $("li div input#waypointToggle").parent().parent()
- .after('<div id="wptTogglerPane" style="display: block;"></div>');
- }
- // if (! $("div#wptTogglerPane").length > 0 ) {
- // LOG("[GPSies+] GPSiesWaypointToggler - nothing to do - no known elements found!");
- // return;
- // }
- $("div#wptTogglerPane").append('<label style="vertical-align: text-bottom; cursor: pointer;" id="runToggleRouteWaypoints" onclick="return false;" href="#"> Toggle Route</label>' ).append('<label style="vertical-align: text-bottom; cursor: pointer;" id="runToggleNonControlPoints" onclick="return false;" href="#"> Toggle non-CP</label>' );
- $("#runToggleRouteWaypoints").click(function() {
- toggleRouteWaypoints();
- return false;
- });
- $("#runToggleNonControlPoints").click(function() {
- toggleNonControlPoints();
- return false;
- });
- };
- this.run = function() {
- // LOG ("[GPSies+] GPSiesWaypointToggler.run");
- updateInterface();
- };
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////
- function GPSiesUpdateList() {
- updateInterface = function () {
- // Check for the page elements
- if ($("#displayTable").length > 0 ) {
- LOG("[GPSies+] map list detected!");
- $("h5 a span").each(function (index) { $( this ).text(this.title.replace(/^\S+\s/,""));} );
- } else {
- LOG("[GPSies+] GPSiesUpdateList nothing to do - no known elements found!");
- return;
- }
- }
- this.run = function() {
- // LOG ("[GPSies+] GPSiesUpdateList.run");
- updateInterface();
- };
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////
- function GPSiesUploadDefaults() {
- updateInterface = function () {
- // Check for the page elements
- if ($("#uploadForm").length > 0 && /\/upload.do$/.test($("link[rel='canonical']").prop('href')) ) {
- LOG("[GPSies+] upload form detected!");
- $("input#mountainbiking").prop('checked', true);
- $("input#gpsRecorded").prop('checked', true);
- $("input#flat").prop('checked', true);
- $("input#trackCombine").prop('checked', true);
- //<option value="78821">4th-k.2016 Кубанские Четвергушки</option>
- $("select#userFolder").val("78821");
- //<option value="78821">4th-k.2017 Кубанские Четвергушки</option>
- // $("select#userFolder").val("78855");
- $("input#websiteUrl").val("http://kirill-k2.blogspot.com/search/label/velo");
- $("input[name='formFile']").change(function (){
- var fileName = $(this).val();
- var nameField = $("input[name='filename']");
- if ( nameField && !nameField.val()) {
- // cut file type and OruxMaps date suffix
- // i.e. all like '__20160402_1242.gpx'
- nameField.val(fileName.replace( /(__[\d_]{11,13})?\.\w{3,4}$/, "" ));
- }
- LOG(fileName + "!"+nameField);
- });
- // Scroll down if no error
- if ( $("ul.error:visible").length == 0) {
- $("html, body").scrollTop($(document).height() - $(window).height());
- // Auto open file dialog
- $("input[name='formFile']").focus().trigger('click');
- }
- } else if ($("div#mainBox").length > 0 && $("a#directLink").length == 0) {
- LOG("[GPSies+] map view detected!");
- if ( /\/(upload.do|editTrack.do)$/.test($("link[rel='canonical']").prop('href')) ) {
- var fileURL = 'http://www.gpsies.com/map.do?fileId='+$("form#downloadForm input#fileId").val();
- // window.location.replace(fileURL);
- window.history.pushState("","", fileURL);
- // $("div#mainBox").prepend('<a id="directLink" href="'+fileURL+'">'+fileURL+'</a><br />');
- }
- } else {
- LOG("[GPSies+] GPSiesUploadDefaults nothing to do - no known elements found!");
- return;
- }
- }
- this.run = function() {
- // LOG ("[GPSies+] GPSiesUploadDefaults.run");
- updateInterface();
- };
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////
- loadall = function () {
- new GPSiesRouteAnnotation().run();
- new GPSiesWaypointToggler().run();
- new GPSiesUpdateList().run();
- new GPSiesUploadDefaults().run();
- };
- LOG ("[GPSies+] is loading... JQuery info: ", $().jquery );
- $(document).ready( loadall );
- $(window).load( loadall );
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement