Greasy Fork 支持简体中文。

Youtube video crop

Crop videos on youtube

  1. // ==UserScript==
  2. // @name Youtube video crop
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1.2
  5. // @description Crop videos on youtube
  6. // @locale en
  7. // @author You
  8. // @match *://www.youtube.com/*
  9. // @require https://code.jquery.com/jquery-3.2.1.min.js
  10. // @grant none
  11. // ==/UserScript==
  12. (function () {
  13. 'use strict';
  14. var DEBUG = false;
  15. function injectCSS(css) {
  16. var styleTag = $('<style>' + css + '</style>');
  17. $('html > head').append(styleTag);
  18. console.log('injected css:', css);
  19. }
  20. injectCSS(
  21. '#_area_overlay{' +
  22. 'display: none; position: absolute; pointer-events: none; ' +
  23. 'top: 0: left: 0; z-index:10; ' +
  24. 'background: rgba(255, 0, 0, 0.3); ' +
  25. '} ' +
  26. '._stupid_btn{'+
  27. 'vertical-align: top; font-weight: bold; ' +
  28. 'width: auto !important; '+
  29. 'padding-right: 5px; padding-left: 5px;' +
  30. '}'
  31. );
  32. var _is_down = false;
  33. var _area_updating = false;
  34. var sx,sy;
  35. var sx_,sy_;
  36. var ex,ey;
  37. var scale = 1;
  38. var overlay;
  39. var _is_cropped = false;
  40. function scale_video(x, y, scale) {
  41. injectCSS('.video-stream{ transform: ' +
  42. 'translate(-50%, -50%) ' +
  43. 'scale(' + scale + ') ' +
  44. 'translate(50%, 50%) ' +
  45. 'translate(-' + x + '%, -' + y + '%)' +
  46. '}');
  47. }
  48. function _update_area() {
  49. var player = $('.video-stream');
  50. var vwidth = player.width();
  51. var vheight = player.height();
  52. scale_video(sx, sy, scale);
  53. overlay.css('display', 'none');
  54. _area_updating = false;
  55. }
  56. function setup_cropper(){
  57. if(DEBUG) console.debug('Try setting up cropper.');
  58. if( $('.video-stream').length == 0) return;
  59. if(DEBUG) console.debug('Video stream found.');
  60. overlay = $('#_area_overlay');
  61. if(overlay.length > 0){
  62. if(DEBUG) console.debug('Overlay already present.');
  63. }else{
  64. if(DEBUG) console.debug('Creating overlay.');
  65. overlay = $('<div id="_area_overlay"></div>');
  66. $('body').append(overlay);
  67. if(DEBUG) console.debug('Adding buttons.');
  68. // we just assume if there is no overlay there are also no new buttons.
  69. $('.ytp-right-controls').prepend('<button class="_stupid_btn ytp-button" title="Crop video" id="_btn_crop">Crop</button>');
  70. // area calculation
  71. if(DEBUG) console.debug('Setting up area calulation event handler.');
  72. sx = sy = ex = ey = sx_ = sy_ = 0;
  73. $('#player').mousedown(function (e) {
  74. if (!_area_updating) return;
  75. _is_down = true;
  76. var vwidth = $('.video-stream').width();
  77. var vheight = $('.video-stream').height();
  78. var offset = $('.video-stream').offset();
  79. sx_ = (e.pageX - offset.left);
  80. sy_ = (e.pageY - offset.top);
  81. sx = ((100 / vwidth) * sx_);
  82. sy = ((100 / vheight) * sy_);
  83. overlay.css('top', e.pageY + 'px');
  84. overlay.css('left', e.pageX + 'px');
  85. });
  86. $('#player').mouseup(function (e) {
  87. if ((!_area_updating) || (!_is_down)) return;
  88. _is_down = false;
  89. _update_area();
  90. });
  91. $('#player').mousemove(function (e) {
  92. if ((!_area_updating) || (!_is_down)) return;
  93. var vwidth = $('.video-stream').width();
  94. var offset = $('.video-stream').offset();
  95. ex = (e.pageX - offset.left);
  96. ey = (e.pageY - offset.top);
  97. scale = (vwidth * 1) / (ex - sx_);
  98. overlay.css('width', (ex - sx_) + 'px');
  99. overlay.css('height', (ey - sy_) + 'px');
  100. });
  101. $('#player').mouseleave(function (e) {
  102. if ((!_area_updating) || (!_is_down)) return;
  103. _is_down = false;
  104. _update_area();
  105. });
  106. // UI buttons
  107. var btn = "#_btn_crop";
  108. if(DEBUG){
  109. console.debug('Setting up UI button handler.');
  110. var button = $('<button id="_dbg_btn"><h2>CROP</h2></button>');
  111. $('#player').prepend(button);
  112. btn += ", #_dbg_btn";
  113. }
  114. $(btn).click(function (e) {
  115. if(_is_cropped){
  116. injectCSS('.video-stream{ transform:translate(-' + 0 + '%, ' + 0 + '%) scale(1) }');
  117. _is_cropped = false;
  118. $(btn).text('Crop');
  119. return;
  120. }
  121. $(btn).text('Uncrop');
  122. overlay.css('display', 'block');
  123. _is_cropped = true;
  124. _area_updating = true;
  125. });
  126. }
  127. }
  128. addEventListener("spfdone", setup_cropper);
  129. setup_cropper();
  130. }) ();