Greasy Fork 支持简体中文。

Zoom function for YouTube

Add zoom function to YouTube video player.

目前為 2017-08-04 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Zoom function for YouTube
  3. // @name:ja YouTubeで動画をズーム
  4. // @description Add zoom function to YouTube video player.
  5. // @description:ja YouTubeの動画プレイヤーにズーム機能を追加します。
  6. // @namespace https://twitter.com/sititou70
  7. // @include /https?:\/\/www\.youtube\.com.*/
  8. // @require https://code.jquery.com/jquery-3.2.1.min.js
  9. // @version 1.5.1
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. jQuery.noConflict();
  14. (function($){
  15. //function
  16. var video_scaleup = function(select_rect){
  17. var player_aspect_ratio = $("#movie_player").width() / $("#movie_player").height();
  18. var select_aspect_ratio = select_rect.width / select_rect.height;
  19. var centering_offset = {
  20. x: 0,
  21. y: 0
  22. };
  23. var scale_up_ratio = null;
  24. if(player_aspect_ratio < select_aspect_ratio){
  25. //fit width
  26. scale_up_ratio = $("#movie_player").width() / select_rect.width;
  27. centering_offset.y = ($("#movie_player").height() - select_rect.height * scale_up_ratio) / 2;
  28. }else{
  29. //fit height
  30. scale_up_ratio = $("#movie_player").height() / select_rect.height;
  31. centering_offset.x = ($("#movie_player").width() - select_rect.width * scale_up_ratio) / 2;
  32. }
  33. $("video").stop().animate({
  34. top: select_rect.y * scale_up_ratio * -1 + centering_offset.y + "px",
  35. left: select_rect.x * scale_up_ratio * -1 + centering_offset.x + "px",
  36. width: $("video").width() * scale_up_ratio,
  37. height: $("video").height() * scale_up_ratio
  38. }, 500);
  39. now_scale_up = true;
  40. };
  41. var video_scaledown = function(){
  42. $("video").stop().animate({
  43. top: save_css.top,
  44. left: save_css.left,
  45. width: save_css.width,
  46. height: save_css.height
  47. }, 500);
  48. now_scale_up = false;
  49. };
  50. //set event handler
  51. var now_scale_up = false;
  52. var mousedown_offset = null;
  53. var save_css = null;
  54. var timer_id = 0;
  55. var set_player_event = function(){
  56. $("#player-api").mousemove(function(){
  57. $(".ytp-gradient-top").css("opacity", "1");
  58. $(".ytp-chrome-top").css("opacity", "1");
  59. $(".ytp-gradient-bottom").css("opacity", "1");
  60. $(".ytp-chrome-bottom").css("opacity", "1");
  61. clearTimeout(timer_id);
  62. timer_id = setTimeout(function(){
  63. $(".ytp-gradient-top").css("opacity", "0");
  64. $(".ytp-chrome-top").css("opacity", "0");
  65. $(".ytp-gradient-bottom").css("opacity", "0");
  66. $(".ytp-chrome-bottom").css("opacity", "0");
  67. }, 2000);
  68. }).mouseleave(function(){
  69. $(".ytp-gradient-top").css("opacity", "0");
  70. $(".ytp-chrome-top").css("opacity", "0");
  71. $(".ytp-gradient-bottom").css("opacity", "0");
  72. $(".ytp-chrome-bottom").css("opacity", "0");
  73. });
  74. $("video").mousedown(function(e){
  75. mousedown_offset = {offsetX: e.offsetX, offsetY: e.offsetY};
  76. if(!now_scale_up){
  77. save_css = {
  78. top: $("video").css("top"),
  79. left: $("video").css("left"),
  80. width: $("video").css("width"),
  81. height: $("video").css("height")
  82. };
  83. }
  84. });
  85. $("video").mouseup(function(e){
  86. var select_rect = {
  87. width: Math.abs(e.offsetX - mousedown_offset.offsetX),
  88. height: Math.abs(e.offsetY - mousedown_offset.offsetY),
  89. x: Math.min(mousedown_offset.offsetX, e.offsetX),
  90. y: Math.min(mousedown_offset.offsetY, e.offsetY)
  91. };
  92. if(!(select_rect.width <= 10 || select_rect.height <= 10)){
  93. video_scaleup(select_rect);
  94. return false;
  95. }
  96. return true;
  97. });
  98. };
  99. $(document).keydown(function(key){
  100. switch(key.keyCode){
  101. case 82:
  102. video_scaledown();
  103. break;
  104. }
  105. });
  106. var observer = new MutationObserver(function(){
  107. if($("#player-api").length === 1)set_player_event();
  108. });
  109. observer.observe($("#page")[0], {
  110. attributes: true
  111. });
  112. set_player_event();
  113. })(jQuery);