Universal Video Sharpener (CSS Filter)

Applies video sharpening using CSS filters across websites

目前為 2024-12-21 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Universal Video Sharpener (CSS Filter)
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.2
  5. // @description Applies video sharpening using CSS filters across websites
  6. // @match *://*/*
  7. // @grant GM_setValue
  8. // @grant GM_getValue
  9. // @grant GM_registerMenuCommand
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Configuration
  17. const CONFIG = {
  18. contrast: 1.1,
  19. brightness: 1.2,
  20. saturate: 1.1,
  21. debugMode: false
  22. };
  23.  
  24. // Logging function
  25. function log(message) {
  26. if (CONFIG.debugMode) {
  27. console.log(`[Video Sharpener] ${message}`);
  28. }
  29. }
  30.  
  31. // Detect if an element is likely a video
  32. function isVideoElement(element) {
  33. return element instanceof HTMLVideoElement &&
  34. element.videoWidth > 0 &&
  35. element.videoHeight > 0 &&
  36. !element.paused;
  37. }
  38.  
  39. // Apply CSS sharpness filter
  40. function applySharpnessFilter(video, isEnabled) {
  41. if (!video) return;
  42.  
  43. try {
  44. if (isEnabled) {
  45. const { contrast, brightness, saturate } = CONFIG;
  46. video.style.filter = `
  47. contrast(${contrast})
  48. brightness(${brightness})
  49. saturate(${saturate})
  50. `;
  51. video.dataset.sharpened = 'true';
  52. log('CSS Sharpness filter applied');
  53. } else {
  54. video.style.filter = 'none';
  55. delete video.dataset.sharpened;
  56. log('Sharpness filter removed');
  57. }
  58. } catch (error) {
  59. console.error('Error applying sharpness filter:', error);
  60. }
  61. }
  62.  
  63. // Main processing function
  64. function processVideos() {
  65. const isScriptEnabled = GM_getValue('universalSharpenerEnabled', false);
  66. if (!isScriptEnabled) return;
  67.  
  68. const videos = document.querySelectorAll('video:not([data-sharpened])');
  69. videos.forEach(video => {
  70. if (isVideoElement(video)) {
  71. applySharpnessFilter(video, true);
  72. }
  73. });
  74. }
  75.  
  76. function initScript() {
  77. // Global toggle menu command
  78. GM_registerMenuCommand('Toggle Video Sharpener', () => {
  79. const currentState = GM_getValue('universalSharpenerEnabled', false);
  80. GM_setValue('universalSharpenerEnabled', !currentState);
  81. location.reload();
  82. });
  83.  
  84. // Use MutationObserver for dynamic content
  85. const observer = new MutationObserver(() => {
  86. processVideos();
  87. });
  88.  
  89. // Observe the entire document for changes
  90. observer.observe(document.body, {
  91. childList: true,
  92. subtree: true
  93. });
  94.  
  95. processVideos();
  96. setInterval(processVideos, 3000);
  97. }
  98.  
  99. initScript();
  100. })();