YouTube Playlist Close

Allow quick closing of playlists

当前为 2016-12-09 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube Playlist Close
  3. // @version 1.0
  4. // @description Allow quick closing of playlists
  5. // @author AjaxGb
  6. // @match http*://www.youtube.com/*
  7. // @run-at document-start
  8. // @resource button https://raw.githubusercontent.com/AjaxGb/YouTube-Playlist-Close/master/closeMedium.png
  9. // @grant GM_getResourceURL
  10. // @noframes
  11. // @namespace https://greasyfork.org/users/85711
  12. // ==/UserScript==
  13.  
  14. (function(){
  15. 'use strict';
  16.  
  17. function getQueryArgs(query){
  18. query = query || window.location.search.substring(1);
  19. if(!query) return {};
  20. return query.split('&').reduce(function(prev, curr){
  21. const p = curr.split('=');
  22. prev[decodeURIComponent(p[0])] = p[1] ? decodeURIComponent(p[1]) : p[1];
  23. return prev;
  24. }, {});
  25. }
  26.  
  27. function setQueryArgs(query){
  28. if(!query) return;
  29. let search = '';
  30. for(let prop in query){
  31. if(query[prop] === undefined){
  32. search += '&'+encodeURIComponent(prop);
  33. }else{
  34. search += '&'+encodeURIComponent(prop)+'='+encodeURIComponent(query[prop]);
  35. }
  36. }
  37. window.location.search = '?' + search.substr(1);
  38. }
  39.  
  40. const b = document.createElement('button'), s = b.style;
  41. s.width = '26px';
  42. s.height = '28px';
  43. s.right = s.top = '0';
  44. s.background = 'url("' + GM_getResourceURL('button') + '") center';
  45. s.position = 'absolute';
  46. s.cursor = 'pointer';
  47. s.opacity = 0.5;
  48. b.classList.add('yt-uix-tooltip');
  49. b.title = 'Close playlist';
  50.  
  51. b.onmouseenter = function(){
  52. s.opacity = 0.6;
  53. };
  54. b.onmouseleave = function(){
  55. s.opacity = 0.5;
  56. };
  57. b.onclick = function(){
  58. const q = getQueryArgs();
  59. q.time_continue = document.getElementById('movie_player').getCurrentTime()|0;
  60. delete q.list;
  61. delete q.index;
  62. setQueryArgs(q);
  63. };
  64.  
  65. function addButton(p){
  66. p.style.position = 'relative';
  67. p.appendChild(b);
  68. }
  69.  
  70. const observer = new MutationObserver(function(mrs){
  71. if(document.contains(b)) return;
  72. for(let i = mrs.length - 1; i >= 0; --i){
  73. const t = mrs[i].target;
  74. if(t){
  75. if(t.classList && t.classList.contains('playlist-info')){
  76. addButton(t);
  77. }else if (t.getElementsByClassName){
  78. const p = t.getElementsByClassName('playlist-info')[0];
  79. if(p) addButton(p);
  80. }
  81. }
  82. }
  83. });
  84. observer.observe(document.documentElement, {
  85. childList: true,
  86. subtree: true
  87. });
  88. })();