Speed Up Web Page Loading

Make web pages load faster by blocking ads and implementing lazy loading for images.

目前为 2024-06-15 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Speed Up Web Page Loading
  3. // @namespace https://discord.gg/gFNAH7WNZj
  4. // @version 1.0
  5. // @description Make web pages load faster by blocking ads and implementing lazy loading for images.
  6. // @author Bacon But Pro
  7. // @match *://*/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Block ads and tracking scripts
  16. const blockedDomains = [
  17. 'doubleclick.net',
  18. 'googlesyndication.com',
  19. 'google-analytics.com',
  20. 'adsafeprotected.com',
  21. 'adnxs.com',
  22. 'rubiconproject.com',
  23. 'pubmatic.com',
  24. 'scorecardresearch.com',
  25. 'bluekai.com'
  26. ];
  27.  
  28. const observer = new MutationObserver(() => {
  29. document.querySelectorAll('script[src], iframe[src]').forEach(el => {
  30. blockedDomains.forEach(domain => {
  31. if (el.src.includes(domain)) {
  32. el.remove();
  33. }
  34. });
  35. });
  36. });
  37.  
  38. observer.observe(document.documentElement, { childList: true, subtree: true });
  39.  
  40. // Implement lazy loading for images
  41. document.addEventListener('DOMContentLoaded', () => {
  42. const images = document.querySelectorAll('img');
  43.  
  44. images.forEach(image => {
  45. if (image.complete) return;
  46. image.setAttribute('loading', 'lazy');
  47. });
  48.  
  49. const lazyLoadObserver = new IntersectionObserver((entries, observer) => {
  50. entries.forEach(entry => {
  51. if (entry.isIntersecting) {
  52. const img = entry.target;
  53. img.src = img.dataset.src;
  54. observer.unobserve(img);
  55. }
  56. });
  57. });
  58.  
  59. images.forEach(img => {
  60. if (img.dataset.src) {
  61. lazyLoadObserver.observe(img);
  62. } else {
  63. img.dataset.src = img.src;
  64. img.src = '';
  65. lazyLoadObserver.observe(img);
  66. }
  67. });
  68. });
  69.  
  70. })();