forked from npf-images-v3/npf-images-v3.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
355 lines (290 loc) · 12.6 KB
/
script.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*---------------------------------------------------------
NPF images fix v3.0 by @glenthemes [2021]
💌 git.io/JRBt7
Credits:
> wrap divs that are next to each other by Nick Craver
stackoverflow.com/a/3329249/8144506
> get 'deepest' element script by Balint Bako
stackoverflow.com/a/18652986/8144506
📍 v1.13.1 - 2023-09-20
🕒 Last updated: 2023-09-20 8:50PM [PDT]
---------------------------------------------------------*/
$(document).ready(function(){
// check jquery version
var jqver = jQuery.fn.jquery;
var ver = jqver.replaceAll(".","");
$(".npf_row .tmblr-full:not(:only-child)").each(function(){
if(!$(this).parent().is(".npf_col")){
$(this).wrap("<div class='npf_col'>")
}
})
// wrap any stray .tmblr-full in row container
$(".tmblr-full + .npf_row").each(function(){
let uw = $(this).prev();
if(!uw.parent().is(".npf_row")){
uw.wrap("<div class='npf_row' columns='1'>")
}
})
/*-------------------------------------------------*/
var spac = parseInt(getComputedStyle(document.documentElement).getPropertyValue("--NPF-Image-Spacing"));
$(".npf_row").each(function(){
if($(this).find(".npf_col").length){
// how many columns?
var cols = $(this).children(".npf_col").length;
$(this).attr("columns",cols);
cols = Number(cols);
// clarify the width of each column
var pognt = Math.floor(($(this).width() - (spac * (cols-1))) / cols);
$(this).children(".npf_col").attr("col-width",pognt)
}
})
/*-------------------------------------------------*/
// wrap .npf_rows that are next to each other
var npf_row = $(".npf_row");
for(var soda=0; soda<npf_row.length;){
if(ver < "180"){
soda += npf_row.eq(soda).nextUntil(':not(.npf_row)').andSelf().wrapAll('<div class="npf_inst">').length;
} else {
soda += npf_row.eq(soda).nextUntil(':not(.npf_row)').addBack().wrapAll('<div class="npf_inst">').length;
}
}
/*-------------------------------------------------*/
// multiple .tmblr-fulls that are next to each other,
// but are not in a row or container
// e.g. headers
$("*:not(.npf_row) > .tmblr-full").each(function(){
if($(this).siblings(".tmblr-full").length){
$(this).not(".tmblr-full + .tmblr-full").each(function(){
if(ver < "180"){
$(this).nextUntil(":not(.tmblr-full").andSelf().wrapAll('<div class="npf_inst">');
} else {
$(this).nextUntil(":not(.tmblr-full").addBack().wrapAll('<div class="npf_inst">');
}
});
}
})
// redo the .npf_inst wrapping
$(".npf_inst").each(function(){
$(this).not(".npf_inst + .npf_inst").each(function(){
if(ver < "180"){
$(this).nextUntil(":not(.npf_inst").andSelf().wrapAll('<div class="npf_inst">');
$(this).nextUntil(":not(.npf_inst").andSelf().children().unwrap();
} else {
$(this).nextUntil(":not(.npf_inst").addBack().wrapAll('<div class="npf_inst">');
$(this).nextUntil(":not(.npf_inst").addBack().children().unwrap();
}
});
})
// wrap single containerless .tmblr-fulls
$(".tmblr-full").each(function(){
if(!$(this).parents(".npf_inst").length){
if(!$(this).parents(".npf_row").length || !$(this).parents(".npf_col").length){
$(this).wrap("<div class='npf_inst'>")
}
}
})
/*-------------------------------------------------*/
// if: .tumblr_parent exists
$("[post-type='text']").each(function(){
$(this).find(".tumblr_parent").eq(0).each(function(){
$(this).find(".npf_inst").eq(0).each(function(){
if($.trim($(this).prev("p").text()) == ""){
$(this).addClass("photo-origin");
// relocate if there's a caption
if($(this).next().length){
$(this).insertBefore($(this).parents("[post-type='text']").find(".tumblr_parent").eq(0));
$(this).css("margin-bottom","var(--NPF-Caption-Spacing)")
} else {
$(this).addClass("npf-no-caption");
}
}
})
});
})
/*-------------------------------------------------*/
// .source-head do the thing - attempt #2
// yes there was an attempt #1 but it was too embarrassing
$("[post-type='text']").each(function(){
// target first commenter
var behead = $(this).find(".source-head").parent();
behead = behead.eq(0);
if(behead.find(".npf_inst").length){
var nuf = $(this).find(".npf_inst").eq(0);
if(nuf.prev().length){
if($.trim(nuf.prev().text()) == ""){
if(nuf.next().length){
nuf.addClass("photo-origin");
nuf.insertBefore(behead.children(".source-head"));
nuf.css("margin-bottom","var(--NPF-Caption-Spacing)")
} else {
nuf.addClass("npf-no-caption")
}
}
}
}
})
// catch any stray npfs that were meant to be included in set
$("[post-type='text']:has(.photo-origin)").each(function(){
var that = this;
// was "p + .npf_inst"
// changed it to "* + p + .npf_inst" bc:
// images that are NOT next to each other were being grouped
$(this).find("* + p + .npf_inst").each(function(){
if($.trim($(this).prev("p").text()) == ""){
if($(this).next().length){
$(this).addClass('recall')
$(this).appendTo($(that).find(".photo-origin"))
}
}
})
})
/*
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┇ ┇
┇ OLD BLOCKQUOTE CAPTIONS ┇
┇ ┇
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
*/
$("[post-type='text']").each(function(){
// [1/2] find the first reblogger name <p>
// technically it's the "last" in the reblog chain
// but bc of how multiple blockquotes are structured,
// the "last" reblogger name <p> would appear as the first instance
$(this).find("a.tumblr_blog[href]").each(function(){
let p = $(this).parent();
if(p.is("p")){
if(!$(this).closest("blockquote").length){
p.attr("last-comment","");
}
}
})
// [2/2] find the "deepest" blockquote,
// aka the OP's reblog trail/entry
var maxDepth = 0;
$(this).find("blockquote").each(function(){
$(this).attr('depth', $(this).parents().length);
if($(this).parents().length > maxDepth){
maxDepth = $(this).parents().length;
}
});
$('[depth="' + maxDepth + '"]').addClass("op-blockquote");
$("blockquote[depth]").removeAttr("depth")
});
// identify and relocate npf photosets where necessary
// (old blockquote capts)
$("[post-type='text'] .op-blockquote > .npf_inst:first-child").each(function(){
let postParent = $(this).parents("[post-type='text']");
let op_blockquote = $(this).parent(".op-blockquote");
let check_p = op_blockquote.prev("p");
if(check_p.length){
let check_a = check_p.find("a.tumblr_blog");
if(check_a.length){
check_p.addClass("PEE")
check_a.addClass("AYY")
/*---------------------------------------*/
// deal with captionless npf
if(!$(this).next().length){
$(this).addClass("photo-origin npf-no-caption");
// put npf photoset above reblog chain
postParent.find("p[last-comment]").before($(this));
// remove captionless npf photoset's blockquote border
op_blockquote.remove();
// put captionless npf post source AFTER the reblog chain
postParent.find("p[last-comment]").parent().append(check_p)
check_p.before(check_a);
check_p.remove();
check_a.wrap("<p class='npf-post-source'>(Source: </p>");
check_a.parent(".npf-post-source").append(")");
check_a.removeClass("tumblr_blog")
}
/*---------------------------------------*/
// deal with npf WITH caption
else {
let theNext = $(this).next();
let theContents = theNext.html().trim();
if(theContents !== ""){
$(this).addClass("photo-origin");
// put npf photoset above reblog chain
postParent.find("p[last-comment]").before($(this));
$(this).css("margin-bottom","var(--NPF-Caption-Spacing)");
}
// deal with captionless npf (part 2, just in case)
else {
$(this).addClass("photo-origin npf-no-caption");
// put npf photoset above reblog chain
postParent.find("p[last-comment]").before($(this));
// remove captionless npf photoset's blockquote border
op_blockquote.remove();
// put captionless npf post source AFTER the reblog chain
postParent.find("p[last-comment]").parent().append(check_p)
check_p.before(check_a);
check_p.remove();
check_a.wrap("<p class='npf-post-source'>(Source: </p>");
check_a.parent(".npf-post-source").append(")");
check_a.removeClass("tumblr_blog")
}
}
}
}
})
/*-------------------------------------------------*/
$(".npf_inst + p, p.tmblr-attribution").each(function(){
$(this).css("display","block")
$(this).css("margin-top","1em")
})
/*-------------------------------------------------*/
// initiate lightbox on images that didn't originally
// come with photo anchor
$(".tmblr-full img").click(function(){
if(!$(this).hasClass("post_media_photo")){
var imgsrc = $(this).attr("src");
Tumblr.Lightbox.init([{
low_res:imgsrc,
high_res:imgsrc
}]);
}
});
// do that thing again if npfs are inside npfs fsfr
$(".npf_inst .npf_inst").each(function(){
$(this).children().unwrap();
})
$(".npf_inst + .npf_inst").each(function(){
$(this).appendTo($(this).prev());
$(this).children().unwrap();
});
$(".npf_inst [list-order]").each(function(){
$(this).removeAttr("list-order");
})
/*-------------------------------------------------*/
$("[data-big-photo]").each(function(){
var gwgw = $(this).attr("data-big-photo");
var guppy = new Image();
guppy.src = gwgw;
})
});// end ready
document.addEventListener("DOMContentLoaded", () => {
// lightbox functionality
fetch("https://static.tumblr.com/gtjt4bo/nqts0jf5w/quick_tumblr_lightbox.js")
.then(response => response.text())
.then(getContents => {
let makeScript = document.createElement("script");
makeScript.textContent = getContents + "\n" + `quick_tumblr_lightbox(".npf_inst")`;
document.body.appendChild(makeScript)
});
// target stray NPFs that are supposed to be .photo-origin
// ideally: old blockquote captions
// example: glen-px.tumblr.com/post/666539156716093440
let targetStrayNPF = document.querySelectorAll("[post-type='text'] blockquote:not(.op-blockquote) > .npf_inst:first-child");
targetStrayNPF ? targetStrayNPF.forEach(tsNPF => {
let elp = tsNPF.parentNode.previousElementSibling;
if(elp.matches("p[last-comment]")){
if(elp.querySelector("a.tumblr_blog")){
if(!elp.previousElementSibling){
elp.parentNode.prepend(tsNPF);
tsNPF.classList.add("photo-origin");
tsNPF.style.marginBottom = "var(--NPF-Caption-Spacing)"
}
}
}
}) : ""
})