Auto HDR

Automatically apply an HDR-like effect to all images on a webpage with adjustable settings

目前为 2024-08-18 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Auto HDR
  3. // @namespace http://taeparlaytampermonkey.net/
  4. // @version 11
  5. // @description Automatically apply an HDR-like effect to all images on a webpage with adjustable settings
  6. // @author tae
  7. // @license MIT
  8. // @match *://*/*
  9. // @grant none
  10. // ==/UserScript==
  11. (function() {
  12. 'use strict';
  13. // Default settings
  14. let settings = {
  15. hdrEnabled: true,
  16. factor: 1.2, // Adjusted factor to prevent over-brightening
  17. excludedSites: []
  18. };
  19. // Load settings from local storage
  20. function loadSettings() {
  21. const savedSettings = localStorage.getItem('autoHDRSettings');
  22. if (savedSettings) {
  23. settings = JSON.parse(savedSettings);
  24. }
  25. }
  26. // Save settings to local storage
  27. function saveSettings() {
  28. localStorage.setItem('autoHDRSettings', JSON.stringify(settings));
  29. }
  30. // Apply HDR-like effect to each image
  31. function applyHDREffect(img) {
  32. const canvas = document.createElement('canvas');
  33. const context = canvas.getContext('2d');
  34. canvas.width = img.width;
  35. canvas.height = img.height;
  36. context.drawImage(img, 0, 0, img.width, img.height);
  37. let imageData = context.getImageData(0, 0, img.width, img.height);
  38. let data = imageData.data;
  39. for (let i = 0; i < data.length; i += 4) {
  40. data[i] = clamp(data[i] * settings.factor); // Red
  41. data[i + 1] = clamp(data[i + 1] * settings.factor); // Green
  42. data[i + 2] = clamp(data[i + 2] * settings.factor); // Blue
  43. }
  44. context.putImageData(imageData, 0, 0);
  45. img.src = canvas.toDataURL();
  46. img.dataset.hdrApplied = true; // Mark as HDR applied
  47. }
  48. // Helper function to clamp values between 0 and 255
  49. function clamp(value) {
  50. return Math.max(0, Math.min(255, value));
  51. }
  52. // Apply or remove HDR effect based on settings
  53. function toggleHDREffect() {
  54. const images = document.querySelectorAll('img');
  55. images.forEach(img => {
  56. if (settings.hdrEnabled) {
  57. if (!img.dataset.hdrApplied) {
  58. applyHDREffect(img);
  59. }
  60. } else {
  61. if (img.dataset.hdrApplied) {
  62. img.src = img.src; // Reset image source to remove HDR effect
  63. img.removeAttribute('data-hdrApplied');
  64. }
  65. }
  66. });
  67. }
  68. // Check if the current site is excluded
  69. function isSiteExcluded() {
  70. return settings.excludedSites.some(site => window.location.href.includes(site));
  71. }
  72. // Run the functions on page load
  73. window.addEventListener('load', () => {
  74. loadSettings();
  75. if (!isSiteExcluded()) {
  76. toggleHDREffect();
  77. }
  78. });
  79. })();