Arxero

Untitled

Apr 17th, 2020
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var vd = {};
  2.  
  3. vd.sendMessage = function (message, callback) {
  4.     chrome.runtime.sendMessage(message, callback);
  5. };
  6.  
  7. vd.videoFormats = ['mp4', "mov", "flv", "webm"];
  8.  
  9. vd.escapeRegExp = function (str) {
  10.     return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
  11. };
  12.  
  13. vd.getVideoType = function (link) {
  14.     var videoType = null;
  15.     vd.videoFormats.some(function (format) {
  16.         if (new RegExp(vd.escapeRegExp('.' + format)).test(link)) {
  17.             videoType = format;
  18.             return true;
  19.         }
  20.     });
  21.     return videoType;
  22. };
  23.  
  24. vd.getAbsolutePath = function (link) {
  25.     console.log(link);
  26.     var protocol = window.location.protocol;
  27.     if (new RegExp("^" + protocol + "//").test(link)) {
  28.         return link;
  29.     }
  30.     if (new RegExp('^//').test(link)) {
  31.         return protocol + link;
  32.     }
  33.     var stack = window.location.href.split("/"),
  34.         parts = link.split("/");
  35.     stack.pop(); // remove current file name (or empty string)
  36.                  // (omit if "base" is the current folder without trailing slash)
  37.     for (var i = 0; i < parts.length; i++) {
  38.         if (parts[i] == ".")
  39.             continue;
  40.         if (parts[i] == "..")
  41.             stack.pop();
  42.         else
  43.             stack.push(parts[i]);
  44.     }
  45.     return stack.join("/");
  46. };
  47.  
  48. vd.getLinkTitleFromNode = function (node) {
  49.     var title = node.attr('title') ? node.attr('title') : node.attr('alt') ? node.attr('alt') : node.text().trim();
  50.     return title ? title : document.title;
  51. };
  52.  
  53. vd.getVideoLinks = function (node) {
  54.     console.log(node);
  55.     var videoLinks = [];
  56.     $(node).find('a').each(function () {
  57.         var link = $(this).attr('href');
  58.         var videoType = vd.getVideoType(link);
  59.         if (videoType) {
  60.             videoLinks.push({url: link, fileName: vd.getLinkTitleFromNode($(this)), extension: "." + videoType});
  61.         }
  62.     });
  63.     $(node).find('video').each(function () {
  64.         // console.log(this);
  65.         var nodes = [];
  66.         // console.log($(this).attr('src'));
  67.         $(this).attr('src') ? nodes.push($(this)) : void 0;
  68.         // console.log(nodes);
  69.         $(this).find('source').each(function () {
  70.             nodes.push($(this));
  71.         });
  72.         nodes.forEach(function (node) {
  73.             var link = node.attr('src');
  74.             if (!link) {
  75.                 return
  76.             }
  77.             var videoType = vd.getVideoType(link);
  78.             videoLinks.push({url: link, fileName: vd.getLinkTitleFromNode(node), extension: "." + videoType})
  79.         });
  80.     });
  81.     return videoLinks;
  82. };
  83.  
  84. vd.mutationObserver = new MutationObserver(function (mutations) {
  85.     mutations.forEach(function (mutation) {
  86.         var addedNodes = mutation.addedNodes;
  87.         addedNodes.forEach(function (node) {
  88.             // console.log(node);
  89.             vd.findVideoLinks(node);
  90.         });
  91.     });
  92. });
  93.  
  94. vd.sendVimeoVideoLinks = function () {
  95.     var videoLinks = [];
  96.     var dataUrl = $("div[data-config-url]").attr('data-config-url');
  97.     $.get(dataUrl, function (response) {
  98.         // console.log(response);
  99.         response = typeof response == 'string' ? JSON.parse(response) : response;
  100.         var title = response.video.title;
  101.         response.request.files.progressive.forEach(function (obj) {
  102.             videoLinks.push({url: obj.url, fileName: title, extension: "." + vd.getVideoType(obj.url)})
  103.         });
  104.         vd.sendVideoLinks(videoLinks);
  105.     });
  106. };
  107.  
  108. vd.sendVideoLinks = function (videoLinks) {
  109.     // if(videoLinks.length == 0) { return }
  110.     videoLinks.forEach(function (videoLink) {
  111.         videoLink.url = vd.getAbsolutePath(videoLink.url);
  112.     });
  113.     vd.sendMessage({message: 'add-video-links', videoLinks: videoLinks});
  114. };
  115.  
  116. vd.findVideoLinks = function (node) {
  117.     var videoLinks = [];
  118.     switch (window.location.host) {
  119.         case "vimeo.com":
  120.             vd.sendVimeoVideoLinks();
  121.             break;
  122.         case "www.youtube.com":
  123.             break;
  124.         default:
  125.             videoLinks = vd.getVideoLinks(node);
  126.     }
  127.     vd.sendVideoLinks(videoLinks);
  128.  
  129. };
  130.  
  131. vd.init = function () {
  132.     vd.findVideoLinks(document.body);
  133. };
  134.  
  135. vd.init();
  136.  
  137. chrome.extension.onMessage.addListener(function (request, sender, sendResponse) {
  138.     switch (request.message) {
  139.         case "":
  140.             break;
  141.     }
  142. });
Advertisement
Add Comment
Please, Sign In to add comment