YouTube Playlist Close

Allow quick closing of playlists

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

  1. // ==UserScript==
  2. // @name YouTube Playlist Close
  3. // @version 1.1
  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. return '?' + search.substr(1);
  38. }
  39.  
  40. let q;
  41. const b = document.createElement('a'), s = b.style;
  42. s.width = '26px';
  43. s.height = '28px';
  44. s.right = s.top = '0';
  45. s.background = 'url("' + GM_getResourceURL('button') + '") center';
  46. s.position = 'absolute';
  47. s.cursor = 'pointer';
  48. s.opacity = 0.5;
  49. b.classList.add('yt-uix-tooltip');
  50. b.title = 'Close playlist';
  51.  
  52. b.onmouseenter = function(){
  53. s.opacity = 0.6;
  54. };
  55. b.onmouseleave = function(){
  56. s.opacity = 0.5;
  57. };
  58. b.onmouseup = function(){
  59. q.time_continue = document.getElementById('movie_player').getCurrentTime()|0;
  60. b.search = setQueryArgs(q);
  61. setTimeout(resetQuery);
  62. };
  63. function resetQuery(){
  64. delete q.time_continue;
  65. b.search = setQueryArgs(q);
  66. }
  67.  
  68. function addButton(p){
  69. b.href = location.toString();
  70. q = getQueryArgs(b.search);
  71. delete q.list;
  72. delete q.index;
  73. b.search = setQueryArgs(q);
  74. p.style.position = 'relative';
  75. p.appendChild(b);
  76. }
  77.  
  78. const observer = new MutationObserver(function(mrs){
  79. if(document.contains(b)) return;
  80. for(let i = mrs.length - 1; i >= 0; --i){
  81. const t = mrs[i].target;
  82. if(t){
  83. if(t.classList && t.classList.contains('playlist-info')){
  84. addButton(t);
  85. }else if (t.getElementsByClassName){
  86. const p = t.getElementsByClassName('playlist-info')[0];
  87. if(p) addButton(p);
  88. }
  89. }
  90. }
  91. });
  92. observer.observe(document.documentElement, {
  93. childList: true,
  94. subtree: true
  95. });
  96. })();