-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
244 lines (205 loc) · 5.91 KB
/
content.js
File metadata and controls
244 lines (205 loc) · 5.91 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
(() => {
const BUTTON_ID = "gh-shallow-clone-btn";
const STYLE_ID = "gh-shallow-clone-style";
const RESERVED_OWNERS = new Set([
"about",
"account",
"codespaces",
"collections",
"contact",
"customer-stories",
"enterprise",
"events",
"explore",
"features",
"gist",
"login",
"marketplace",
"mobile",
"new",
"notifications",
"orgs",
"organizations",
"pricing",
"pulls",
"search",
"security",
"sessions",
"settings",
"showcases",
"site",
"sponsors",
"team",
"teams",
"topics",
"trending"
]);
let lastHref = location.href;
let renderQueued = false;
function ensureStyle() {
if (document.getElementById(STYLE_ID)) return;
const style = document.createElement("style");
style.id = STYLE_ID;
style.textContent = `
#${BUTTON_ID} {
margin-left: 8px;
display: inline-flex;
align-items: center;
justify-content: center;
border: 1px solid var(--button-default-borderColor-rest, rgba(31, 35, 40, 0.15));
border-radius: 6px;
background: var(--button-default-bgColor-rest, #f6f8fa);
color: var(--fgColor-default, #24292f);
font-size: 12px;
font-weight: 500;
line-height: 20px;
padding: 5px 12px;
cursor: pointer;
white-space: nowrap;
}
#${BUTTON_ID}:hover {
background: var(--button-default-bgColor-hover, #f3f4f6);
}
`;
document.head.appendChild(style);
}
function getRepositoryFullName() {
const meta = document.querySelector(
'meta[name="octolytics-dimension-repository_nwo"]'
);
const nwo = meta?.getAttribute("content")?.trim();
if (nwo && nwo.includes("/")) return nwo;
const parts = location.pathname.split("/").filter(Boolean);
if (parts.length < 2) return null;
const owner = parts[0];
const repo = parts[1];
if (RESERVED_OWNERS.has(owner.toLowerCase())) return null;
return `${owner}/${repo}`;
}
function normalizedText(el) {
return el?.textContent?.replace(/\s+/g, " ").trim() || "";
}
function findModernCodeButton() {
const iconButton = document
.querySelector('main button[aria-haspopup="true"] svg.octicon-code')
?.closest("button");
if (iconButton) return iconButton;
const labelNodes = document.querySelectorAll(
'main button[aria-haspopup="true"] span[data-component="text"]'
);
for (const label of labelNodes) {
if (normalizedText(label) !== "Code") continue;
const button = label.closest("button");
if (button) return button;
}
return null;
}
function findLegacyCodeControl() {
const getRepo = document.querySelector("main get-repo");
if (getRepo) return getRepo;
const candidates = document.querySelectorAll(
'main summary[aria-haspopup="true"], main button[aria-haspopup="true"]'
);
for (const candidate of candidates) {
if (candidate.id === BUTTON_ID) continue;
if (normalizedText(candidate) !== "Code") continue;
return candidate.closest("details, get-repo") || candidate;
}
return null;
}
function findCodeAnchor() {
return findModernCodeButton() || findLegacyCodeControl();
}
function buildCloneCommand(repoFullName) {
return `git clone --depth=1 https://github.com/${repoFullName}.git`;
}
async function copyText(text) {
if (navigator.clipboard?.writeText) {
await navigator.clipboard.writeText(text);
return;
}
const textarea = document.createElement("textarea");
textarea.value = text;
textarea.style.position = "fixed";
textarea.style.opacity = "0";
document.body.appendChild(textarea);
textarea.select();
document.execCommand("copy");
textarea.remove();
}
function removeButton() {
const button = document.getElementById(BUTTON_ID);
if (button) button.remove();
}
function createButton() {
const button = document.createElement("button");
button.id = BUTTON_ID;
button.type = "button";
button.className = "btn btn-sm";
button.textContent = "Shallow clone";
return button;
}
function renderButton() {
const repoFullName = getRepositoryFullName();
const codeAnchor = findCodeAnchor();
if (!repoFullName || !codeAnchor || !codeAnchor.parentElement) {
removeButton();
return;
}
ensureStyle();
let button = document.getElementById(BUTTON_ID);
if (!button) {
button = createButton();
}
if (
button.parentElement !== codeAnchor.parentElement ||
button.previousElementSibling !== codeAnchor
) {
codeAnchor.insertAdjacentElement("afterend", button);
}
const command = buildCloneCommand(repoFullName);
button.title = command;
button.onclick = async () => {
const originalText = button.textContent;
try {
await copyText(command);
button.textContent = "Copied";
} catch (err) {
button.textContent = "Copy failed";
console.error("Failed to copy command:", err);
}
setTimeout(() => {
button.textContent = originalText;
}, 1200);
};
}
function queueRender() {
if (renderQueued) return;
renderQueued = true;
requestAnimationFrame(() => {
renderQueued = false;
renderButton();
});
}
function updateIfNeeded() {
if (location.href !== lastHref) {
lastHref = location.href;
}
queueRender();
}
const observer = new MutationObserver(() => {
if (location.href !== lastHref) {
lastHref = location.href;
}
queueRender();
});
observer.observe(document.documentElement, {
childList: true,
subtree: true
});
window.addEventListener("popstate", updateIfNeeded);
window.addEventListener("hashchange", updateIfNeeded);
document.addEventListener("pjax:end", updateIfNeeded);
document.addEventListener("turbo:render", updateIfNeeded);
updateIfNeeded();
})();