Repository navigation
Expand file tree
/
Copy pathfourchan.js
More file actions
155 lines (141 loc) · 6.24 KB
/
Copy pathfourchan.js
File metadata and controls
155 lines (141 loc) · 6.24 KB
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
window.ApacheVisualizer = window.ApacheVisualizer || {};
(function(AV) {
AV.is4chan = window.location.hostname.includes('4chan.org') || window.location.hostname.includes('4channel.org');
var isSingleThreadUrl = window.location.pathname.includes('/thread/');
AV.isThreadedPage = document.querySelectorAll('.thread').length > 0 && !isSingleThreadUrl;
AV.extract4chanThreads = function(uniqueLinks, rootEl) {
var root = rootEl || document;
var threads = [];
var threadElements = root.querySelectorAll('.thread');
var globalSeen = new Set();
for (var t = 0; t < threadElements.length; t++) {
var threadEl = threadElements[t];
var tId = threadEl.getAttribute('id') || ('t' + t);
var threadUrl = '';
var metaUrl = threadEl.querySelector('meta[itemprop="url"]');
if (metaUrl) {
threadUrl = metaUrl.getAttribute('content');
if (threadUrl) {
try {
var tempUrl = new URL(threadUrl.startsWith('//') ? 'https:' + threadUrl : threadUrl);
tempUrl.searchParams.set('visualizer', 'true');
threadUrl = tempUrl.toString();
} catch (e) {
if (threadUrl.includes('?')) {
threadUrl += '&visualizer=true';
} else {
threadUrl += '?visualizer=true';
}
}
}
}
var title = '';
var subjectEl = threadEl.querySelector('.op .subject');
if (subjectEl && subjectEl.textContent.trim()) {
title = subjectEl.textContent.trim();
} else {
var messageEl = threadEl.querySelector('.op .postMessage');
if (messageEl && messageEl.textContent.trim()) {
var text = messageEl.textContent.trim();
title = text.length > 50 ? text.substring(0, 50) + '...' : text;
} else {
title = 'Thread #' + tId.replace('t', '');
}
}
// Extract posts
var postContainers = threadEl.querySelectorAll('.postContainer');
var threadPosts = [];
var threadLinksCount = 0;
for (var p = 0; p < postContainers.length; p++) {
var container = postContainers[p];
var postEl = container.querySelector('.post');
var pId = postEl ? postEl.getAttribute('id') : ('p-' + p);
var msgEl = container.querySelector('.postMessage');
var textHTML = msgEl ? msgEl.innerHTML.trim() : '';
var fileUrl = null;
var thumbUrl = null;
var fileEl = container.querySelector('.file');
if (fileEl) {
var fileAnchor = fileEl.querySelector('a.fileThumb');
if (fileAnchor) {
fileUrl = fileAnchor.getAttribute('href');
if (fileUrl) {
if (fileUrl.startsWith('//')) {
fileUrl = 'https:' + fileUrl;
}
globalSeen.add(fileUrl);
threadLinksCount++;
}
var fileImg = fileAnchor.querySelector('img');
if (fileImg) {
thumbUrl = fileImg.getAttribute('src');
if (thumbUrl && thumbUrl.startsWith('//')) {
thumbUrl = 'https:' + thumbUrl;
}
}
}
}
// Add the post (either file post or text post)
if (fileUrl || textHTML) {
threadPosts.push({
id: pId,
file: fileUrl,
thumbnail: thumbUrl,
text: textHTML,
isOp: container.classList.contains('opContainer')
});
}
}
// Calculate totalFiles
var totalFiles = threadLinksCount;
var summaryEl = threadEl.querySelector('.summary.desktop span');
if (summaryEl) {
var omitMatch = summaryEl.textContent.match(/(\d+)\s+image/i);
if (omitMatch) {
totalFiles += parseInt(omitMatch[1], 10);
}
} else {
var infoEl = threadEl.querySelector('.postLink.mobile .info');
if (infoEl) {
var infoMatch = infoEl.textContent.match(/(\d+)\s+Image/i);
if (infoMatch) {
totalFiles = parseInt(infoMatch[1], 10);
}
}
}
if (threadPosts.length > 0) {
threads.push({
id: tId,
title: title,
url: threadUrl,
totalFiles: totalFiles,
posts: threadPosts,
linksCount: threadLinksCount
});
}
}
// Safety: Collect any media files that were not part of any thread element
var otherPosts = [];
for (var i = 0; i < uniqueLinks.length; i++) {
var link = uniqueLinks[i];
if ((AV.isImage(link) || AV.isVideo(link) || AV.isAudio(link) || AV.isDocument(link)) && !globalSeen.has(link)) {
otherPosts.push({
id: 'other-' + i,
file: link,
thumbnail: link,
text: '',
isOp: false
});
}
}
if (otherPosts.length > 0) {
threads.push({
id: 'other-files',
title: 'Other Files',
posts: otherPosts,
linksCount: otherPosts.length
});
}
return threads;
};
})(window.ApacheVisualizer);