Guest User

reddit 4 column layout

a guest
Jul 10th, 2024
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. // ==UserScript==
  2. // u/name Reddit Four-Column Layout with Zigzag Flow
  3. // u/namespace http://tampermonkey.net/
  4. // u/version 1.0
  5. // u/description Modifies Reddit to display content in four columns with a zigzag flow
  6. // u/match https://*.reddit.com/*
  7. // u/exclude https://www.reddit.com/r/*/comments/*
  8. // u/exclude https://www.reddit.com/notifications
  9. // u/grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. function applyFourColumnLayout(container) {
  16. container.style.display = 'grid';
  17. container.style.gridTemplateColumns = 'repeat(4, 1fr)';
  18. container.style.gap = '15px'; // Reduced gap to accommodate more columns
  19. container.style.width = '100%';
  20.  
  21. let index = 0;
  22. function processChildren(parent) {
  23. const children = Array.from(parent.children);
  24. for (let i = 0; i < children.length; i++) {
  25. const child = children[i];
  26. if (child.tagName === 'ARTICLE' || child.tagName === 'FACEPLATE-BATCH') {
  27. const row = Math.floor(index / 4);
  28. const col = index % 4;
  29.  
  30. child.style.gridArea = `${row + 1} / ${col + 1} / auto / span 1`;
  31. child.style.width = '100%';
  32. child.style.maxWidth = '100%';
  33.  
  34. if (child.tagName === 'FACEPLATE-BATCH') {
  35. child.style.display = 'contents';
  36. processChildren(child);
  37. } else {
  38. index++;
  39. }
  40.  
  41. // Handle HR after the article
  42. if (i + 1 < children.length && children[i + 1].tagName === 'HR') {
  43. const hr = children[i + 1];
  44. hr.style.gridArea = `${row + 1} / ${col + 1} / auto / span 1`;
  45. hr.style.width = '100%';
  46. hr.style.maxWidth = '100%';
  47. hr.style.margin = '8px 0'; // Reduced margin
  48. i++;
  49. }
  50. }
  51. }
  52. }
  53.  
  54. processChildren(container);
  55. }
  56.  
  57. function applyLayoutToAll() {
  58. // Target the grid-container
  59. const gridContainer = document.querySelector('.grid-container');
  60. if (gridContainer) {
  61. gridContainer.style.display = 'block';
  62. gridContainer.style.width = '100%';
  63. gridContainer.style.maxWidth = '100%';
  64. }
  65.  
  66. // Target the subgrid-container
  67. const subgridContainer = document.querySelector('.subgrid-container');
  68. if (subgridContainer) {
  69. subgridContainer.style.gridColumn = '1 / -1';
  70. subgridContainer.style.maxWidth = '100%';
  71. subgridContainer.style.width = '100%';
  72. subgridContainer.style.padding = '0 15px'; // Reduced padding
  73. subgridContainer.style.margin = '0';
  74. subgridContainer.style.boxSizing = 'border-box';
  75. }
  76.  
  77. // Target the main-container
  78. const mainContainer = document.querySelector('.main-container');
  79. if (mainContainer) {
  80. mainContainer.style.width = '100%';
  81. mainContainer.style.maxWidth = '100%';
  82. }
  83.  
  84. // Target the main content
  85. const mainContent = document.getElementById('main-content');
  86. if (mainContent) {
  87. mainContent.style.width = '100%';
  88. }
  89.  
  90. // Apply layout to shreddit-feed
  91. const shredditFeed = document.querySelector('shreddit-feed');
  92. if (shredditFeed) {
  93. applyFourColumnLayout(shredditFeed);
  94. }
  95. }
  96.  
  97. // Function to continuously apply the layout
  98. function continuouslyApplyLayout() {
  99. applyLayoutToAll();
  100. requestAnimationFrame(continuouslyApplyLayout);
  101. }
  102.  
  103. // Start continuously applying the layout
  104. continuouslyApplyLayout();
  105.  
  106. // MutationObserver for dynamic content loading
  107. const observer = new MutationObserver((mutations) => {
  108. mutations.forEach((mutation) => {
  109. if (mutation.type === 'childList') {
  110. applyLayoutToAll();
  111. }
  112. });
  113. });
  114.  
  115. // Start observing the document body for changes
  116. observer.observe(document.body, { childList: true, subtree: true });
  117. })();
Advertisement
Add Comment
Please, Sign In to add comment