Xtube Download Button

Adds a download buttons to Xtube and Pornhub with a formatted link from the savedeo.com download service, or a direct link with resolution options on Xtube.

// ==UserScript==
// @name           Xtube Download Button
// @namespace      http://xtube.com
// @version        3.1.3
// @description    Adds a download buttons to Xtube and Pornhub with a formatted link from the savedeo.com download service, or a direct link with resolution options on Xtube.
// @author         persistentScripter
// @include        http*://www.xtube.com/*
// @include        http*://www.pornhub.com/*
// @require        http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
// @run-at         document-end
// ==/UserScript==
$(document).ready(function($) {
  $('a#downloadVideoBtn').remove();

  var linkPath = 'https://savedeo.com/download?url=' + encodeURIComponent(document.URL);
  if (window.location.origin === 'http://www.xtube.com' || window.location.origin === 'https://www.xtube.com') {
    setTimeout(function() {
      let sources;
      if (typeof playerConf !== 'undefined') {
        sources = playerConf;
      } else {
        let s1 = $('#playerWrapper > script:nth-child(3)')
          .text()
          .split('var playerConf =')[1];
        if (s1) {
          s1 = s1.split(',\n')[0];
        }
        if (!s1) {
          s1 = $('#playerWrapper > script:nth-child(4)')
            .text()
            .split('var playerConf =')[1];
          if (s1) {
            s1 = s1.split(',\n')[0];
          }
        }
        try {
          sources = JSON.parse(s1);
        } catch (e) {
          console.log(e);
          try {
            sources = JSON.parse(s1.substr(0, s1.length - 1));
          } catch (e) {
            console.log(e);
          }
        }
      }

      try {
        console.log(sources);
        if (!sources.mainRoll) {
          sources.mainRoll = sources.media;
        }
        let keys = Object.keys(sources.mainRoll.sources);
        for (let i = 0; i < keys.length; i++) {
          $(
            '<a id="downloadVideoFile" style="background-color: #E6E6E6;" href="' +
              sources.mainRoll.sources[keys[i]] +
              '" class="btn btn-outline bright" target="_blank">Download (' +
              keys[i].toString() +
              ')</a>'
          ).insertAfter('#mainSection > div > div > form');
        }
      } catch (e) {
        console.log(e);
      }
    }, 1000);
  } else if (window.location.origin === 'http://www.pornhub.com') {
    $('<div class="tab-menu-wrapper-cell xdl"><div class="tab-menu-item"><i class="main-sprite-dark-2"></i><span>Download File</span></div>').insertAfter('div.tab-menu-wrapper-row>div:nth-child(3)');
    $('.tab-menu-wrapper-cell.xdl').click(function(e) {
      e.preventDefault();
      window.open(linkPath);
    });
  }
});