Himado, Display video url

ひまわり動画のソースURLを表示

// ==UserScript==
// @name        Himado, Display video url
// @description ひまわり動画のソースURLを表示
// @namespace   org.userscript.bunnelby
// @match       http://himado.in/*
// @match       https://web.archive.org/web/*/http://himado.in/*
// @version     1.5
// @run-at      document-end
// ==/UserScript==

(function () {
    "use strict";
    var isArchiveOrg = (location.host == "web.archive.org");

    var escapeHtml = function () {
        var specialChars = {
            '&': '&',
            "'": ''',
            '`': '`',
            '"': '"',
            '<': '&lt;',
            '>': '&gt;',
        };
        var regex = /[&'`"<>]/g;

        return t => t.replace(regex, m => specialChars[m]);
    }();

    function InHimado() {
		function appendSrc (url, label, target) {
            var httpIndex = url.lastIndexOf("https://");
            url = httpIndex >= 0 ? url.substr(httpIndex) : "url is wrong";
			console.log(url);
			target.insertAdjacentHTML("beforeend", `<div><a href="${url}" download="${escapeHtml(document.title)}.mp4">${label}<br />${url}</a></div>`);
		}

		var sources = ary_spare_sources.spare;
		if (!sources) {
            var $description = document.querySelector("#link_disablemessag_own, #author_comment");
            if ($description) {
                $description.insertAdjacentHTML("beforeend", `<a href="https://web.archive.org/web/*/${location.href}">Arcive</a>`);
            }
            return;
        }

		var $links = document.querySelector("a[href*='javascript:showMovieUrl()']").parentNode;
		$links.innerHTML = "";

		var $options = $("#select_othersource option");

		// ソースが一つの場合、SELECT要素がない
		if ($options.length == 0) {
			var url = decodeURIComponent(movie_url || display_movie_url);
			var label = "★ ";
			appendSrc(url, label, $links);
			return;
		}

		$options.each(function (index) {
			var source = sources.find(function (element, sourceIndex, array) {
				return index == element.lid;
			});

            var url, label;
			// ソースがデフォルトの場合、OPTIONが選択されてない、スペアソースにもない
			if ($(this).is("[selected]") || !source) {
				url = decodeURIComponent(movie_url || display_movie_url);
				label = "★" + this.label;
			} else {
				url = decodeURIComponent(source.src);
				label = this.label
			}
			appendSrc(url, label, $links);
		});

        $("#movie_title").click(function () {
            var $title = $("#movie_title");
            var selection = getSelection();
            var range = document.createRange();
            range.setStartBefore($title.first()[0]);
            range.setEndAfter($title.last()[0]);
            selection.removeAllRanges();
            selection.addRange(range);
        });
    }

	addEventListener("load", function () {
		console.log("enable display url");
        InHimado();
	});
})();