YouTube Playlist Close

Allow quick closing of playlists

当前为 2017-10-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube Playlist Close
  3. // @version 1.7
  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/closeMediumDark.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 = '44px';
  43. s.height = '40px';
  44. s.position = 'absolute';
  45. s.top = '-8px';
  46. s.right = '0';
  47. s.background = 'url("' + GM_getResourceURL('button') + '") center';
  48. s.backgroundRepeat = 'no-repeat';
  49. s.cursor = 'pointer';
  50. s.opacity = 0.5;
  51. b.title = 'Close playlist';
  52.  
  53. function updateURL() {
  54. b.href = location.toString();
  55. q = getQueryArgs(b.search);
  56. delete q.list;
  57. delete q.index;
  58. b.search = setQueryArgs(q);
  59. }
  60.  
  61. b.onmouseenter = function(){
  62. updateURL();
  63. s.opacity = 0.6;
  64. };
  65. b.onmouseleave = function(){
  66. s.opacity = 0.5;
  67. };
  68. b.onmouseup = function(){
  69. updateURL();
  70. const t = document.getElementById('movie_player').getCurrentTime()|0;
  71. if (t > 0) {
  72. q.time_continue = t;
  73. b.search = setQueryArgs(q);
  74. setTimeout(resetQuery);
  75. }
  76. };
  77. function resetQuery(){
  78. delete q.time_continue;
  79. b.search = setQueryArgs(q);
  80. }
  81.  
  82. function addButton(p){
  83. updateURL();
  84. p.style.position = 'relative';
  85. p.appendChild(b);
  86. }
  87.  
  88. const observer = new MutationObserver(function(mrs){
  89. if(document.contains(b)) return;
  90. const p = document.getElementById('header-contents');
  91. if(p) addButton(p);
  92. });
  93. observer.observe(document.documentElement, {
  94. childList: true,
  95. subtree: true
  96. });
  97. })();