-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
190 lines (157 loc) · 5.96 KB
/
Copy pathcontent.js
File metadata and controls
190 lines (157 loc) · 5.96 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
// content.js — injected into github.com/*/pull/* pages
// ---------------------------------------------------------------------------
// PR info from URL
// ---------------------------------------------------------------------------
function parsePRFromURL(url) {
const match = url.match(/github\.com\/([^/]+)\/([^/]+)\/pull\/(\d+)/);
if (!match) return null;
return { org: match[1], repo: match[2], prNumber: parseInt(match[3], 10) };
}
// ---------------------------------------------------------------------------
// Cleanup
// ---------------------------------------------------------------------------
function cleanup() {
document.getElementById('codacy-preview-btn')?.remove();
document.getElementById('codacy-panel')?.remove();
document.getElementById('codacy-error-toast')?.remove();
}
// ---------------------------------------------------------------------------
// Button injection
// ---------------------------------------------------------------------------
function injectButton(prInfo) {
if (document.getElementById('codacy-preview-btn')) return;
const btn = document.createElement('button');
btn.id = 'codacy-preview-btn';
btn.className = 'codacy-preview-btn';
btn.textContent = 'Preview Codacy Summary';
document.body.appendChild(btn);
btn.addEventListener('click', () => handlePreviewClick(prInfo, btn));
}
// ---------------------------------------------------------------------------
// Main click handler
// ---------------------------------------------------------------------------
async function handlePreviewClick(prInfo, btn) {
// If panel already open, just toggle it closed
const existing = document.getElementById('codacy-panel');
if (existing) {
existing.remove();
btn.textContent = 'Preview Codacy Summary';
return;
}
btn.textContent = 'Loading…';
btn.disabled = true;
try {
const response = await chrome.runtime.sendMessage({
type: 'FETCH_CODACY_PR',
payload: prInfo,
});
if (response.error) {
showError(response.error);
return;
}
const markdown = generateMarkdown(response);
if (!markdown) {
showError('Could not generate summary — check the service worker console for the raw API response.');
return;
}
showPanel(markdown);
btn.textContent = 'Close Preview';
} catch (err) {
showError('Extension error: ' + err.message);
} finally {
btn.disabled = false;
}
}
// ---------------------------------------------------------------------------
// Copy panel
// ---------------------------------------------------------------------------
function showPanel(markdown) {
const panel = document.createElement('div');
panel.id = 'codacy-panel';
panel.className = 'codacy-panel';
panel.innerHTML = `
<div class="codacy-panel-header">
<span class="codacy-panel-title">Codacy Summary Preview</span>
<div class="codacy-panel-actions">
<button class="codacy-copy-btn" id="codacy-copy-btn">Copy markdown</button>
<button class="codacy-close-btn" id="codacy-close-btn">✕</button>
</div>
</div>
<div class="codacy-panel-body">
<textarea class="codacy-markdown-output" id="codacy-markdown-output" readonly spellcheck="false">${escapeForHtml(markdown)}</textarea>
</div>
<div class="codacy-panel-footer">
Copy the markdown above and paste it as a comment on this PR.
</div>
`;
document.body.appendChild(panel);
document.getElementById('codacy-copy-btn').addEventListener('click', () => {
copyToClipboard(markdown);
});
document.getElementById('codacy-close-btn').addEventListener('click', () => {
panel.remove();
const btn = document.getElementById('codacy-preview-btn');
if (btn) btn.textContent = 'Preview Codacy Summary';
});
}
async function copyToClipboard(text) {
const copyBtn = document.getElementById('codacy-copy-btn');
try {
await navigator.clipboard.writeText(text);
copyBtn.textContent = 'Copied! ✓';
copyBtn.classList.add('codacy-copy-btn--success');
setTimeout(() => {
copyBtn.textContent = 'Copy markdown';
copyBtn.classList.remove('codacy-copy-btn--success');
}, 2000);
} catch {
// Clipboard API unavailable — select the textarea so the user can copy manually
document.getElementById('codacy-markdown-output')?.select();
copyBtn.textContent = 'Select all & copy manually';
}
}
function escapeForHtml(str) {
return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
}
// ---------------------------------------------------------------------------
// Error toast
// ---------------------------------------------------------------------------
function showError(message) {
let toast = document.getElementById('codacy-error-toast');
if (!toast) {
toast = document.createElement('div');
toast.id = 'codacy-error-toast';
toast.className = 'codacy-error-toast';
document.body.appendChild(toast);
}
toast.textContent = message;
toast.style.display = 'block';
clearTimeout(toast._timer);
toast._timer = setTimeout(() => { toast.style.display = 'none'; }, 8000);
const btn = document.getElementById('codacy-preview-btn');
if (btn) btn.textContent = 'Preview Codacy Summary';
}
// ---------------------------------------------------------------------------
// Initialization + SPA navigation
// ---------------------------------------------------------------------------
function initialize() {
const prInfo = parsePRFromURL(location.href);
if (prInfo) {
injectButton(prInfo);
} else {
cleanup();
}
}
document.addEventListener('turbo:load', () => { cleanup(); initialize(); });
document.addEventListener('pjax:end', () => { cleanup(); initialize(); });
let lastUrl = location.href;
const urlObserver = new MutationObserver(() => {
if (location.href !== lastUrl) {
lastUrl = location.href;
cleanup();
initialize();
}
});
const titleEl = document.querySelector('title');
if (titleEl) urlObserver.observe(titleEl, { childList: true });
initialize();