Auto HDR

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

当前为 2024-08-27 提交的版本,查看 最新版本

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