jc youtube script - save playlist

Save playlist title to browser local storage

  1. // ==UserScript==
  2. // @name jc youtube script - save playlist
  3. // @namespace http://jc.at.home/
  4. // @require http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.1.min.js
  5. // @match http://www.youtube.com/playlist*
  6. // @match https://www.youtube.com/playlist*
  7. // @include http://www.youtube.com/playlist*
  8. // @include https://www.youtube.com/playlist*
  9. // @description Save playlist title to browser local storage
  10. // @version 2014.08.09.23h
  11. // @copyright 2011+, You
  12. // ==/UserScript==
  13.  
  14.  
  15. (function($) {
  16. //GM_addStyle(".jc_url:visited { color: red !important; }");
  17. GM_addStyle("#divJcSavePlaylistArea {border:1px solid gray; position: fixed; right: 30px; top: 130px; width: 400px;}");
  18. GM_addStyle("#divJcMsg {border:1px solid blue; height:700px; overflow-y:auto; background-color:white;}");
  19. GM_addStyle(".jcItem { border:1px solid gray; margin-bottom:5px; }");
  20. var nowUrl = '';
  21. function getJcPlaylistId() {
  22. // 取得目前的 Playlist Id
  23. var aurl = location.href;
  24. if (aurl.indexOf('playlist?')==-1) {
  25. listId = '';
  26. } else {
  27. var r = new RegExp('list=(.*?)(&|$)' , '');
  28. var listId = r.exec(aurl)[1];
  29. aurl = null;
  30. r = null;
  31. }
  32. return (listId);
  33. }
  34. function doJcLoadVideos() {
  35. // 載入已儲存的標題
  36. var listId = getJcPlaylistId();
  37. if (''==listId) {
  38. $('#divJcSavePlaylistArea').hide();
  39. } else {
  40. $('#divJcSavePlaylistArea').show();
  41. $('#divJcMsg').html( 'Load Playlist Id = ' + listId + '<hr />' + localStorage.getItem('jc_youtube_playlist_' + listId));
  42. listId = null;
  43. nowUrl = location.href;
  44. // console.log('exec doJcLoadVideos()');
  45. }
  46. window.setTimeout(function() {
  47. doJcClickLoadMoreBtn();
  48. },2000);
  49. }
  50. function doJcSaveVideos() {
  51. // 儲存影片標題至 Browser Local Storage
  52. var yVideos = [];
  53. $('.pl-video-title-link').each(function() {
  54. var atitle = $.trim($(this).text());
  55. yVideos.push(atitle);
  56. });
  57. var astr = yVideos.join(',,,,,');
  58. var listId = getJcPlaylistId();
  59. localStorage.setItem('jc_youtube_playlist_' + listId , astr);
  60. $('#divJcMsg').html( 'Playlist Id = ' + listId + '<hr />' + astr );
  61. listId = null;
  62. yVideos = null;
  63. atitle = null;
  64. astr = null;
  65. }
  66. function doJcCheckVideos() {
  67. // 檢查哪些影片不存在 或 重複
  68. var listId = getJcPlaylistId();
  69. var videosStr = localStorage.getItem('jc_youtube_playlist_' + listId);
  70. var videosStrLen = parseInt(videosStr.length / 1024 , 10) + ' KB';
  71. $('#spanJcSavePlaylistDataSize').text(videosStrLen);
  72. var videosArr = videosStr.split(',,,,,');
  73. var atitle;
  74. var btitle;
  75. var surl;
  76. var bArr = [];
  77. var video_link_class = '.pl-video-title-link';
  78. $(video_link_class).each(function() {
  79. var atitle = $.trim($(this).text());
  80. bArr[atitle] = '1';
  81. });
  82. $('#divJcMsg').html('');
  83. for (video in videosArr) {
  84. btitle = videosArr[video];
  85. if (undefined == bArr[btitle]) {
  86. surl = 'http://www.youtube.com/results?search_query=' + encodeURIComponent(btitle);
  87. $('#divJcMsg').append('<div class="jcItem">' + btitle + ' : Not Exists! ' + ' <a target="_blank" href="' + surl + '">Search</a>' + '</div>');
  88. }
  89. } // for
  90. video = null;
  91. var cArr = [];
  92. for (video in videosArr) {
  93. btitle = videosArr[video];
  94. cArr[btitle] = '2';
  95. }
  96. $('#divJcMsg').append('<hr />');
  97. $(video_link_class).each(function() {
  98. var atitle = $.trim($(this).text());
  99. if (undefined == cArr[atitle]) {
  100. //surl = 'http://www.youtube.com/results?search_query=' + encodeURIComponent(btitle);
  101. $('#divJcMsg').append('<div class="jcItem">' + atitle + ' : Not Save! </div>');
  102. }
  103. });
  104. //$('#divJcMsg').html( bArr.join('<br />') );
  105. surl = null;
  106. atitle = null;
  107. btitle = null;
  108. listId = null;
  109. videosArr = null;
  110. videosStr = null;
  111. cArr = null;
  112. bArr = null;
  113. }
  114. function doJcAddSavePlaylistBtn() {
  115. $('body').append('<div id="divJcSavePlaylistArea">'+
  116. '<div><input type="button" id="btnJcMax" value="放大" /><input type="button" id="btnJcMin" value="縮小" /></div>'+
  117. '<div id="divJcSavePlaylistBtns2Area"><input type="button" id="btnJcSave" value="Save To Local Storage" />' +
  118. '<input type="button" id="btnJcCheck" value="Check deleted videos" /><span id="spanJcSavePlaylistDataSize">--- KB</span></div>' +
  119. '<div id="divJcMsg"></div></div>');
  120. $('#btnJcSave').click(function() {
  121. // 儲存影片標題至 Browser Local Storage
  122. doJcSaveVideos();
  123. });
  124. $('#btnJcCheck').click(function() {
  125. // 檢查哪些影片不存在 或 重複
  126. doJcCheckVideos();
  127. });
  128. $('#btnJcMax').on('click', function() {
  129. $('#divJcMsg').show();
  130. $('#divJcSavePlaylistBtns2Area').show();
  131. });
  132. $('#btnJcMin').on('click', function() {
  133. $('#divJcMsg').hide();
  134. $('#divJcSavePlaylistBtns2Area').hide();
  135. });
  136. // 載入已儲存的標題
  137. doJcLoadVideos();
  138. }
  139. function doJcClickLoadMoreBtn() {
  140. // Click LoadMore Button
  141. if ($('button.load-more-button').length>0) {
  142. $('button.load-more-button').eq(0).trigger('click');
  143. }
  144. }
  145. $(document).ready(function() {
  146. window.setTimeout(function() {
  147. doJcAddSavePlaylistBtn();
  148. } , 200);
  149. window.setInterval(function() {
  150. if (nowUrl != location.href) {
  151. doJcLoadVideos();
  152. }
  153. },5000);
  154. });
  155. })(jQuery);