forked from stashapp/CommunityScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupDetails.js
More file actions
318 lines (278 loc) · 8.81 KB
/
GroupDetails.js
File metadata and controls
318 lines (278 loc) · 8.81 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
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
"use strict";
(function () {
var ROOT_ID = "root";
var ROUTE_PREFIX = "/groups";
var GROUP_METRICS_QUERY =
"query GroupDetailsMetrics($id: ID!) {" +
" findGroup(id: $id) {" +
" id " +
" scenes { " +
" id " +
" files { duration height } " +
" groups { group { id } scene_index } " +
" } " +
" }" +
"}";
var state = {
observer: null,
attachedRoot: null,
retryTimer: null,
applyingDomEnhancements: false,
cacheByGroupId: new Map(),
inFlightByGroupId: new Map(),
};
async function gql(query, variables) {
var res = await fetch("/graphql", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ query: query, variables: variables || {} }),
});
var j = await res.json();
if (j.errors && j.errors.length) {
throw new Error(
j.errors.map(function (e) {
return e.message;
}).join("; ")
);
}
return j.data;
}
function routeMatches() {
var p = window.location.pathname || "";
return p === ROUTE_PREFIX || p.indexOf(ROUTE_PREFIX + "/") === 0;
}
function parseGroupIdFromHref(href) {
if (!href) return null;
var match = String(href).match(/\/groups\/(\d+)/);
return match ? String(match[1]) : null;
}
function parseGroupIdFromCard(card) {
if (!card) return null;
var header = card.querySelector("a.group-card-header");
if (header && header.getAttribute("href")) {
return parseGroupIdFromHref(header.getAttribute("href"));
}
var firstLink = card.querySelector("a[href*='/groups/']");
if (firstLink && firstLink.getAttribute("href")) {
return parseGroupIdFromHref(firstLink.getAttribute("href"));
}
return null;
}
function sceneIndexForGroup(scene, groupId) {
var groups = (scene && scene.groups) || [];
var gid = String(groupId);
for (var i = 0; i < groups.length; i++) {
var g = groups[i];
if (g && g.group && String(g.group.id) === gid) return g.scene_index;
}
return undefined;
}
function isEligibleSceneIndex(idx) {
if (idx == null) return true;
var n = Number(idx);
return Number.isFinite(n) && n >= 0 && n <= 89;
}
function getSceneDurationSeconds(scene) {
var files = (scene && scene.files) || [];
var maxDur = 0;
for (var i = 0; i < files.length; i++) {
var dur = Number(files[i] && files[i].duration);
if (Number.isFinite(dur) && dur > maxDur) maxDur = dur;
}
return maxDur;
}
function getSceneVerticalPixels(scene) {
var files = (scene && scene.files) || [];
var maxHeight = 0;
for (var i = 0; i < files.length; i++) {
var h = Number(files[i] && files[i].height);
if (Number.isFinite(h) && h > maxHeight) maxHeight = h;
}
return maxHeight;
}
function computeMetrics(groupId, scenes) {
var totalDurationSec = 0;
var verticalSum = 0;
var verticalCount = 0;
var list = scenes || [];
for (var i = 0; i < list.length; i++) {
var scene = list[i];
var idx = sceneIndexForGroup(scene, groupId);
if (!isEligibleSceneIndex(idx)) continue;
var duration = getSceneDurationSeconds(scene);
totalDurationSec += duration;
if (duration > 600) {
var height = getSceneVerticalPixels(scene);
if (height > 0) {
verticalSum += height;
verticalCount += 1;
}
}
}
return {
totalDurationSec: Math.round(totalDurationSec),
averageVerticalPixels:
verticalCount > 0 ? Math.round(verticalSum / verticalCount) : null,
verticalSampleCount: verticalCount,
};
}
async function fetchMetricsForGroup(groupId) {
var data = await gql(GROUP_METRICS_QUERY, { id: String(groupId) });
var group = data && data.findGroup;
return computeMetrics(groupId, (group && group.scenes) || []);
}
async function getMetricsForGroup(groupId) {
if (!groupId) return null;
var gid = String(groupId);
if (state.cacheByGroupId.has(gid)) return state.cacheByGroupId.get(gid);
if (state.inFlightByGroupId.has(gid)) return state.inFlightByGroupId.get(gid);
var p = fetchMetricsForGroup(gid)
.then(function (metrics) {
state.cacheByGroupId.set(gid, metrics);
state.inFlightByGroupId.delete(gid);
return metrics;
})
.catch(function (e) {
state.inFlightByGroupId.delete(gid);
throw e;
});
state.inFlightByGroupId.set(gid, p);
return p;
}
function formatDuration(totalSeconds) {
var s = Math.max(0, Math.round(Number(totalSeconds) || 0));
var hrs = Math.floor(s / 3600);
var mins = Math.floor((s % 3600) / 60);
var secs = s % 60;
return hrs + ":" + String(mins).padStart(2, "0") + ":" + String(secs).padStart(2, "0");
}
function formatVerticalPixels(avgHeight) {
var n = Number(avgHeight);
if (!Number.isFinite(n) || n <= 0) return "n/a";
return String(Math.round(n)) + "p";
}
function buildStatNode(id, text, title) {
var el = document.createElement("span");
el.id = id;
el.className = "gd-stat";
el.textContent = text;
if (title) el.title = title;
return el;
}
function injectMetricsIntoCard(card, metrics) {
if (!card || !metrics) return;
var popovers = card.querySelector(".card-popovers");
if (!popovers) return;
var sceneCount = popovers.querySelector(".scene-count");
if (!sceneCount) return;
var oldLeft = popovers.querySelector(".gd-stat-left");
if (oldLeft && oldLeft.parentNode) oldLeft.parentNode.removeChild(oldLeft);
var oldRight = popovers.querySelector(".gd-stat-right");
if (oldRight && oldRight.parentNode) oldRight.parentNode.removeChild(oldRight);
var durationNode = buildStatNode(
"gd-stat-left-" + Date.now(),
formatDuration(metrics.totalDurationSec),
"Total duration for scenes with scene_index null or 0..89"
);
durationNode.classList.add("gd-stat-left");
var resolutionNode = buildStatNode(
"gd-stat-right-" + Date.now(),
formatVerticalPixels(metrics.averageVerticalPixels),
"Average vertical resolution for those scenes with duration > 600s"
);
resolutionNode.classList.add("gd-stat-right");
popovers.insertBefore(durationNode, sceneCount);
if (sceneCount.nextSibling) popovers.insertBefore(resolutionNode, sceneCount.nextSibling);
else popovers.appendChild(resolutionNode);
}
async function decorateGroupCard(card) {
var groupId = parseGroupIdFromCard(card);
if (!groupId) return;
var metrics = await getMetricsForGroup(groupId);
injectMetricsIntoCard(card, metrics);
}
function applyDomEnhancements() {
if (state.applyingDomEnhancements) return;
state.applyingDomEnhancements = true;
var cards = Array.prototype.slice.call(
document.querySelectorAll("div.group-card")
);
Promise.all(
cards.map(function (card) {
return decorateGroupCard(card).catch(function () {
// Ignore per-card failures so one bad response does not block others.
});
})
).finally(function () {
state.applyingDomEnhancements = false;
});
}
function detachObserver() {
if (state.observer) {
state.observer.disconnect();
state.observer = null;
}
state.attachedRoot = null;
}
function clearRetryTimer() {
if (state.retryTimer) {
clearInterval(state.retryTimer);
state.retryTimer = null;
}
}
function attach() {
if (!routeMatches()) {
detachObserver();
return false;
}
var root = document.getElementById(ROOT_ID);
if (!root) return false;
if (state.attachedRoot === root && state.observer) {
applyDomEnhancements();
return true;
}
detachObserver();
state.cacheByGroupId.clear();
state.inFlightByGroupId.clear();
var obs = new MutationObserver(function () {
applyDomEnhancements();
});
obs.observe(root, { childList: true, subtree: true });
state.observer = obs;
state.attachedRoot = root;
applyDomEnhancements();
return true;
}
function scheduleAttachRetries() {
clearRetryTimer();
state.retryTimer = setInterval(function () {
try {
if (!routeMatches()) {
detachObserver();
return;
}
if (attach()) clearRetryTimer();
} catch (e) {
// Ignore transient route/render timing errors.
}
}, 500);
setTimeout(clearRetryTimer, 60000);
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", function () {
attach();
scheduleAttachRetries();
});
} else {
attach();
scheduleAttachRetries();
}
window.addEventListener("popstate", function () {
attach();
scheduleAttachRetries();
});
window.addEventListener("hashchange", function () {
attach();
scheduleAttachRetries();
});
})();