Repository navigation
Expand file tree
/
Copy pathcontentScript.js
More file actions
265 lines (224 loc) · 7.43 KB
/
Copy pathcontentScript.js
File metadata and controls
265 lines (224 loc) · 7.43 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
"use strict";
(() => {
const CLICK_INTERVAL_MS = 100;
const FILES_CONTAINER_POLL_MS = 500;
const FILES_CONTAINER_POLL_TIMEOUT_MS = 10000;
const RETRY_SCAN_DELAY_MS = 200;
let processed = new WeakSet();
let retryCount = new WeakMap();
const MAX_RETRIES = 10;
let clickQueue = [];
let isProcessingQueue = false;
let retryScanTimerId = null;
let drainTimerId = null;
// ---- Migration ----
const migrateStorage = (callback) => {
chrome.storage.sync.get(null, (data) => {
if (data.schemaVersion >= 1) {
callback();
return;
}
const migrated = {
schemaVersion: 1,
globalPatterns: data.patterns || [],
repoPatterns: data.repoPatterns || {},
};
chrome.storage.sync.remove("patterns", () => {
chrome.storage.sync.set(migrated, callback);
});
});
};
// ---- Rule matching ----
let activePatterns = [];
const getOwnerRepo = () => {
const match = location.pathname.match(/^\/([^/]+\/[^/]+)/);
return match ? match[1] : null;
};
const loadPatterns = (callback) => {
migrateStorage(() => {
chrome.storage.sync.get({ globalPatterns: [], repoPatterns: {} }, (result) => {
const ownerRepo = getOwnerRepo();
const repoSpecific = ownerRepo ? (result.repoPatterns[ownerRepo] || []) : [];
activePatterns = [...result.globalPatterns, ...repoSpecific];
if (callback) callback();
});
});
};
chrome.storage.onChanged.addListener((changes) => {
if (changes.globalPatterns || changes.repoPatterns) {
loadPatterns();
}
});
// ---- Click queue (throttled) ----
const enqueueClick = (el) => {
clickQueue.push(el);
if (!isProcessingQueue) drainQueue();
};
const drainQueue = () => {
isProcessingQueue = true;
const next = clickQueue.shift();
if (!next) {
isProcessingQueue = false;
return;
}
try {
next.click();
} catch (e) {
console.debug("[auto-viewed] click failed:", e);
}
drainTimerId = setTimeout(drainQueue, CLICK_INTERVAL_MS);
};
// ---- Core scan ----
/**
* Collect all diff-block elements.
* New UI: CSS-module class containing "Diff-module__diff__"
* Legacy: .js-file
*/
const getDiffElements = () => {
// New UI (2025+)
const newEls = document.querySelectorAll('[class*="Diff-module__diff__"]');
if (newEls.length > 0) return newEls;
// Legacy UI
return document.querySelectorAll(".js-file");
};
const scanFiles = () => {
// Cancel any pending retry to debounce
if (retryScanTimerId !== null) {
clearTimeout(retryScanTimerId);
retryScanTimerId = null;
}
const fileEls = getDiffElements();
if (fileEls.length === 0 && isFilesPage()) {
console.warn(
"[auto-viewed] No diff containers found on Files page — selectors may be outdated."
);
return;
}
const { clickTargets, marked, pendingRetry } = AutoViewed.scanFilesLogic(
fileEls, processed, retryCount, activePatterns, MAX_RETRIES
);
for (const toggle of clickTargets) {
enqueueClick(toggle);
}
if (marked > 0) {
console.debug(`[auto-viewed] Queued ${marked} file(s) to mark as Viewed.`);
}
if (pendingRetry) {
retryScanTimerId = setTimeout(scanFiles, RETRY_SCAN_DELAY_MS);
}
};
// ---- URL helpers ----
/** Match both legacy /files and new /changes URL. */
const isFilesPage = () =>
/\/pull\/\d+\/(files|changes)/.test(location.pathname);
// ---- MutationObserver ----
let observer = null;
/**
* Find the container that holds all diff blocks and observe it.
* New UI: the <div class="d-flex flex-column gap-3"> that wraps all diffs,
* or fall back to the PageLayout content wrapper.
* Legacy: #files
*/
const findDiffListContainer = () => {
// Legacy
const legacy = document.getElementById("files");
if (legacy) return legacy;
// New UI: find the parent of the first diff block
const firstDiff = document.querySelector('[class*="Diff-module__diff__"]');
if (firstDiff) {
// Walk up to the list container (the one with many diff children)
let parent = firstDiff.parentElement;
while (parent) {
if (parent.children.length > 1) return parent;
parent = parent.parentElement;
}
}
// Fallback: PageLayout content area
return document.querySelector('[class*="prc-PageLayout-Content-"]');
};
const observeFilesContainer = () => {
if (observer) {
observer.disconnect();
observer = null;
}
const target = findDiffListContainer();
if (!target) return false;
observer = new MutationObserver(() => scanFiles());
observer.observe(target, { childList: true, subtree: true });
return true;
};
// Wait for diff container to appear (lazy-rendered by GitHub)
const waitForFilesContainer = () => {
if (observeFilesContainer()) return;
let elapsed = 0;
const interval = setInterval(() => {
elapsed += FILES_CONTAINER_POLL_MS;
if (observeFilesContainer() || elapsed >= FILES_CONTAINER_POLL_TIMEOUT_MS) {
clearInterval(interval);
if (elapsed >= FILES_CONTAINER_POLL_TIMEOUT_MS) {
console.warn("[auto-viewed] Diff container not found within timeout.");
}
}
}, FILES_CONTAINER_POLL_MS);
};
// ---- Initialization per page ----
const init = () => {
if (!isFilesPage()) return;
console.debug("[auto-viewed] Activated on", location.href);
// Cancel pending timers to prevent leaks on SPA navigation
if (retryScanTimerId !== null) { clearTimeout(retryScanTimerId); retryScanTimerId = null; }
if (drainTimerId !== null) { clearTimeout(drainTimerId); drainTimerId = null; }
// Reset all state for SPA navigations
clickQueue = [];
isProcessingQueue = false;
processed = new WeakSet();
retryCount = new WeakMap();
loadPatterns(() => {
scanFiles();
waitForFilesContainer();
});
};
// ---- SPA navigation support ----
// GitHub Turbo / pjax (legacy)
document.addEventListener("turbo:load", () => init());
document.addEventListener("pjax:end", () => init());
// New React-based UI uses History API
let deferredTimerId = null;
let deferredGeneration = 0;
const deferredInit = () => {
if (deferredTimerId) clearTimeout(deferredTimerId);
if (!isFilesPage()) return;
const gen = ++deferredGeneration;
let attempts = 0;
const maxAttempts = 20; // 100ms * 20 = 2s max
const tryInit = () => {
if (gen !== deferredGeneration) return; // stale chain — abort
attempts++;
if (getDiffElements().length > 0) {
init();
return;
}
if (attempts >= maxAttempts) {
console.debug("[auto-viewed] deferredInit timed out — no diff elements found.");
return;
}
deferredTimerId = setTimeout(tryInit, 100);
};
tryInit();
};
window.addEventListener("popstate", () => deferredInit());
// URL polling: MV3 content scripts run in an isolated world, so
// history.pushState/replaceState interception does NOT reach the main
// page's JS. GitHub's new React UI also uses the Navigation API which
// bypasses pushState entirely. Polling the URL is the most reliable
// cross-world detection method.
let lastUrl = location.href;
let urlPollTimerId = setInterval(() => {
if (location.href !== lastUrl) {
lastUrl = location.href;
deferredInit();
}
}, 300);
// Initial run
init();
})();