-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (40 loc) · 1.38 KB
/
index.js
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
'use strict';
const btnContainer = document.getElementsByClassName(
'd-flex my-2 mx-md-2 flex-md-justify-end'
)[0];
const copyCommitBtn = document.createElement('button');
copyCommitBtn.type = 'button';
copyCommitBtn.className = 'btn btn-secondary';
copyCommitBtn.style.margin = '0px 10px';
copyCommitBtn.innerHTML = 'Copy commits';
btnContainer.insertBefore(copyCommitBtn, btnContainer.firstChild);
const handleButtonClick = () => {
const commitMessageContainers = document.getElementsByClassName(
'TimelineItem-body'
);
let commitMessages = ``;
commitMessageContainers &&
Object.keys(commitMessageContainers).forEach((key) => {
const commitElement = commitMessageContainers[key].querySelector(
'div div div div code a'
);
if (commitElement) {
const commitMessage = commitElement.title || '';
commitMessages += `- ${commitMessage}\n`;
}
});
copyCommitsToClipBoard(commitMessages);
copyCommitBtn.innerHTML = 'Copied to clipboard!';
setTimeout(() => {
copyCommitBtn.innerHTML = 'Copy commits';
}, 3000);
};
const copyCommitsToClipBoard = (commitMessages) => {
const el = document.createElement('textarea');
el.value = commitMessages;
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
};
btnContainer.addEventListener('click', handleButtonClick);