gazza911 / TVmaze - Link Externals

// ==UserScript==
// @name         TVmaze - Link Externals
// @namespace    TVmaze
// @version      1.12
// @description  Add a link to externals (format given for thetvdb & imdb)
// @author       gazza911
// @match        https://www.tvmaze.com/showexternal/view*
// @updateURL https://openuserjs.org/meta/gazza911/TVmaze_-_Link_Externals.meta.js
// @grant        GM_getValue
// @grant        GM_setValue
// @license      MIT
// ==/UserScript==

$(`<style>
    .permalinkEdit {
        margin-left: 5px;
    }
    .permalinkEdit:hover {
        color: #3C948B;
        cursor: pointer;
    }
</style>`).appendTo("head");

const thisVersion = "1.12";

var permalinks = GM_getValue("tvmaze_externalFormats", {}),
    defaults = {
        Thetvdb: "https://thetvdb.com/?tab=series&id={Thetvdb-id}",
        Imdb: "http://www.imdb.com/title/{Imdb-id}/"
    },
    version = GM_getValue("tvmaze_externals_version", "");

if(Object.keys(permalinks).length === 0) {
    permalinks = Object.assign({}, defaults);
}

if(version.length > 0 && version !== thisVersion) {
    reset("TVmaze - Link Externals has been updated, would you like to reset the formats?");
}

GM_setValue("tvmaze_externals_version", thisVersion);

$(`<div class="row hide-for-print">
    <div class="small-12-columns columns">
        <button class="alert small button round permalinkReset">
            <i class="fa fa-trash fa-lg" alt="Reset Permalinks"></i>
            <span class="show-for-medium">Reset Permalinks</span>
        </button>
    </div>
</div>`).insertAfter('#site-header ~ div:first');

$("table.detail-view tr th").each(function(row) {
    $(this).append('<i class="fas fa-edit permalinkEdit hide-for-print" alt="Edit" data-name="' + $(this).text().replace(" ", "_") + '"></i>');
});

setLinks();

$(".permalinkEdit").click(edit);
$(".permalinkReset").click(reset);

function setLinks() {
    $("table.detail-view tr").each(function(row) {
        var $type = $(this).children("th"),
            name = $type.text().replace(" ", "_"),
            permalink = permalinks[name];
        if (permalink) {
            var $element = $(this).children("td"),
                text = $element.text();
            if(text !== "(not set)") {
                $element.html("<a href='" + permalink.replace("{" + name + "-id}", text) + "' target='_blank'>" + text + "</a>");
            }
        }

    });
}

function edit() {
    var type = $(this).data("name");
    var permalink = permalinks[type];
    var pattern = prompt("Please enter the format, use {" + type + "-id} as a placeholder for the ID", permalink);
    if (pattern !== null) {
        permalinks[type] = pattern;
        GM_setValue("tvmaze_externalFormats", permalinks);
        setLinks();
    }
}

function reset(message) {
    var perform = confirm(message.length > 0 ? message : "Are you sure that you want to reset the permalink formats? You will loose any custom formats");
    if (perform) {
        permalinks = Object.assign({}, defaults);
        GM_setValue("tvmaze_externalFormats", permalinks);
        setLinks();
    }
}