[Outdated] Get Into PC - Direct Download

[Outdated] Extract the direct file download links from the redirecting pages.

  1. // ==UserScript==
  2. // @name [Outdated] Get Into PC - Direct Download
  3. // @description [Outdated] Extract the direct file download links from the redirecting pages.
  4. // @namespace RainSlide
  5. // @author RainSlide
  6. // @icon https://getintopc.com/wp-content/uploads/Getintopc.png
  7. // @version 1.0.20200629.outdated
  8. // @match https://getintopc.com/*
  9. // @grant GM_xmlhttpRequest
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // @inject-into context
  13. // @run-at document-end
  14. // ==/UserScript==
  15.  
  16. "use strict";
  17.  
  18. document.querySelectorAll('body .post-content > form[action]').forEach(
  19. form => form.addEventListener(
  20. "submit", event => {
  21.  
  22. event.stopImmediatePropagation();
  23. event.preventDefault();
  24.  
  25. // //
  26.  
  27. form = this.form || event.target;
  28.  
  29. const data = new URLSearchParams( new FormData(form) ).toString();
  30.  
  31. // "URL" is for webpages, and "link" is for direct file download links
  32.  
  33. const currentURL = location.origin + location.pathname;
  34. const middleURL = form.action || location.origin + "/wait-for-access/";
  35. const finalURL = GM_getValue("finalURL") || (() => {
  36. const temp = "https://share-knowledgee.info/please-wait-file-will-download-automatically-2/";
  37. GM_setValue("finalURL", temp);
  38. return temp;
  39. })();
  40.  
  41. const xhr = ( url, referrer, success = null, failed = null ) =>
  42. GM_xmlhttpRequest({
  43. "url": url,
  44. "method": "POST",
  45. "responseType": "text",
  46. "anonymous": true,
  47. "headers": {
  48. "Content-Type" : "application/x-www-form-urlencoded",
  49. "Origin" : "https://getintopc.com",
  50. "Referrer" : referrer
  51. },
  52. "data": data,
  53. "onload": success,
  54. "onerror": failed,
  55. "ontimeout": failed
  56. });
  57.  
  58. const isString = something =>
  59. Object.prototype.toString.call(something) === "[object String]";
  60.  
  61. const sanitizeURL = rawURL => new URL(rawURL).toString();
  62.  
  63. // Call String.prototype.match(), and get a capturing group from the return value of it
  64. const getGroupfromMatch = ( str, regex, index = 1 ) => {
  65. if (
  66. isString(str) &&
  67. Object.prototype.toString.call(regex) === "[object RegExp]"
  68. ) {
  69. const matchResults = String.prototype.match.call(str, regex); // str.match(regex);
  70. return Array.isArray(matchResults)
  71. && isString(matchResults[index])
  72. && matchResults[index];
  73. } else {
  74. return false;
  75. }
  76. };
  77.  
  78. // Extract, sanitize and return the direct download link
  79. const getLink = response => {
  80. const rawLink = getGroupfromMatch( response, /"location.href = ('.+?');"/ );
  81. if ( rawLink ) {
  82. const link = JSON.parse(
  83. rawLink.replace( /"/g, '\\"' ).replace( /^'|'$/g, '"' )
  84. );
  85. return isString(link) && sanitizeURL(link);
  86. } else {
  87. return false;
  88. }
  89. }
  90.  
  91. // submit(): form.submit()
  92. const submit = () => form.submit();
  93.  
  94. // refreshFinalURL(): fetch the middle URL for a new final URL
  95. const refreshFinalURL = () => xhr(
  96. middleURL, currentURL, xhrEvent => {
  97. // 1. fetch success, try to get a new final URL from middle URL
  98. const newFinalURL = sanitizeURL(
  99. getGroupfromMatch(
  100. xhrEvent.responseText || xhrEvent.response,
  101. /<form id="gip_form" action="(.+?)" rel="nofollow" method="post">/
  102. )
  103. );
  104.  
  105. if ( newFinalURL && newFinalURL !== finalURL ) {
  106. // 1.1. the new final URL is valid and is not the old one, test it in action
  107. xhr(
  108. newFinalURL, middleURL, xhrEvent => {
  109. const newLink = getLink(xhrEvent.responseText || xhrEvent.response);
  110. if (newLink) {
  111. // 1.1.1. it works, store the final URL, navgate to the link, end
  112. GM_setValue("finalURL", newFinalURL);
  113. location.href = Link;
  114. return undefined;
  115. // 1.1.2. it doesn't work, submit(), end
  116. } else submit();
  117. }
  118. );
  119. // 1.2. can't find a usable new final URL, submit(), end
  120. } else submit();
  121. submit();
  122. // 2. fetch failed, submit(), end
  123. }, submit
  124. );
  125.  
  126. // Entry point: fetch the final URL
  127. xhr(
  128. finalURL, middleURL, xhrEvent => {
  129. // 1. fetch success, try to get the link from final URL
  130. const link = getLink(xhrEvent.responseText || xhrEvent.response);
  131. link
  132. ? location.assign(link) // 1.1. it works, just navgate to the link and end
  133. : refreshFinalURL(); // 1.2. something is wrong, refreshFinalURL()
  134. // 2. fetch failed, refreshFinalURL()
  135. }, refreshFinalURL
  136. );
  137.  
  138. // //
  139.  
  140. }
  141. )
  142. );