eileen12 / Hi10MKVgrabber

// ==UserScript==
// @name         Hi10MKVgrabber
// @namespace    JDscripts
// @version      0.6
// @description  Adds a button on screen to show list of direct links to videos. Can be used with IDM to 'grab links on page'.
// @author       JD
// @match        http*://hi10anime.com/?p=*
// @updateURL    https://openuserjs.org/meta/eileen12/Hi10MKVgrabber.meta.js
// @licence      MIT
// @grant        none
// ==/UserScript==

var fileExt = [".mkv",".rar"];

fileExt.forEach(function(item,index){fileExt[index]=item.replace(/(?<!\\)\./g, "\\.");});
fileExt = "("+fileExt.join("|")+")";
var fileMatcher = "\\w{3,4}:\\/\\/[^:]+?" + fileExt + "(?!(\\.))"; //"http([^\\n]+?)\\(Hi10\\)([^\\n]+?)\\.(mkv|rar)";
var filemap = new Map();
var button = document.createElement("Button");
button.setAttribute("onclick", "#");
button.innerHTML = "Get Links";
button.style = "display: block; /* Hidden by default */\
  position: fixed; /* Fixed/sticky position */\
    bottom: 20px; /* Place the button at the bottom of the page */\
    right: 30px; /* Place the button 30px from the right */\
    z-index: 99; /* Make sure it does not overlap */\
    border: none; /* Remove borders */\
    outline: none; /* Remove outline */\
    background-color: red; /* Set a background color */\
    color: white; /* Text color */\
    cursor: pointer; /* Add a mouse pointer on hover */\
    padding: 15px; /* Some padding */\
    border-radius: 10px; /* Rounded corners */\
    font-size: 18px; /* Increase font size */";

var createFileMap = function () {
  // Get all .mkv files.
  document.body.innerHTML.match(new RegExp(fileMatcher, "g"))
    .forEach(function (link) {
      // Change to direct link, i.e. grab from the last pos. of :// till the end of file.
      // And convert to https if link starts with http.
      //link = link.replace(/.*?(?=(\w{3,4}(:\/\/(?!(.*:\/\/)))).*$)/g, "").replace(/^(http)/g, "https");
      link = link.replace(/.*(?=hi10anime\.)/i, "https://");
      // Group all links according to filenames.
      //var filename = link.match("\\(Hi10\\)([^\\n]+?)\\.mkv")[0];
      var filename = link.match(/[^/\\&\?]+\.\w+(?=([\?&].*$|$))/g);
      if (filename.length < 1) return;
      else filename = filename[0];
      // Initialise the file links array when new file found.
      if (!filemap.has(filename)) filemap.set(filename, encodeURI(link));
    });
  console.log(filemap);
  return filemap;
};

var getHTMLlistFromfileMap = function (filemap) {
  var htmlcode = "<h2>Grabbed MKV Links</h2><br />" + "<div><ul>";
  filemap.forEach(
    function (file, filename) {
      htmlcode += "<li><a id=" + filename + " href=" + file + ">" + filename + "</a></li>";
    }
  );
  htmlcode += "</ul></div>";
  return htmlcode;
};

customHTML = "";
var getCustomHTML = function () {
  if (customHTML === "" || typeof customHTML === "undefined") customHTML = getHTMLlistFromfileMap(filemap);
  return customHTML;
};
var source, mainscrollxy = [0, 0];
var swapMainHTML = function () {
  if (filemap.length < 1) {
    customHTML = "";
  }
  if (typeof source === "undefined" || source === "undefined") {
    source = document.body;
    html = getCustomHTML();
    document.body = document.createElement("BODY");
    document.body.innerHTML = html;
    document.body.appendChild(button);
    button.innerText = "Go Back";
    mainscrollxy[0] = document.scrollingElement.scrollLeft;
    mainscrollxy[1] = document.scrollingElement.scrollTop;
    window.scrollTo(0, 0);
  }
  else {
    document.body = source;
    document.body.appendChild(button);
    button.innerText = "View Links";
    source = "undefined";
    setTimeout(document.scrollingElement.scrollTo(mainscrollxy[0], mainscrollxy[1]), 500);
  }
};

var start = function () {
  console.log("Hi");
  createFileMap();
  button.addEventListener("click", swapMainHTML, false);
  document.body.appendChild(button);
};

// GreaseMonkey runs on DOMContentLoaded
start();