-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcustom.js
280 lines (260 loc) · 10.2 KB
/
custom.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// common =================================================================
MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
const watchTarget = document.getElementById("app-container");
// throttle MutationObserver
// from https://stackoverflow.com/a/52868150
const throttle = (func, limit) => {
let inThrottle;
return (...args) => {
if (!inThrottle) {
func(...args);
inThrottle = setTimeout(() => (inThrottle = false), limit);
}
};
};
// ================================== END COMMON
// query table resizer ==============================================
// source : https://htmldom.dev/resize-columns-of-a-table/
console.log("========= query table resizer v20220312 ============");
const createResizableColumn = function (col, resizer) {
// Track the current position of mouse
let x = 0;
let w = 0;
const mouseDownHandler = function (e) {
// Get the current mouse position
x = e.clientX;
// Calculate the current width of column
const styles = window.getComputedStyle(col);
w = parseInt(styles.width, 10);
// Attach listeners for document's events
document.addEventListener("mousemove", mouseMoveHandler);
document.addEventListener("mouseup", mouseUpHandler);
};
const mouseMoveHandler = function (e) {
// Determine how far the mouse has been moved
const dx = e.clientX - x;
// Update the width of column
col.style.width = `${w + dx}px`;
};
// When user releases the mouse, remove the existing event listeners
const mouseUpHandler = function () {
document.removeEventListener("mousemove", mouseMoveHandler);
document.removeEventListener("mouseup", mouseUpHandler);
};
resizer.addEventListener("mousedown", mouseDownHandler);
};
const updateTables = function () {
// Query the table
const table = document.querySelectorAll(".table-auto:not(.table-resizable)");
for (let i = 0; i < table.length; i++) {
// Query all headers1
const cols = table[i].querySelectorAll("thead tr > th.whitespace-nowrap");
// Loop ver them
Array.from(cols).forEach((col) => {
// Create a resizer element
const resizer = document.createElement("div");
resizer.classList.add("query-table-resizer");
table[i].classList.add("table-resizable");
console.info("-- injected div.query-table-resizer --");
// Add a resizer element to the column
col.appendChild(resizer);
createResizableColumn(col, resizer);
});
}
};
const updateTablesThrottled = throttle(updateTables, 1000);
const obsTable = new MutationObserver(updateTablesThrottled);
obsTable.observe(watchTarget, {
subtree: true,
childList: true,
});
// ====================================================== query table resizer
// namespace prefixes collapser =============================================
function hideNamespace() {
console.info("====== LS HIDE NAMESPACE v20220314 =====");
let nmsp = document.querySelectorAll(
'a.page-ref[data-ref*="/"]:not(.hidden-namespace)'
);
for (var i = 0; i < nmsp.length; i++) {
if (nmsp[i].innerText.indexOf("/") !== -1) {
nmsp[i].innerHTML =
"<span style='color:rgb(133, 211, 81)'>..</span>" +
nmsp[i].innerText.substring(nmsp[i].innerText.lastIndexOf("/"));
nmsp[i].classList.add("hidden-namespace");
//console.info(" namespace off ==> " + nmsp[i].innerText);
}
}
}
const updateHideNamespace = throttle(hideNamespace, 1000);
const obsNamespace = new MutationObserver(updateHideNamespace);
obsNamespace.observe(watchTarget, {
subtree: true,
attributes: true,
});
//===================================== end of namespace prefixes collapser
// property data-refs =====================================
// injects [data-refs-self='property'] attributes to property divs
// to be used in the next functions + custom.css
console.log("========= property data-ref v20220715 ============");
const addPropDataRef = function () {
console.log("addPropDataRef running...");
const propertiesBlocks = document.querySelectorAll(
"#main-content-container .page.relative > .relative .block-properties:not(.datarefd)" //.page.relative > .relative => main container only
);
for (let i = 0; i < propertiesBlocks.length; i++) {
const propertySpan = propertiesBlocks[i].children;
Array.from(propertySpan).forEach((divProp) => {
console.log(" divProp : ", divProp);
let propName = divProp.firstChild.innerText;
console.log(" property : ", propName);
divProp.setAttribute("data-refs-self", propName);
switch (propName) {
case "cover-pic":
document
.querySelector(".page.relative > .relative .page-blocks-inner")
.classList.add("has-coverPic");
console.log(" .has-coverPic injected");
break;
case "cover-pic-height":
console.log(" .has-coverPic injected");
break; // TODO
}
});
propertiesBlocks[i].classList.add("datarefd");
}
};
const addPropDataRefThrottled = throttle(addPropDataRef, 1000);
const obsProps = new MutationObserver(addPropDataRefThrottled);
obsProps.observe(
watchTarget,
{
subtree: true,
childList: true,
}
);
// =====================================end of property data-refs
// add bg-pic =======================================
console.log("========= bg-pic v20220327 ============");
const addbgPic = function () {
console.log("addbgPic running...");
const bgPic = document.querySelectorAll(
"[data-refs-self='bg-pic']"
);
console.log("has bgpic : ", bgPic.length);
if (bgPic.length > 0) {
const bgPica = Array.from(bgPic).filter(
(item) => !item.closest(".references-blocks")
);
console.log("bg-pic exists : ", bgPica.length);
console.log("bg-pic parent : ", bgPica);
if (bgPica.length > 0) {
const bgPicUrl = bgPica[0].getElementsByTagName("img")[0].src;
console.log("bg-pic url : ", bgPicUrl);
document.getElementById(
"main-content-container"
).style.backgroundImage = "url(" + bgPicUrl + ")";
}
} else {
document.getElementById("main-content-container").removeAttribute("style");
};
};
const addbgPicThrottled = throttle(addbgPic, 1000);
const addbg = new MutationObserver(addbgPicThrottled);
addbg.observe(watchTarget, {
subtree: true,
childList: true,
});
// =====================================end of bg-pic
// ============ BETTER-SIDEBAR rotate closed tabs in right sidebar=========
// ============ remove if you don't use the better-sidebar.css=============
console.log("========= rsidebar fold 90° ============");
const foldTab = function () {
let foldedTab = document.querySelectorAll(
".sidebar-item.content > .flex.flex-col > .flex.flex-row"
);
if (foldedTab.length > 0) {
let foldedTabsArray = Array.from(foldedTab);
console.log("sidebar tabs : ", foldedTabsArray.length);
for (let i = 0; i < foldedTabsArray.length; i++) {
if (foldedTabsArray[i].nextElementSibling.classList.contains("hidden")) {
// console.log("fold detected: ", foldedTabsArray[i].nextElementSibling, " is folded.");
let tab = foldedTabsArray[i].closest(".sidebar-item.content");
tab.classList.add("folded");
} else {
if (
foldedTabsArray[i].nextElementSibling.classList.contains("initial") &&
foldedTabsArray[i]
.closest(".sidebar-item.content")
.classList.contains("folded")
) {
//console.log("this one is unfolded !!!");
let tab = foldedTabsArray[i].closest(".sidebar-item.content");
tab.classList.remove("folded");
}
}
}
}
};
const foldTabthrottled = throttle(foldTab, 300);
const foldTabs = new MutationObserver(foldTabthrottled);
const sidebarTarget = document.querySelector(".sidebar-item-list");
foldTabs.observe(watchTarget, {
subtree: true,
childList: true,
attributes: true,
});
// =================== end of rotate closed tabs in right sidebar =========
// ====== LS-TWITTER-EMBED =============================================
console.info('====== LS-TWITTER-EMBED ======');
// add twitter script and meta tags to head
var s = document.createElement("script");
s.type = "text/javascript";
s.src = "https://platform.twitter.com/widgets.js";
s.async = true;
var m = document.createElement("meta");
m.name = "twitter:widgets:theme";
m.content = "dark";
document.head.append(s, m);
function embedTwitter() {
let isTweet = document.querySelectorAll(
"a.external-link[href^='https://twitter.com"
);
for (let i = 0; i < isTweet.length; i++) {
if (isTweet[i].children[0] === undefined) {
var requestUrl =
"https://publish.twitter.com/oembed?omit_script=1&url=" +
isTweet[i].href + "&limit=8&theme=dark&maxwidth=550&maxheight=600";
var oReq = new XMLHttpRequest();
oReq.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var data = JSON.parse(this.responseText);
console.log(
'requestUrl : ', requestUrl,
'\noReq.response : ', oReq.response,
'\ndata : ', data,
'\ndata.html : ', data.html
);
insertResponse(data, i);
}
}
oReq.open("GET", requestUrl, true);
oReq.send();
}
}
function insertResponse(data, i) {
var insTw = document.createElement("div");
insTw.className = "twembed";
insTw.innerHTML = data.html;
isTweet[i].appendChild(insTw);
console.log("embedding Tweets...");
twttr.widgets.load();
}
};
const embTwthrottled = throttle(embedTwitter, 1000);
const embTw = new MutationObserver(embTwthrottled);
const embTwTarget = document.getElementById('main-container');
embTw.observe(embTwTarget, {
subtree: true,
childList: true,
});
// =============================================== end of twitter embed =