canadaka / Trailforks Map Link for Strava

// ==UserScript==
// @name         Trailforks Map Link for Strava
// @namespace    https://www.trailforks.com/
// @version      0.4
// @description  Show link to view Strava activity on Trailforks map. Be sure to allow popups from Trailforks.com
// @icon         https://es.pinkbike.org/245/sprt/i/trailforks/trailforks_symbol.png
// @homepageURL  https://www.trailforks.com/
// @author       Trailforks
// @license      MIT
// @match        www.strava.com/activities/*
// @match        www.strava.com/dashboard*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    var $ = window.jQuery;

    function getActivityId() {
        return (_.isUndefined(window.pageView)) ? null : pageView.activity().id;
    }

    var divMsg = `<div style="text-align:right; background-color:#29691f; color:#fff;">
    <a href="https://www.trailforks.com/"><img src="//es.pinkbike.org/245/sprt/i/trailforks/trailforks_logo_plain_black.png" alt="Trailforks" style="width:100px; padding: 5px; float:left;"></a>
    <div id="trailforksmaplink" style="padding:5px; cursor: pointer;">View on Trailforks map</div>
    </div>
    `;
    $('#map-canvas').before(divMsg);
    $('#trailforksmaplink').on( "click", function() {
        event.preventDefault();
        var id = getActivityId();
        console.log(id);
        getActivityThenForward(id);

    });

    var divMsg2 = `<div class="trailforksmaplink" style="display:inline-block; background-color:#f5f5f5; color:#29691f; padding:2px 4px; cursor: pointer; font-size: 9px;"><img src="https://es.pinkbike.org/246/sprt/i/trailforks/favicon.ico" style="width: 15px;"> view on trailforks</div>`;
    $('.title-text').append(divMsg2);
    $('.trailforksmaplink').on( "click", function(event) {
        event.preventDefault();
        var tempID = $(this).parent().find('a').attr('href');
        var id = tempID.replace('/activities/', '');
        console.log(tempID, id);
        getActivityThenForward(id);

    });

    function getActivityThenForward(id) {
        var url = "/activities/" + id + "/streams?stream_types[]=latlng";
        $.ajax(url).done(function(jsonResponse) {
            $('body').append($('<form/>').attr({'action': 'https://www.trailforks.com/map/', 'method': 'post', 'id': 'trailforksmap', 'target': '_blank'})
            .append($('<input/>').attr({'type': 'hidden', 'name': 'track', 'value': JSON.stringify(jsonResponse)})))
            .find('#trailforksmap').submit().remove();
        });
    }

})();