Repository navigation
Expand file tree
/
Copy pathrecent-changes.js
More file actions
221 lines (190 loc) · 7.42 KB
/
Copy pathrecent-changes.js
File metadata and controls
221 lines (190 loc) · 7.42 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
const LIMIT = 4;
const TTL_MS = 5 * 60 * 1000; // 5 minutes
const HTML_CACHE_KEY = "https://rc-cache.local/recentchanges.html";
export default {
async fetch(request, env, ctx) {
try {
const url = new URL(request.url);
if (request.method === "GET" && url.pathname === "/api/recent-changes") {
return handleRecentChanges(env);
}
return handleProxy(request, env, ctx);
} catch (err) {
return new Response("Worker error: " + (err?.stack || err?.message || String(err)), {
status: 500,
headers: { "Content-Type": "text/plain; charset=utf-8" },
});
}
},
};
/* ------------------------------------------------------------
MAIN (API endpoint)
------------------------------------------------------------ */
async function handleRecentChanges(env) {
const innerHtml = await getOrBuildWidgetInnerHtml(env);
if (!innerHtml) {
return new Response("Unable to build recent changes widget", { status: 500 });
}
return new Response(
`<ul class="vector-menu-content-list recent-changes-list">${innerHtml}</ul>`,
{
status: 200,
headers: {
"Content-Type": "text/html; charset=utf-8",
"Cache-Control": "no-store",
},
}
);
}
/* ------------------------------------------------------------
HTML REWRITE
------------------------------------------------------------ */
async function handleProxy(request, env, ctx) {
const originResponse = await fetch(request);
const contentType = originResponse.headers.get("Content-Type") || "";
if (!contentType.toLowerCase().includes("text/html")) {
return originResponse;
}
const widgetInnerHtml = await getOrBuildWidgetInnerHtml(env);
if (!widgetInnerHtml) {
return originResponse;
}
const rewriter = new HTMLRewriter()
.on("#p-Recent_changes ul.vector-menu-content-list", {
element(el) {
el.setAttribute("class", "vector-menu-content-list recent-changes-list");
el.setInnerContent(widgetInnerHtml, { html: true });
},
});
return rewriter.transform(originResponse);
}
/* ------------------------------------------------------------
GET OR BUILD WIDGET (5 min TTL)
------------------------------------------------------------ */
async function getOrBuildWidgetInnerHtml(env) {
const cache = await caches.open("recent-changes-cache");
const cacheKey = new Request(HTML_CACHE_KEY);
const htmlCached = await cache.match(cacheKey);
if (htmlCached) {
const cachedAt = Number(htmlCached.headers.get("X-Cached-At") || 0);
const age = Date.now() - cachedAt;
if (cachedAt && age < TTL_MS) {
return await htmlCached.text();
}
}
if (!env.MEDIAWIKI_API) {
return htmlCached ? await htmlCached.text() : null;
}
const apiUrl = buildApiUrl(env);
const upstream = await fetch(apiUrl);
if (!upstream.ok) {
if (htmlCached) {
return await htmlCached.text();
}
return null;
}
const data = await upstream.json();
const now = Date.now();
const innerHtml = renderWidgetItems(
data?.query?.recentchanges ?? []
);
await cache.put(
cacheKey,
new Response(innerHtml, {
headers: {
"Content-Type": "text/html; charset=utf-8",
"Cache-Control": "max-age=31536000",
"X-Cached-At": String(now),
},
})
);
return innerHtml;
}
/* ------------------------------------------------------------
MEDIAWIKI API
------------------------------------------------------------ */
function buildApiUrl(env) {
const api = new URL(env.MEDIAWIKI_API);
api.searchParams.set("action", "query");
api.searchParams.set("list", "recentchanges");
api.searchParams.set("rcprop", "title|timestamp|user|ids|flags");
api.searchParams.set("rctype", "edit|new");
api.searchParams.set("rcnamespace", "0");
api.searchParams.set("rclimit", String(LIMIT));
api.searchParams.set("format", "json");
return api.toString();
}
/* ------------------------------------------------------------
HTML RENDER
------------------------------------------------------------ */
function renderWidgetItems(items) {
let html = "";
for (let i = 0; i < items.length; i++) {
const item = items[i];
const id = "n-change" + (i + 1);
const title = String(item.title || "Unknown");
const userName = String(item.user || "Unknown");
const safeTitle = escapeHtml(title);
const safeUserName = escapeHtml(userName);
const timeText = escapeHtml(formatRelativeTime(item.timestamp));
const titleUrl = escapeHtml(getPageUrl(title));
const diffUrl = escapeHtml(getDiffUrl(item));
const userUrl = item.user ? escapeHtml(getContributionsUrl(item.user)) : null;
html += `<li class="mw-list-item recent-changes-item" id="${id}"><a class="recent-changes-title" href="${titleUrl}" title="${safeTitle}"><span>${safeTitle}</span></a><div class="recent-changes-meta"><a class="recent-changes-time" href="${diffUrl}" title="View diff for ${safeTitle}">${timeText}</a> · ${
item.user
? `<a class="recent-changes-user" href="${userUrl}">${safeUserName}</a>`
: `<span class="recent-changes-user">${safeUserName}</span>`
}</div></li>`;
}
html += `<li class="mw-list-item recent-changes-item" id="n-change-more"><a class="recent-changes-title" href="/Special:RecentChanges"><span>Show more...</span></a></li>`;
return html;
}
function getPageUrl(title) {
return "/" + String(title || "").replace(/ /g, "_");
}
function getContributionsUrl(user) {
return "/Special:Contributions/" + String(user || "").replace(/ /g, "_");
}
function getDiffUrl(item) {
if (item && item.revid) {
return "/Special:Diff/" + item.revid;
}
return "/Special:RecentChanges";
}
/* ------------------------------------------------------------
RELATIVE TIME
------------------------------------------------------------ */
function formatRelativeTime(timestamp) {
const then = new Date(timestamp).getTime();
const now = Date.now();
const diffSeconds = Math.round((now - then) / 1000);
const abs = Math.abs(diffSeconds);
let value;
let short;
if (abs < 60) {
short = "s";
value = diffSeconds;
} else if (abs < 3600) {
short = "m";
value = Math.round(diffSeconds / 60);
} else if (abs < 86400) {
short = "h";
value = Math.round(diffSeconds / 3600);
} else {
short = "d";
value = Math.round(diffSeconds / 86400);
}
const n = Math.abs(value);
return diffSeconds < 0 ? "in " + n + short : n + short + " ago";
}
/* ------------------------------------------------------------
ESCAPE
------------------------------------------------------------ */
function escapeHtml(str) {
return String(str)
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}