forked from oleggr/helper_browser_extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforeground.js
146 lines (118 loc) · 4.1 KB
/
foreground.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
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
var styles = `
.create-diff-image-btn {
background-color: transparent;
color: black;
border: none;
padding: 0px ;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 14px;
margin-right: 5px;
cursor: pointer;
}
`
var styleSheet = document.createElement("style")
styleSheet.innerText = styles
document.head.appendChild(styleSheet)
let btn = document.createElement("button");
btn.innerHTML = "📷";
btn.title = 'Copy image to clipboard';
btn.className = 'create-diff-image-btn';
btn.addEventListener("click", async () => {
await genImage()
for (let i = 0; i < 3; i++) {
await new Promise(r => setTimeout(r, 200));
btn.innerHTML = '📸';
await new Promise(r => setTimeout(r, 200));
btn.innerHTML = '📷';
}
});
async function genImage() {
let taskDescription = document.getElementsByClassName('review-head-summary-headline__headline')[0].innerText.split('/');
let taskNumberReserve = taskDescription[taskDescription.length - 1]
let taskData = document.getElementsByClassName('pr-info-tickets__ticket-name')
let taskNumber = taskNumberReserve
if (typeof taskData != "undefined" && taskData.length > 0) {
console.log('Using task number from Tickets section')
taskData[0].innerText;
}
let added = document.getElementsByClassName("diff-stat__additions")[0].innerText;
let removed = document.getElementsByClassName("diff-stat__deletions")[0].innerText;
var canvas = document.createElement('canvas');
canvas.id = 'canvas';
canvas.width = 400;
canvas.height = 200;
canvas.style.display = 'none';
document.body.appendChild(canvas);
var ctx = canvas.getContext('2d');
function drawNumber() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
const hMiddle = canvas.width / 2;
const vMiddle = canvas.height / 2;
ctx.textAlign = "right";
ctx.font = "20px monospace";
ctx.fillStyle = "black";
ctx.textAlign = "right";
ctx.fillText("Review Request", hMiddle - 5, 20);
ctx.font = "40px monospace";
ctx.fillStyle = "green";
ctx.fillText(added, hMiddle - 5, vMiddle + 15);
ctx.textAlign = "left";
ctx.font = "bold 20px monospace";
ctx.fillStyle = "#5282ff";
ctx.fillText(taskNumber, hMiddle + 5, 20);
ctx.font = "40px monospace";
ctx.fillStyle = "red";
ctx.fillText(removed, hMiddle + 5, vMiddle + 15);
}
drawNumber();
async function copyToClipboard() {
canvas.toBlob(function (blob) {
var item = new ClipboardItem({ "image/png": blob });
navigator.clipboard.write([item]);
console.log('done')
});
}
copyToClipboard();
document.body.removeChild(canvas);
}
function waitForElement(selector) {
return new Promise(
(resolve) => {
if (document.querySelector(selector)) {
return resolve(element);
}
const observer = new MutationObserver(() => {
const element = document.querySelector(selector);
if (element) {
console.log('element found')
resolve(element);
createButton(element);
// observer.disconnect();
}
});
observer.observe(document.body, { childList: true, subtree: true, });
});
}
function createButton(element) {
// if button already exists, do nothing
if (document.getElementById('createDiffImageWrapper')) {
return;
}
// get parent element
let parent = element.parentElement;
let newDiv = document.createElement('div');
newDiv.className = 'navigation__column';
newDiv.id = 'createDiffImageWrapper';
newDiv.appendChild(btn);
// insert new div before parent element
parent.parentNode.insertBefore(newDiv, parent);
}
async function start() {
console.log('start')
waitForElement('.pr-navigation__stats').then((element) => {
console.log('Element is ready');
});
}
start();