-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontent.js
More file actions
96 lines (79 loc) · 2.63 KB
/
content.js
File metadata and controls
96 lines (79 loc) · 2.63 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
const CODERABBIT_USERNAME = "coderabbitai";
const SEVERITY_PATTERNS = {
nitpick: /\[nitpick\]/i,
suggestion: /\[suggestion\]/i,
warning: /\[warning\]/i,
issue: /\[issue\]/i,
};
let currentSettings = { nitpick: true, suggestion: true, warning: true, issue: true };
function isCodeRabbitComment(commentEl) {
const authorEl =
commentEl.querySelector(".author") ||
commentEl.querySelector("a.Link--primary");
if (!authorEl) return false;
return authorEl.textContent.trim().toLowerCase() === CODERABBIT_USERNAME;
}
function detectSeverity(commentEl) {
const body =
commentEl.querySelector(".comment-body") ||
commentEl.querySelector(".review-comment-body") ||
commentEl.querySelector("td.comment-body");
if (!body) return null;
const text = body.textContent;
for (const [severity, pattern] of Object.entries(SEVERITY_PATTERNS)) {
if (pattern.test(text)) return severity;
}
return null;
}
function getCommentElements() {
return document.querySelectorAll(
".timeline-comment, .review-comment, .js-comment, [data-body-version]"
);
}
function applyFilters() {
let hiddenCount = 0;
getCommentElements().forEach((el) => {
if (!isCodeRabbitComment(el)) return;
const severity = detectSeverity(el);
if (!severity) return;
const shouldShow = currentSettings[severity] !== false;
// Walk up to the nearest hideable container (inline comment thread, timeline item, etc.)
const container = el.closest(".js-timeline-item") || el.closest(".js-resolvable-timeline-thread-container") || el;
if (shouldShow) {
container.style.display = "";
container.classList.remove("coderabbit-hidden");
} else {
container.style.display = "none";
container.classList.add("coderabbit-hidden");
hiddenCount++;
}
});
return hiddenCount;
}
// Listen for settings changes from popup
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
if (message.type === "UPDATE_SEVERITY_FILTER") {
currentSettings = message.settings;
applyFilters();
sendResponse({ ok: true });
} else if (message.type === "GET_HIDDEN_COUNT") {
const hiddenCount = applyFilters();
sendResponse({ hiddenCount });
}
return true;
});
// Load saved settings on page load
chrome.storage.sync.get("severitySettings", (result) => {
if (result.severitySettings) {
currentSettings = result.severitySettings;
}
applyFilters();
});
// Re-apply when GitHub dynamically loads content (SPA navigation, lazy-loaded comments)
const observer = new MutationObserver(() => {
applyFilters();
});
observer.observe(document.body, {
childList: true,
subtree: true,
});