-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
243 lines (191 loc) · 6.92 KB
/
content.js
File metadata and controls
243 lines (191 loc) · 6.92 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
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
function extractTimestampFromId(postId) {
try {
const id = BigInt(postId);
const timestampMs = id >> 22n;
const date = new Date(Number(timestampMs));
return date;
} catch (error) {
return null;
}
}
function getRelativeTime(date) {
const now = new Date();
const diffMs = now - date;
const diffSec = Math.floor(diffMs / 1000);
const diffMin = Math.floor(diffSec / 60);
const diffHour = Math.floor(diffMin / 60);
const diffDay = Math.floor(diffHour / 24);
const diffWeek = Math.floor(diffDay / 7);
const diffMonth = Math.floor(diffDay / 30);
const diffYear = Math.floor(diffDay / 365);
if (diffSec < 60) return `${diffSec} second${diffSec !== 1 ? "s" : ""} ago`;
if (diffMin < 60) return `${diffMin} minute${diffMin !== 1 ? "s" : ""} ago`;
if (diffHour < 24) return `${diffHour} hour${diffHour !== 1 ? "s" : ""} ago`;
if (diffDay < 7) return `${diffDay} day${diffDay !== 1 ? "s" : ""} ago`;
if (diffWeek < 4) return `${diffWeek} week${diffWeek !== 1 ? "s" : ""} ago`;
if (diffMonth < 12) return `${diffMonth} month${diffMonth !== 1 ? "s" : ""} ago`;
return `${diffYear} year${diffYear !== 1 ? "s" : ""} ago`;
}
function getTimeDifference(date1, date2) {
const diffMs = Math.abs(date2 - date1);
const diffSec = Math.floor(diffMs / 1000);
const diffMin = Math.floor(diffSec / 60);
const diffHour = Math.floor(diffMin / 60);
const diffDay = Math.floor(diffHour / 24);
if (diffSec < 60) return `${diffSec} second${diffSec !== 1 ? "s" : ""}`;
if (diffMin < 60) return `${diffMin} minute${diffMin !== 1 ? "s" : ""}`;
if (diffHour < 24) return `${diffHour} hour${diffHour !== 1 ? "s" : ""}`;
return `${diffDay} day${diffDay !== 1 ? "s" : ""}`;
}
function detectLocale() {
const browserLang = navigator.language || navigator.userLanguage;
const browserLangs = navigator.languages || [browserLang];
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const hungarianTimezones = [
"Europe/Budapest",
"Europe/Prague",
"Europe/Bratislava",
"Europe/Belgrade",
"Europe/Ljubljana",
"Europe/Sarajevo",
"Europe/Zagreb",
];
if (hungarianTimezones.includes(timezone)) {
return "hu-HU";
}
for (const lang of browserLangs) {
if (lang.startsWith("hu")) {
return "hu-HU";
}
}
return browserLang || "en-US";
}
function formatDate(date, postDate = null) {
if (!date) return null;
const detectedLocale = detectLocale();
const localFormat = date.toLocaleString(detectedLocale, {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
});
const relativeTime = getRelativeTime(date);
let result = `${localFormat} (${relativeTime})`;
if (postDate && date > postDate) {
const timeDiff = getTimeDifference(postDate, date);
result += ` [${timeDiff} after post]`;
}
return result;
}
function processPost(post) {
if (post.dataset.timestampProcessed) return;
const urn = post.getAttribute("data-urn");
if (!urn || !urn.includes("activity:")) return;
const postId = urn.split(":").pop();
const exactDate = extractTimestampFromId(postId);
if (!exactDate) return;
const formattedDate = formatDate(exactDate);
const timeSelectors = [
".feed-shared-actor__sub-description",
".update-components-actor__sub-description",
"time[datetime]",
"span.update-components-actor__sub-description",
];
timeSelectors.forEach((selector) => {
const timeElems = post.querySelectorAll(selector);
timeElems.forEach((timeElem) => {
if (timeElem.dataset.exactTime) return;
const originalText = timeElem.textContent.trim();
const timePatterns = [
/\d+[smhd]\s*(?:ago)?/i,
/\d+\s*(?:second|minute|hour|day|week|month|year)s?\s*ago/i,
/\d+[smhdwy]/i,
/^\s*\d+[smhd]\s*•/,
];
const isTimeElement = timePatterns.some((pattern) => pattern.test(originalText));
if (isTimeElement && originalText.length < 100) {
timeElem.textContent = formattedDate;
timeElem.title = `Exact time: ${formattedDate} (Original: ${originalText})`;
timeElem.setAttribute("aria-label", `Posted on ${formattedDate}`);
timeElem.dataset.exactTime = "true";
}
});
});
post.dataset.timestampProcessed = "true";
post.dataset.postTimestamp = exactDate.getTime();
}
function processComment(comment, postTimestamp = null) {
if (comment.dataset.commentProcessed) return;
const dataId = comment.getAttribute("data-id");
if (!dataId) return;
let commentId = null;
if (dataId.includes("comment:")) {
const match = dataId.match(/comment:\([^,]+,(\d+)\)/);
if (match) {
commentId = match[1];
}
}
if (!commentId || !/^\d+$/.test(commentId)) return;
const commentDate = extractTimestampFromId(commentId);
if (!commentDate) return;
const postDate = postTimestamp ? new Date(parseInt(postTimestamp)) : null;
const formattedDate = formatDate(commentDate, postDate);
const timeElems = comment.querySelectorAll("time.comments-comment-meta__data");
timeElems.forEach((timeElem) => {
if (timeElem.dataset.exactTime) return;
const originalText = timeElem.textContent.trim();
const timePatterns = [/^\d+[smhd]$/i, /^\d+\s*(?:second|minute|hour|day|week|month|year)s?$/i, /^\d+[smhdwy]$/i];
const isTimeElement = timePatterns.some((pattern) => pattern.test(originalText));
if (isTimeElement) {
timeElem.textContent = formattedDate;
timeElem.title = `Exact time: ${formattedDate} (Original: ${originalText})`;
timeElem.setAttribute("aria-label", `Comment posted on ${formattedDate}`);
timeElem.dataset.exactTime = "true";
}
});
comment.dataset.commentProcessed = "true";
}
function processComments() {
const commentSelectors = [
"article.comments-comment-entity",
"article.comments-comment-entity--reply",
"[data-id*='comment']",
];
commentSelectors.forEach((selector) => {
document.querySelectorAll(selector).forEach((comment) => {
const parentPost = comment.closest("[data-urn*='activity']");
const postTimestamp = parentPost?.dataset?.postTimestamp || null;
processComment(comment, postTimestamp);
});
});
}
function processPosts() {
const postSelectors = [".feed-shared-update-v2", ".update-v2", "[data-urn*='activity']"];
postSelectors.forEach((selector) => {
document.querySelectorAll(selector).forEach((post) => {
processPost(post);
});
});
}
function processAll() {
processPosts();
processComments();
}
/*
// Compliance Mode: Automatic DOM modification DISABLED to comply with LinkedIn ToS
// The following logic previously automated timestamp replacement in the feed.
// It is preserved here for reference but inactive.
const observer = new MutationObserver((mutations) => {
processAll();
});
observer.observe(document.body, {
childList: true,
subtree: true
});
window.addEventListener("load", () => {
setTimeout(processAll, 1000);
});
processAll();
*/