-
Notifications
You must be signed in to change notification settings - Fork 3
/
four-column-responsive.user.js
162 lines (135 loc) Β· 4.3 KB
/
four-column-responsive.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// ==UserScript==
// @name Four Column Responsive
// @namespace http://tampermonkey.net/
// @version 0.1
// @description render text in as many as four columns, depending on display size
// @author Josh Parker
// @source https://github.com/joshparkerj/silly-internet-tricks/blob/main/wikipedia/four-column-responsive.user.js
// @downloadURL https://gist.github.com/joshparkerj/fbe1e3126cde7f365a9492f951d9bf7a/raw/four-column-responsive.user.js
// @updateURL https://gist.github.com/joshparkerj/fbe1e3126cde7f365a9492f951d9bf7a/raw/four-column-responsive.meta.js
// @match https://en.wikipedia.org/wiki/*
// @exclude https://en.wikipedia.org/wiki/Category*
// @exclude https://en.wikipedia.org/wiki/Template*
// @exclude https://en.wikipedia.org/wiki/Talk*
// @icon https://www.google.com/s2/favicons?domain=wikipedia.org
// @grant none
// ==/UserScript==
(function fourColumnResponsive() {
const css = `
#mw-content-text > #grid-container {
display: grid;
grid-template-columns: repeat(4, 1fr);
overflow: hidden;
position: relative;
top: -37px;
}
#mw-content-text > #grid-container > .mw-parser-output {
min-width: 0;
grid-column: span 1;
position: relative;
padding: 1em;
border-right: 1px solid #bbc;
}
@media (max-width: 2000px) {
#mw-content-text > #grid-container > .mw-parser-output:nth-child(4) {
display: none;
}
#mw-content-text > #grid-container {
grid-template-columns: repeat(3, 1fr);
}
}
@media (max-width: 1500px) {
#mw-content-text > #grid-container > .mw-parser-output:nth-child(3) {
display: none;
}
#mw-content-text > #grid-container {
grid-template-columns: repeat(2, 1fr);
}
}
@media (max-width: 1000px) {
#mw-content-text > #grid-container > .mw-parser-output:nth-child(2) {
display: none;
}
#mw-content-text > #grid-container {
grid-template-columns: repeat(1, 1fr);
}
}
div#mw-panel,
div#mw-head,
div#mw-page-base,
div#siteSub,
h1#firstHeading {
z-index: 1;
}
div#mw-page-base,
div#siteSub,
h1#firstHeading {
position: relative;
}
div#siteSub,
h1#firstHeading {
background-color: white;
}
div#siteSub,
h1#firstHeading {
padding-top: 21px;
}
div#siteSub {
top: -29px;
}
h1#firstHeading {
top: -21px;
border-top: 1px solid #a7d7f9
}
`;
const gridContainer = document.createElement('div');
gridContainer.id = 'grid-container';
const mct = document.querySelector('div#mw-content-text');
mct.appendChild(gridContainer);
const mpo = mct.querySelector('.mw-parser-output');
mpo.querySelectorAll('.wikitable').forEach((wikitable) => {
const tableContainer = document.createElement('div');
tableContainer.classList.add('table-container');
tableContainer.appendChild(wikitable.cloneNode(true));
tableContainer.style.setProperty('max-width', '100%');
tableContainer.style.setProperty('overflow', 'auto');
if (wikitable.getAttribute('align') === 'right') {
tableContainer.style.setProperty('float', 'right');
}
wikitable.insertAdjacentElement('afterend', tableContainer);
wikitable.parentNode.removeChild(wikitable);
});
for (let i = 0; i < 4; i++) {
gridContainer.appendChild(mpo.cloneNode(true));
}
mpo.parentNode.removeChild(mpo);
const style = document.createElement('style');
style.appendChild(new Text(css));
gridContainer.appendChild(style);
const intersectionObserver = new IntersectionObserver((e, o) => {
const columnHeight = e[0].intersectionRect.height;
const totalHeight = e[0].target.scrollHeight;
const columns = [...document.querySelectorAll('#grid-container > .mw-parser-output')].filter(
(column) => getComputedStyle(column).getPropertyValue('display') !== 'none',
);
columns.forEach((column, i) => {
const top = columnHeight * i;
column.style.setProperty('top', `-${top}px`);
});
if (totalHeight > columns.length * columnHeight) {
gridContainer.style.setProperty(
'height',
`${totalHeight - (columns.length - 1) * columnHeight}px`,
);
} else {
gridContainer.style.setProperty('height', `${totalHeight / columns.length}px`);
}
o.disconnect();
});
const resizeColumns = () => intersectionObserver.observe(document.querySelector('#grid-container > .mw-parser-output'));
resizeColumns();
for (let i = 0; i < 10; i++) {
setTimeout(resizeColumns, 200 * i);
}
setInterval(resizeColumns, 2000);
}());