Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.86 KB | None | 0 0
  1. window.CustomYoutube = window.CustomYoutube || {};
  2.  
  3. CustomYoutube = (function() {
  4. const selectors = {
  5. RecoverHeading: '#RecoverHeading',
  6. RecoverEmail: '#RecoverEmail',
  7. LoginHeading: '#LoginHeading'
  8. };
  9.  
  10. function _isSearchPage(){
  11. return /search/.test(window.location.href);
  12. }
  13.  
  14. function _JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) {
  15. //If JSONData is not an object then JSON.parse will parse the JSON string in an Object
  16. var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
  17.  
  18. var CSV = '';
  19. //Set Report title in first row or line
  20.  
  21. CSV += ReportTitle + '\r\n\n';
  22.  
  23. //This condition will generate the Label/Header
  24. if (ShowLabel) {
  25. var row = "";
  26.  
  27. //This loop will extract the label from 1st index of on array
  28. for (var index in arrData[0]) {
  29.  
  30. //Now convert each value to string and comma-seprated
  31. row += index + ',';
  32. }
  33.  
  34. row = row.slice(0, -1);
  35.  
  36. //append Label row with line break
  37. CSV += row + '\r\n';
  38. }
  39.  
  40. //1st loop is to extract each row
  41. for (var i = 0; i < arrData.length; i++) {
  42. var row = "";
  43.  
  44. //2nd loop will extract each column and convert it in string comma-seprated
  45. for (var index in arrData[i]) {
  46. row += '"' + arrData[i][index] + '",';
  47. }
  48.  
  49. row.slice(0, row.length - 1);
  50.  
  51. //add a line break after each row
  52. CSV += row + '\r\n';
  53. }
  54.  
  55. if (CSV == '') {
  56. alert("Invalid data");
  57. return;
  58. }
  59.  
  60. //Generate a file name
  61. var fileName = "MyReport_";
  62. //this will remove the blank-spaces from the title and replace it with an underscore
  63. fileName += ReportTitle.replace(/ /g,"_");
  64.  
  65. //Initialize file format you want csv or xls
  66. var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);
  67.  
  68. // Now the little tricky part.
  69. // you can use either>> window.open(uri);
  70. // but this will not work in some browsers
  71. // or you will not get the correct file extension
  72.  
  73. //this trick will generate a temp <a /> tag
  74. var link = document.createElement("a");
  75. link.href = uri;
  76.  
  77. //set the visibility hidden so it will not effect on your web-layout
  78. link.style = "visibility:hidden";
  79. link.download = fileName + ".csv";
  80.  
  81. //this part will append the anchor tag and remove it after automatic click
  82. document.body.appendChild(link);
  83. link.click();
  84. document.body.removeChild(link);
  85. }
  86.  
  87. function copyToClipboard(text) {
  88. if (window.clipboardData && window.clipboardData.setData) {
  89. // IE specific code path to prevent textarea being shown while dialog is visible.
  90. return clipboardData.setData("Text", text);
  91.  
  92. } else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
  93. var textarea = document.createElement("textarea");
  94. textarea.textContent = text;
  95. textarea.style.position = "fixed"; // Prevent scrolling to bottom of page in MS Edge.
  96. document.body.appendChild(textarea);
  97. textarea.select();
  98. try {
  99. return document.execCommand("copy"); // Security exception may be thrown by some browsers.
  100. } catch (ex) {
  101. console.warn("Copy to clipboard failed.", ex);
  102. return false;
  103. } finally {
  104. document.body.removeChild(textarea);
  105. }
  106. }
  107. }
  108.  
  109. function numberToEnglish( n ) {
  110.  
  111. var string = n.toString(), units, tens, scales, start, end, chunks, chunksLen, chunk, ints, i, word, words, and = 'and';
  112.  
  113. /* Remove spaces and commas */
  114. string = string.replace(/[, ]/g,"");
  115.  
  116. /* Is number zero? */
  117. if( parseInt( string ) === 0 ) {
  118. return 'zero';
  119. }
  120.  
  121. /* Array of units as words */
  122. units = [ '', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' ];
  123.  
  124. /* Array of tens as words */
  125. tens = [ '', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety' ];
  126.  
  127. /* Array of scales as words */
  128. scales = [ '', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quatttuor-decillion', 'quindecillion', 'sexdecillion', 'septen-decillion', 'octodecillion', 'novemdecillion', 'vigintillion', 'centillion' ];
  129.  
  130. /* Split user arguemnt into 3 digit chunks from right to left */
  131. start = string.length;
  132. chunks = [];
  133. while( start > 0 ) {
  134. end = start;
  135. chunks.push( string.slice( ( start = Math.max( 0, start - 3 ) ), end ) );
  136. }
  137.  
  138. /* Check if function has enough scale words to be able to stringify the user argument */
  139. chunksLen = chunks.length;
  140. if( chunksLen > scales.length ) {
  141. return '';
  142. }
  143.  
  144. /* Stringify each integer in each chunk */
  145. words = [];
  146. for( i = 0; i < chunksLen; i++ ) {
  147.  
  148. chunk = parseInt( chunks[i] );
  149.  
  150. if( chunk ) {
  151.  
  152. /* Split chunk into array of individual integers */
  153. ints = chunks[i].split( '' ).reverse().map( parseFloat );
  154.  
  155. /* If tens integer is 1, i.e. 10, then add 10 to units integer */
  156. if( ints[1] === 1 ) {
  157. ints[0] += 10;
  158. }
  159.  
  160. /* Add scale word if chunk is not zero and array item exists */
  161. if( ( word = scales[i] ) ) {
  162. words.push( word );
  163. }
  164.  
  165. /* Add unit word if array item exists */
  166. if( ( word = units[ ints[0] ] ) ) {
  167. words.push( word );
  168. }
  169.  
  170. /* Add tens word if array item exists */
  171. if( ( word = tens[ ints[1] ] ) ) {
  172. words.push( word );
  173. }
  174.  
  175. /* Add 'and' string after units or tens integer if: */
  176. if( ints[0] || ints[1] ) {
  177.  
  178. /* Chunk has a hundreds integer or chunk is the first of multiple chunks */
  179. if( ints[2] || (i + 1) > chunksLen ) {
  180. words.push( and );
  181. }
  182.  
  183.  
  184. }
  185.  
  186. /* Add hundreds word if array item exists */
  187. if( ( word = units[ ints[2] ] ) ) {
  188. words.push( word + ' hundred' );
  189. }
  190.  
  191. }
  192.  
  193. }
  194.  
  195. return words.reverse().join( ' ' );
  196.  
  197. }
  198.  
  199. String.prototype.replaceAll = function(obj) {
  200. let finalString = '';
  201. let word = this;
  202. for (let each of word){
  203. for (const o in obj){
  204. const value = obj[o];
  205. if (each == o){
  206. each = value;
  207. }
  208. }
  209. finalString += each;
  210. }
  211.  
  212. return finalString;
  213. };
  214.  
  215. function getAboutLinkAtSearchPage(){
  216. var users = document.querySelectorAll('#main-link[href]');
  217. var getAboutPage = (url) => `https://www.youtube.com${url}/about`
  218. var aboutPages = [];
  219.  
  220. for (let each of users){
  221. let url = each.getAttribute('href');
  222. let subs = each.querySelector('#subscribers').textContent;
  223. let channelName = each.querySelector('#text.ytd-channel-name').textContent;
  224. let subsEnglish;
  225.  
  226. const formatUrl = (() => {
  227. url = getAboutPage(url);
  228. })();
  229.  
  230. const formatChannelName = (() => {
  231. channelName = channelName.trim();
  232. })();
  233.  
  234. const formatSubs = (() => {
  235. subs = subs.replace('mil','000');
  236. subs = subs.replaceAll({'M':'0000', ',':'', ' ':''});
  237. subs = subs.replace(/\D/g,'');
  238. })();
  239.  
  240. const formatSubsEnglish = (() => {
  241. subsEnglish = numberToEnglish(subs);
  242. })();
  243.  
  244. aboutPages.push({
  245. channelName,
  246. url,
  247. subs,
  248. subsEnglish
  249. });
  250. }
  251.  
  252. console.table(aboutPages);
  253. copyToClipboard([...aboutPages].map(e=>e.url).join('\n'));
  254.  
  255. }
  256.  
  257.  
  258.  
  259.  
  260. return {
  261. init: function() {
  262. document.addEventListener("DOMContentLoaded", function() {
  263.  
  264. if (_isSearchPage()){
  265.  
  266. }
  267.  
  268. });
  269. },
  270. getAboutLinkAtSearchPage
  271. };
  272. })();
  273.  
  274.  
  275. CustomYoutube.init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement