-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit.js
83 lines (70 loc) · 2.45 KB
/
git.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
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
function copy(text) {
const clipboard = document.createElement('textarea');
clipboard.value = text;
document.body.appendChild(clipboard);
clipboard.focus();
clipboard.select();
try {
const success = document.execCommand('copy');
if (success) {
console.debug('[Trello Branch Name] Branch name successfully copied to clipboard');
} else {
console.debug('[Trello Branch Name] Copy to clipboard failed');
}
} catch (err) {
console.debug('[Trello Branch Name] Copy to clipboard failed', err);
}
document.body.removeChild(clipboard);
}
function notify(name) {
browser.runtime.sendMessage({
content: name,
id: 'branchNameCopied',
title: 'Branch name copied to clipboard',
type: 'notification',
});
}
function generateName(event, title) {
event.preventDefault();
event.stopPropagation();
let branchName = title.toLowerCase();
// Remove escaped characters
branchName = branchName.replace(/&[a-zA-Z]+;/g, '');
// Replace non alphanumeric characters by spaces
branchName = branchName.replace(/[^a-zA-Z0-9]/g, ' ');
// Replace consecutive spaces by single space
branchName = branchName.replace(/\s+/g, ' ');
// Remove spaces at the begining or end of string that may have been introduce by previous steps
branchName = branchName.trim();
// Replace spaces
branchName = branchName.replace(/\s+/g, '-');
copy(branchName);
notify(branchName);
return false;
}
function insertAfter(newNode, referenceNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
function createIcon(badges) {
const img = document.createElement("img");
img.setAttribute('src', browser.extension.getURL("images/git_16.png"));
img.setAttribute('height', '18px');
img.setAttribute('width', '18px');
img.setAttribute('class', 'badge');
img.addEventListener("click", (event) => {
let title = badges.previousElementSibling.innerText;
browser.runtime.sendMessage({ type: 'options' }).then((options) => {
if (options.hash) {
title = badges.previousElementSibling.textContent;
}
})
generateName(event, title)
});
return img;
}
document.arrive('.badges', { existing: true }, function() {
if (this.querySelector('img.badge')) {
return;
}
this.appendChild(createIcon(this));
})