-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
156 lines (130 loc) · 3.66 KB
/
content.js
File metadata and controls
156 lines (130 loc) · 3.66 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
function initCherryPicker() {
// Check if we're on a GitHub page with commits
if (isCommitPage() || isPullRequestPage()) {
addCherryIcons();
// Add mutation observer to handle dynamically loaded content
observeChanges();
}
}
function isCommitPage() {
return (
window.location.pathname.includes("/commits/") ||
window.location.pathname.includes("/commit/")
);
}
function isPullRequestPage() {
return window.location.pathname.includes("/pull/");
}
function addCherryIcons() {
const commitElements = document.querySelectorAll(
'.TimelineItem-body a:not(.markdown-title)[href*="/commits/"]'
);
const prCommitElements = document.querySelectorAll(
'[data-testid="commit-row-item"] [aria-label="View commit details"]'
);
commitElements.forEach((commitElement) => {
if (!commitElement.querySelector(".cherry-pick-button")) {
const commitId = getCommitId(commitElement);
if (commitId) {
addCherryIconToElement(commitElement, commitId);
}
}
});
prCommitElements.forEach((commitElement) => {
if (!commitElement.parentElement.querySelector(".cherry-pick-button")) {
const commitId = commitElement.textContent.trim();
if (commitId) {
addCherryIconToElement(commitElement.parentElement, commitId);
}
}
});
}
function getCommitId(commitElement) {
const commitSha = commitElement.textContent;
if (commitSha) {
return commitSha.trim();
}
// Alternative: check for commit link
const commitLink = commitElement.href;
if (commitLink) {
const match = commitLink.match(/\/commit\/([a-f0-9]+)/);
if (match && match[1]) {
return match[1];
}
}
return null;
}
function addCherryIconToElement(element, commitId) {
const cherryButton = document.createElement("button");
cherryButton.className = "cherry-pick-button";
cherryButton.title = "Copy git cherry-pick command";
cherryButton.innerHTML = `<span class="cherry-icon">🍒</span>`;
// Add click event to copy cherry-pick command
cherryButton.addEventListener("click", (event) => {
event.preventDefault();
event.stopPropagation();
const command = `git cherry-pick ${commitId}`;
navigator.clipboard
.writeText(command)
.then(() => {
showCopyFeedback(cherryButton);
})
.catch((err) => {
console.error("Failed to copy: ", err);
});
});
element.appendChild(cherryButton);
}
function showCopyFeedback(button) {
const feedback = document.createElement("span");
feedback.className = "cherry-pick-feedback";
feedback.textContent = "Copied!";
button.appendChild(feedback);
setTimeout(() => {
feedback.remove();
}, 2000);
}
// Set up mutation observer to handle dynamically loaded content
function observeChanges() {
const observer = new MutationObserver((mutations) => {
let shouldCheck = false;
mutations.forEach((mutation) => {
if (mutation.addedNodes.length > 0) {
shouldCheck = true;
}
});
if (shouldCheck) {
addCherryIcons();
}
});
observer.observe(document.body, {
childList: true,
subtree: true,
});
}
function setupURLChangeListener() {
let lastUrl = location.href;
const urlObserver = new MutationObserver(() => {
if (location.href !== lastUrl) {
lastUrl = location.href;
setTimeout(() => {
initCherryPicker();
}, 500);
}
});
urlObserver.observe(document.querySelector("body"), {
childList: true,
subtree: true,
});
}
const init = () => {
initCherryPicker();
setupURLChangeListener();
};
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", () => {
init();
});
} else {
init();
}