Bilibili Auto Crop Mode

自动打开Bilibili播放器的裁切模式

  1. // ==UserScript==
  2. // @name Bilibili Auto Crop Mode
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description 自动打开Bilibili播放器的裁切模式
  6. // @author F_thx
  7. // @match https://www.bilibili.com/video/*
  8. // @grant GM_registerMenuCommand
  9. // @grant GM_setValue
  10. // @grant GM_getValue
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. // 默认设置
  18. const defaultSettings = {
  19. cropPortrait: false,
  20. maxCropPercentage: 0.65
  21. };
  22.  
  23. // 加载用户设置
  24. const userSettings = {
  25. cropPortrait: GM_getValue('cropPortrait', defaultSettings.cropPortrait),
  26. maxCropPercentage: GM_getValue('maxCropPercentage', defaultSettings.maxCropPercentage)
  27. };
  28.  
  29. // 保存用户设置
  30. function saveSettings() {
  31. GM_setValue('cropPortrait', userSettings.cropPortrait);
  32. GM_setValue('maxCropPercentage', userSettings.maxCropPercentage);
  33. }
  34.  
  35. // 添加菜单命令
  36. GM_registerMenuCommand('竖屏视频裁切', () => {
  37. userSettings.cropPortrait = !userSettings.cropPortrait;
  38. saveSettings();
  39. alert(`是否裁切竖屏视频: ${userSettings.cropPortrait}`);
  40. });
  41.  
  42. GM_registerMenuCommand('最大裁切比例', () => {
  43. const input = prompt('输入最大裁切比例 (0.1 ~ 1 默认0.65):', userSettings.maxCropPercentage);
  44. const value = parseFloat(input);
  45. if (value >= 0.1 && value <= 1) {
  46. userSettings.maxCropPercentage = value;
  47. saveSettings();
  48. alert(`最大裁切比例已改为: ${userSettings.maxCropPercentage}`);
  49. } else {
  50. alert('错误: 输入的值不在0.1 ~ 1之间');
  51. }
  52. });
  53.  
  54. // 监听键盘事件
  55. document.addEventListener('keydown', (event) => {
  56. if (event.key.toLowerCase() === 'p') {
  57. const cropModeButton = document.querySelector('.bpx-player-ctrl-setting-fit-mode .bui-switch-input');
  58. if (cropModeButton) {
  59. cropModeButton.click();
  60. console.log('Crop Mode Toggled');
  61. }
  62. }
  63. });
  64.  
  65. // 监听全屏事件
  66. document.addEventListener('fullscreenchange', () => {
  67. if (document.fullscreenElement) {
  68. enableCropMode();
  69. }
  70. });
  71.  
  72. function enableCropMode() {
  73. const cropModeButton = document.querySelector('.bpx-player-ctrl-setting-fit-mode .bui-switch-input');
  74. if (cropModeButton && __playinfo__) {
  75. //console.warn('00 检查裁切模式');
  76. const videoInfo = __playinfo__.data.dash.video[0];
  77. const videoWidth = videoInfo.width;
  78. const videoHeight = videoInfo.height;
  79. const screenWidth = window.screen.width;
  80. const screenHeight = window.screen.height;
  81. //console.warn('00 1 视频宽高:', videoWidth, videoHeight, ' 00 屏幕宽高:', screenWidth, screenHeight);
  82.  
  83. // 判断是否需要启用裁切模式
  84. const isPortraitVideo = videoHeight > videoWidth;
  85. const isExcessiveCrop = (videoWidth / screenWidth < userSettings.maxCropPercentage) ||
  86. (videoHeight / screenHeight < userSettings.maxCropPercentage);
  87. //console.warn('00 竖屏视频:', isPortraitVideo);
  88. //console.warn('00 裁切过度:', isExcessiveCrop);
  89. //console.warn('00 裁切范围:', userSettings.maxCropPercentage,' 00 竖屏裁切:', userSettings.cropPortrait);
  90.  
  91. if ((!isPortraitVideo || userSettings.cropPortrait) && !isExcessiveCrop && !cropModeButton.checked) {
  92. cropModeButton.click(); // 模拟点击以启用裁切模式
  93. console.log('00 裁切启用');
  94. }
  95. }
  96. }
  97. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址