Edna.cz Subtitle Downloader

Přidává možnost stáhnout titulky nebo přejít na původní YT video

  1. // ==UserScript==
  2. // @author Setcher
  3. // @name Edna.cz Subtitle Downloader
  4. // @name:cs Edna.cz Stahovač Titulků
  5. // @namespace https://greasyfork.org/users/30331-setcher
  6. // @description Přidává možnost stáhnout titulky nebo přejít na původní YT video
  7. // @description:cs Gives you the opportunity to download the subtitles or go directly to the original YT video
  8. // @include http*://*edna.cz/*
  9. // @version 0.9.3
  10. // @grant GM_addStyle
  11. // ==/UserScript==
  12.  
  13. function saveFile(url) {
  14. // Get file name from url.
  15. var filename = url.substring(url.lastIndexOf("/") + 1).split("?")[0];
  16. var xhr = new XMLHttpRequest();
  17. xhr.responseType = 'blob';
  18. xhr.onload = function() {
  19. var a = document.createElement('a');
  20. a.href = window.URL.createObjectURL(xhr.response); // xhr.response is a blob
  21. a.download = filename; // Set the file name.
  22. a.style.display = 'none';
  23. document.body.appendChild(a);
  24. a.click();
  25. delete a;
  26. };
  27. xhr.open('GET', url);
  28. xhr.send();
  29. }
  30.  
  31. var jwplayers = document.getElementsByClassName('video-box');
  32. if (jwplayers.length > 0) {
  33. for (i = 0; i < jwplayers.length; i++){
  34. if (jwplayers[i].childNodes[0].wholeText.length <= 1) {
  35. var ytRegexp = /video_id:\s"([^"]+)/gi;
  36. var yt = ytRegexp.exec(jwplayers[i].innerHTML);
  37. var youtube = yt[1].replace(/\\/g, "");
  38.  
  39. var srtRegexp = /subtitles:\s*?"([^"]+)/gi;
  40. var srt = srtRegexp.exec(jwplayers[i].innerHTML);
  41. var result = srt[1].replace(/\\/g, "");
  42.  
  43. } else {
  44. var dataSource = jwplayers[i].childNodes[0];
  45. var result = dataSource.getAttribute('data-subtitles');
  46. result = result.split("/").pop().replace(/\..+$/, '.srt')
  47. result = 'http://www.edna.cz/runtime/userfiles/'+result;
  48. var youtube = dataSource.getAttribute('data-video');
  49. }
  50.  
  51. var zNode = document.createElement ('span');
  52. zNode.setAttribute ('style', 'display: block; margin: 20px 0; text-align: center;');
  53. zNode.setAttribute ('id', 'downloadBlock');
  54.  
  55. var srt = document.createElement ('a');
  56. if (result) {
  57. srt.innerHTML = 'Stáhnout titulky';
  58. var filename = result.substring(result.lastIndexOf("/") + 1).split("?")[0];
  59. srt.setAttribute ('download', filename);
  60. srt.setAttribute ('id', 'downloadSRTButton');
  61. srt.setAttribute ('href', result);
  62. //srt.setAttribute ('href', 'javascript:saveFile('+result+')');
  63. } else {
  64. srt.innerHTML = 'Titulky nenalezeny';
  65. srt.setAttribute ('id', 'failedButton');
  66. }
  67. zNode.innerHTML += srt.outerHTML;
  68.  
  69. var yt = document.createElement ('a');
  70. if (youtube) {
  71. yt.innerHTML = 'Přejít na YouTube';
  72. yt.setAttribute ('id', 'downloadSRTButton');
  73. yt.setAttribute ('href', youtube);
  74. } else {
  75. yt.innerHTML = 'YT link nenalezen';
  76. yt.setAttribute ('id', 'failedButton');
  77. }
  78. zNode.innerHTML += yt.outerHTML;
  79. /*
  80. if (jwplayers[i].nextSibling == null || !jwplayers[i].nextSibling.getAttribute('id') == 'downloadBlock')
  81. {
  82. jwplayers[i].parentNode.insertBefore(zNode, jwplayers[i].nextSibling);
  83. }
  84. */
  85. jwplayers[i].parentNode.insertBefore(zNode, jwplayers[i].nextSibling);
  86. }
  87. } else {
  88. // regex way
  89.  
  90. }
  91.  
  92. //--- Style our newly added elements using CSS.
  93. GM_addStyle ( multilineStr ( function () {/*!
  94. #downloadSRTButton {
  95. font-weight: bold;
  96. letter-spacing: 1px;
  97. margin: 5px;
  98. opacity: 0.9;
  99. z-index: 222;
  100. width: 150px;
  101. padding: 5px 20px;
  102. }
  103. #failedButton {
  104. font-size: 12px;
  105. font-weight: bold;
  106. color: black;
  107. letter-spacing: 1px;
  108. background: orange;
  109. border: 3px outset black;
  110. margin: 5px;
  111. opacity: 0.9;
  112. z-index: 222;
  113. width: 150px;
  114. padding: 5px 20px;
  115. }
  116. */} ) );
  117.  
  118. function multilineStr (dummyFunc) {
  119. var str = dummyFunc.toString ();
  120. str = str.replace (/^[^\/]+\/\*!?/, '') // Strip function () { /*!
  121. .replace (/\s*\*\/\s*\}\s*$/, '') // Strip */ }
  122. .replace (/\/\/.+$/gm, '') // Double-slash comments wreck CSS. Strip them.
  123. ;
  124. return str;
  125. }