-
Notifications
You must be signed in to change notification settings - Fork 2
/
gh-util.user.js
697 lines (606 loc) · 30.4 KB
/
gh-util.user.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
// ==UserScript==
// @name Octopus GitHub
// @version 0.60
// @description A userscript for GitHub
// @author Oreo
// @homepage https://github.com/Oreoxmt/octopus-github
// @updateURL https://github.com/Oreoxmt/octopus-github/raw/main/gh-util.user.js
// @downloadURL https://github.com/Oreoxmt/octopus-github/raw/main/gh-util.user.js
// @supportURL https://github.com/Oreoxmt/octopus-github
// @match https://github.com/*/pulls*
// @match https://github.com/*/pull/*
// @run-at document-start
// @require https://cdnjs.cloudflare.com/ajax/libs/rest.js/15.2.6/octokit-rest.js
// ==/UserScript==
(function () {
'use strict';
const ATTR = 'octopus-github-util-mark'
const STORAGEKEY = 'octopus-github-util:token'
function GetRepositoryInformation() {
// Get the pathname of the current page
var pathname = location.pathname;
// Split the pathname into an array of parts
var parts = pathname.split('/');
// Return an object containing the user name and repository name
return {
owner: parts[1],
name: parts[2],
}
}
function EnsureToken() {
var token = localStorage.getItem(STORAGEKEY)
if (!token) {
// Prompt user to set token
// TODO: Use HTML element instead of prompt
token = prompt('Enter your GitHub token:');
if (!token) {
throw 'No token set'
}
localStorage.setItem(STORAGEKEY, token);
}
return token;
}
// This function can be used to leave a comment on a specific PR
function LeaveCommentOnPR(commentLink, comment) {
// Send the POST request to the GitHub API
// TODO: Use Octokit to create requests
fetch(commentLink, {
method: 'POST',
headers: {
'Authorization': `Bearer ${EnsureToken()}`,
'Accept': 'application/vnd.github+json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
'body': comment
})
}).then((response) => {
console.log('response to ', commentLink, response)
}).catch((error) => {
console.log('error on ', commentLink, error)
})
}
// This function can be used to get the GitHub login name of the current user
async function GetMyGitHubID() {
try {
const userURL = 'https://api.github.com/user';
const response = await fetch(userURL, {
headers: {'Authorization': `Bearer ${EnsureToken()}`,},
});
if (response.ok) {
const userData = await response.json();
return userData.login;
} else {
throw new Error('Failed to fetch current user login name.');
}
} catch (error) {
console.error('An error occurred:', error);
throw error;
}
}
// This function can be used to get the PR title, description, label, base, and head information
function GetPRInfo(octokit, messageTextElement, RepoOwner, RepoName, PRNumber) {
return new Promise((resolve, reject) => {
octokit.pullRequests.get({
owner: RepoOwner,
repo: RepoName,
number: PRNumber,
})
.then(response => {
const PRData = response.data;
const sourceTitle = PRData.title;
const sourceDescription = PRData.body;
const sourceLabels = PRData.labels.map(label => label.name);
const baseRepo = PRData.base.repo.full_name;
const baseBranch = PRData.base.ref;
const headRepo = PRData.head.repo.full_name;
const headBranch = PRData.head.ref;
messageTextElement.innerHTML += `[Log]: Getting the source language PR information...<br>`;
console.log(`Getting source language PR information was successful. The head branch name is: ${headBranch}`);
const result = [sourceTitle, sourceDescription, sourceLabels, baseRepo, baseBranch, headRepo, headBranch];
resolve(result);
})
.catch(error => {
messageTextElement.innerHTML += `<br>[Error]: Failed to get the source language PR information: ${error.message}`;
reject(error);
});
});
}
// This function can be used to sync the latest content from the upstream branch to your own branch
async function SyncMyRepoBranch(octokit, messageTextElement, targetRepoOwner, targetRepoName, myRepoOwner, myRepoName, baseBranch) {
try {
const upstreamRef = await octokit.gitdata.getReference({
owner: targetRepoOwner,
repo: targetRepoName,
ref: `heads/${baseBranch}`
});
const upstreamSHA = upstreamRef.data.object.sha;
console.log(upstreamSHA);
messageTextElement.innerHTML += `[Log]: Syncing the latest content from the upstream branch...<br>`;
await octokit.gitdata.updateReference({
owner: myRepoOwner,
repo: myRepoName,
ref: `heads/${baseBranch}`,
sha: upstreamSHA,
force: true,
headers: { 'Authorization': `Bearer ${EnsureToken()}` }
});
console.log("The content sync is successful!");
} catch (error) {
const myRepoUrl = `https://github.com/${myRepoOwner}/${myRepoName}`;
const myBranchesUrl = `${myRepoUrl}/branches`;
const baseBranchUrl = `${myRepoUrl}/tree/${baseBranch}`;
const myRepoResponse = await fetch(myRepoUrl, { method: 'HEAD' });
if (myRepoResponse.ok) {
const baseBranchResponse = await fetch(baseBranchUrl, { method: 'HEAD' });
messageTextElement.innerHTML += baseBranchResponse.ok ? `<br>[Error]: Failed to sync the <a href="${baseBranchUrl}" target="_blank">${baseBranch}</a> branch of your forked <a href="${myRepoUrl}" target="_blank">${myRepoOwner}</a> repo. <br> You need to manually sync your <a href="${baseBranchUrl}" target="_blank">${baseBranch}</a> branch from the upstream branch first.<br>` : `<br>[Error]: Your forked <a href="${myRepoUrl}" target="_blank">${myRepoName}</a> repo does not have the ${baseBranch} branch yet. <br> You need to manually <a href="${myBranchesUrl}" target="_blank">create the ${baseBranch} branch</a> in your repo based on the upstream.<br>`;
} else {
messageTextElement.innerHTML += `<br>[Error]: Failed to sync the latest content from the upstream branch to your branch. Please check whether you have forked the <a href="https://github.com/${targetRepoOwner}/${targetRepoName}" target="_blank">${targetRepoOwner}/${targetRepoName}</a> repo. If not, you need to fork the <a href="https://github.com/${targetRepoOwner}/${targetRepoName}" target="_blank">${targetRepoOwner}/${targetRepoName}</a> repo with all its branches first.<br>`;
}
console.log(error);
throw error;
}
}
// This function can be used to create a new branch in your repo
async function CreateBranch(octokit, messageTextElement, repoOwner, repoName, branchName, baseBranch) {
try {
const baseRef = await octokit.gitdata.getReference({
owner: repoOwner,
repo: repoName,
ref: `heads/${baseBranch}`
});
const baseSha = baseRef.data.object.sha;
console.log(baseSha);
messageTextElement.innerHTML += `[Log]: Creating a branch for the translation PR...<br>`;
await octokit.gitdata.createReference({
owner: repoOwner,
repo: repoName,
ref: `refs/heads/${branchName}`,
sha: baseSha,
headers: {'Authorization': `Bearer ${EnsureToken()}`}
});
const branchUrl = `https://github.com/${repoOwner}/${repoName}/tree/${branchName}`;
console.log(`A new branch is created successfully. The branch address is: ${branchUrl}`);
} catch (error) {
const branchesUrl = `https://github.com/${repoOwner}/${repoName}/branches`;
const targetBranchUrl = `https://github.com/${repoOwner}/${repoName}/tree/${branchName}`;
console.log(targetBranchUrl)
fetch(targetBranchUrl, { method: 'HEAD' })
.then(response => {
messageTextElement.innerHTML += response.ok ? `<br>[Error]: Failed to create the branch for the translation PR. <br> The target branch <a href="${targetBranchUrl}" target="_blank">${branchName}</a> already exists in your <a href="${branchesUrl}" target="_blank">repo</a>. You need to manually create a new translation PR with a different branch name.` : `<br>[Error]: Failed to create the branch for the translation PR:<br> ${error.message}`;
});
console.error(error);
throw error;
}
}
// This function can be used to create a temp file in your specified branch
async function CreateFileInBranch(octokit, messageTextElement, repoOwner, repoName, branchName, filePath, fileContent, commitMessage) {
try {
const contentBase64 = btoa(fileContent);
const response = await octokit.repos.createFile({
owner: repoOwner,
repo: repoName,
branch: branchName,
path: filePath,
message: commitMessage,
content: contentBase64,
headers: {'Authorization': `Bearer ${EnsureToken()}`}
});
console.log('A temp file is created successfully!');
} catch (error) {
//console.log('Failed to create the temp file.');
messageTextElement.innerHTML += `<br>[Error]: Failed to create a temp file in the new branch: ${error.message}<br>`;
console.error(error);
}
}
// This function can be used to modify the description of the translation PR
function UpdatePRDescription(sourceRepoOwner, sourceRepoName, sourcePRNumber, sourceDescription, targetRepoName) {
const sourcePRCLA = "https://cla-assistant.io/pingcap/" + sourceRepoName;
const newPRCLA = "https://cla-assistant.io/pingcap/" + targetRepoName;
const sourcePRURL = `https://github.com/${sourceRepoOwner}/${sourceRepoName}/pull/${sourcePRNumber}`;
let newPRDescription = sourceDescription.replace(sourcePRCLA, newPRCLA);
newPRDescription = newPRDescription.replace("This PR is translated from:", "This PR is translated from: " + sourcePRURL);
const regexConstructor = new RegExp(".*?\\[tips for choosing the affected versions.*?\\n\\n?", "g");
newPRDescription = newPRDescription.replace(regexConstructor, "");
console.log(newPRDescription)
return newPRDescription;
}
// This function can be used to create a pull request based on your specified branch
async function CreatePullRequest(octokit, messageTextElement, targetRepoOwner, targetRepoName, baseBranch, myRepoOwner, myRepoName, newBranchName, title, body, labels) {
try {
messageTextElement.innerHTML += `[Log]: Creating the empty translation PR...<br>`;
const prResponse = await octokit.pullRequests.create({
owner: targetRepoOwner,
repo: targetRepoName,
title: title,
body: body,
head: `${myRepoOwner}:${newBranchName}`,
base: baseBranch,
headers: {'Authorization': `Bearer ${EnsureToken()}`}
});
try {
console.log(prResponse);
const prUrl = prResponse.data.html_url;
//console.log(`Your target PR is created successfully. The PR address is: ${prUrl}`);
messageTextElement.innerHTML += `<br> Your target PR is created successfully. <br> The PR address is:<br> <a href="${prUrl}" target="_blank">${prUrl}</a>`;
const urlParts = prUrl.split("/");
const prNumber = urlParts[6];
// Add labels to the created PR
const labelsResponse = await octokit.issues.addLabels({
owner: targetRepoOwner,
repo: targetRepoName,
number: prNumber,
labels: labels,
headers: {'Authorization': `Bearer ${EnsureToken()}`}
});
console.log('Labels are added successfully.');
return prUrl;
} catch (error) {
console.log('Failed to add the PR labels.');
console.error(error);
}
} catch (error) {
console.log('Failed to create the translation PR.');
messageTextElement.innerHTML += `<br>[Error]: Failed to create the translation PR: ${error.message}<br>`;
console.error(error);
}
}
async function DeleteFileInBranch(octokit, repoOwner, repoName, branchName, filePath, commitMessage) {
try {
const { data: fileInfo } = await octokit.repos.getContent({
owner: repoOwner,
repo: repoName,
path: filePath,
ref: branchName
});
await octokit.repos.deleteFile({
owner: repoOwner,
repo: repoName,
path: filePath,
message: commitMessage,
sha: fileInfo.sha,
branch: branchName,
headers: {'Authorization': `Bearer ${EnsureToken()}`}
});
console.log("The temp.md is deleted successfully!");
} catch (error) {
console.log(`Failed to delete temp.md. Error message: ${error.message}`);
throw error;
}
}
async function CreateTransPR() {
try {
const messageBox = document.createElement("div");
messageBox.style.position = "fixed";
messageBox.style.top = "50%";
messageBox.style.left = "50%";
messageBox.style.transform = "translate(-50%, -50%)";
messageBox.style.padding = "30px";
messageBox.style.backgroundColor = "white";
messageBox.style.border = "1px solid #e1e4e8";
messageBox.style.borderRadius = "6px";
messageBox.style.boxShadow = "0 0 10px rgba(0, 0, 0, 0.1)";
messageBox.style.zIndex = "9999";
messageBox.style.width = "430px";
messageBox.style.height = "300px";
messageBox.style.marginTop = "10px";
document.body.appendChild(messageBox);
const messageTextElement = document.createElement("span");
messageTextElement.innerHTML = `Start creating an empty translation PR for you.<br>Wait for a few seconds....<br><br>`;
messageTextElement.style.fontSize = "14px";
messageTextElement.style.color = "#24292e";
messageTextElement.style.marginBottom = "10px";
messageBox.appendChild(messageTextElement);
const closeButton = document.createElement("span");
closeButton.innerText = "X";
closeButton.style.position = "absolute";
closeButton.style.top = "8px";
closeButton.style.right = "10px";
closeButton.style.right = "8px";
closeButton.style.fontSize = "12px";
closeButton.style.fontWeight = "bold";
closeButton.style.color = "#586069";
closeButton.style.border = "none";
closeButton.style.backgroundColor = "transparent";
closeButton.style.cursor = "pointer";
closeButton.addEventListener("click", () => {
messageBox.style.display = "none";
});
messageBox.appendChild(closeButton);
// Show the message box
messageBox.style.display = "block";
const octokit = new Octokit({ auth: EnsureToken() });
console.log(octokit);
// TODO: Define a new function to parse the current URL and return the repo owner, repo name, PR number, etc.
const currentURL = window.location.pathname;
const currentURLSplit = currentURL.split("/");
const currentRepoOwner = currentURLSplit[1];
const currentRepoName = currentURLSplit[2];
const currentPRNumber = currentURLSplit[4];
const targetRepoOwner = "pingcap"
let myRepoName, targetRepoName, translationLabel;
switch (currentRepoName) {
case "docs-cn":
myRepoName = "docs";
targetRepoName = "docs";
translationLabel = "translation/from-docs-cn";
break;
case "docs":
myRepoName = "docs-cn";
targetRepoName = "docs-cn";
translationLabel = "translation/from-docs";
break;
}
//1.Get the GitHub login name of the current user
const myRepoOwner = await GetMyGitHubID();
//2.Get the source PR information
const [sourceTitle, sourceDescription, sourceLabels, baseRepo, baseBranch, headRepo, headBranch] = await GetPRInfo(octokit, messageTextElement, currentRepoOwner, currentRepoName, currentPRNumber);
const excludeLabels = ["size", "translation", "status", "first-time-contributor", "contribution", "lgtm", "approved"];
const targetLabels = sourceLabels.filter(label => !excludeLabels.some(excludeLabel => label.includes(excludeLabel)));
if (!sourceLabels.includes("translation/done")) {
// Proceed with the PR creation only if the translation/done label is not added for the current source PR
//3. Sync the base branch of my forked repository
await SyncMyRepoBranch(octokit, messageTextElement, targetRepoOwner, targetRepoName, myRepoOwner, myRepoName, baseBranch);
//4. Create a new branch in the repository that I forked
const newBranchName = `${headBranch}-${currentPRNumber}`;
await CreateBranch(octokit, messageTextElement, myRepoOwner, myRepoName, newBranchName, baseBranch);
//5. Create a temporary temp.md file in the new branch
const filePath = "temp.md";
const FileContent = "This is a test file.";
const CommitMessage = "Add temp.md";
await CreateFileInBranch(octokit, messageTextElement, myRepoOwner, myRepoName, newBranchName, filePath, FileContent, CommitMessage);
//6. Create a pull request
const title = sourceTitle;
const body = UpdatePRDescription(currentRepoOwner, currentRepoName, currentPRNumber, sourceDescription, targetRepoName);
targetLabels.push(translationLabel);
const labels = targetLabels;
const targetPRURL = await CreatePullRequest(octokit, messageTextElement, targetRepoOwner, targetRepoName, baseBranch, myRepoOwner, myRepoName, newBranchName, title, body, labels);
//7. Delete the temporary temp.md file
const CommitMessage2 = "Delete temp.md";
await DeleteFileInBranch(octokit, myRepoOwner, myRepoName, newBranchName, filePath, CommitMessage2);
}
else {
messageTextElement.innerHTML += `<br>[Error]: The current PR already has the <b>translation/done</b> label, which means that there is already a translation PR for it. Please check if you still need to create another translation PR. If yes, you need to change the <b>translation/done</b> label to <b>translation/doing</b> first.<br>`;
}
} catch (error) {
console.error("An error occurred:", error);
return error;
}
}
// TODO: Use toggle instead of button, and add more features to the toggle, e.g., editing tokens.
function EnsureCommentButton() {
const MARK = 'comment-button'
if (document.querySelector(`button[${ATTR}="${MARK}"]`)) {
return;
}
// First, find the "table-list-header-toggle" div
var toggleDiv = document.querySelector('.table-list-header-toggle.float-right');
if (!toggleDiv) {
return;
}
// Next, create a button element and add it to the page
var button = document.createElement('button');
button.innerHTML = 'Comment';
button.setAttribute('class', 'btn btn-sm js-details-target d-inline-block float-left float-none m-0 mr-md-0 js-title-edit-button');
button.setAttribute(ATTR, MARK);
toggleDiv.appendChild(button);
// Next, add an event listener to the button to listen for clicks
button.addEventListener('click', function () {
EnsureToken();
// Get a list of all the checkboxes on the page (these are used to select PRs)
var checkboxes = document.querySelectorAll('input[type=checkbox][data-check-all-item]');
// Iterate through the checkboxes and get the ones that are checked
var selectedPRs = [];
checkboxes.forEach(function (checkbox) {
if (checkbox.checked) {
selectedPRs.push(checkbox.value);
}
})
// Prompt the user for a comment to leave on the selected PRs
var comment = prompt('Enter a comment to leave on the selected PRs:');
if (!comment) {
return;
}
var repo = GetRepositoryInformation();
// Leave the comment on each selected PR
selectedPRs.forEach(function (pr) {
var commentLink = `https://api.github.com/repos/${repo.owner}/${repo.name}/issues/${pr}/comments`;
// Leave a comment on the PR
LeaveCommentOnPR(commentLink, comment);
});
});
}
function EnsureCommentButtonOnPR() {
const MARK = "comment-button-pr";
if (document.querySelector(`button[${ATTR}="${MARK}"]`)) {
return;
}
// First, find the "table-list-header-toggle" div
var headerActions = document.querySelector(".gh-header-actions");
if (!headerActions) {
return;
}
// Next, create a button element and add it to the page
var button = document.createElement("button");
button.innerHTML = "Comment";
button.setAttribute(
"class",
"flex-md-order-2 Button--secondary Button--small Button m-0 mr-md-0"
);
button.setAttribute(ATTR, MARK);
headerActions.appendChild(button);
// Next, add an event listener to the button to listen for clicks
button.addEventListener("click", function () {
EnsureToken();
// get the pr number
const url = window.location.pathname;
const urlSplit = url.split("/");
const index = urlSplit.indexOf("pull");
const pr = urlSplit[index + 1];
// Prompt the user for a comment to leave on the selected PRs
var comment = prompt("Enter a comment to leave on the selected PRs:");
if (!comment) {
return;
}
var repo = GetRepositoryInformation();
// Leave the comment on this PR
var commentLink = `https://api.github.com/repos/${repo.owner}/${repo.name}/issues/${pr}/comments`;
LeaveCommentOnPR(commentLink, comment);
});
}
function EnsureFileLink(issueElement) {
const MARK = 'file-link-span'
if (issueElement.querySelector(`span[${ATTR}="${MARK}"]`)) {
return; // Already added
}
var issueId = issueElement.getAttribute("id")
var originalLinkElement = document.getElementById(issueId + "_link")
if (!originalLinkElement) {
return; // Element is not ready
}
var originalLink = originalLinkElement.getAttribute("href")
var newLink = originalLink + "/files"
var openedByElement = issueElement.querySelectorAll('span[class="opened-by"]');
if (openedByElement.length == 1) {
var openedBy = openedByElement[0];
var linkSpanElement = document.createElement('span');
linkSpanElement.setAttribute('class', 'd-inline-block mr-1 custom')
linkSpanElement.setAttribute(ATTR, MARK)
var dotSpanElement = document.createElement('span');
dotSpanElement.innerHTML = ' • ';
dotSpanElement.setAttribute('class', 'd-inline-block mr-1 custom')
var linkElement = document.createElement('a')
linkElement.setAttribute('href', newLink)
linkElement.setAttribute('class', 'Link--muted')
linkElement.innerHTML = "Files"
linkSpanElement.appendChild(linkElement)
openedBy.insertAdjacentElement('beforebegin', linkSpanElement)
openedBy.insertAdjacentElement('beforebegin', dotSpanElement);
}
}
// This function creates a button that scrolls to top of the page
function EnsureScrollToTopButton() {
const MARK = 'scroll-to-top-button';
if (document.querySelector(`button[${ATTR}="${MARK}"]`)) {
return;
}
// create the button
var button = document.createElement('button');
button.innerHTML = '↑';
// set position and style for the button
button.style.position = "fixed";
button.style.bottom = "55px";
button.style.right = "20px";
button.style.zIndex = "999"; // always on top
button.style.width = "30px";
button.style.display = "none"; // initially hidden
button.className = "js-details-target js-title-edit-button flex-md-order-2 Button--secondary Button--small Button m-0 mr-md-0";
// trigger scrolling to top when button is clicked
button.addEventListener('click', function () {
window.scrollTo(0, 0);
});
// add the button to the page
document.body.appendChild(button);
// show the button only when not at the top
window.addEventListener("scroll", function() {
if (window.pageYOffset > 0) {
button.style.display = "block";
} else {
button.style.display = "none";
}
});
}
// This function creates a button that scrolls to bottom of the page
function EnsureScrollToBottomButton() {
const MARK = 'scroll-to-bottom-button';
if (document.querySelector(`button[${ATTR}="${MARK}"]`)) {
return;
}
// create the button
var button = document.createElement('button');
button.innerHTML = '↓';
// set position and style for the button
button.style.position = "fixed";
button.style.bottom = "20px";
button.style.right = "20px";
button.style.zIndex = "999"; // always on top
button.style.width = "30px";
button.className = "js-details-target js-title-edit-button flex-md-order-2 Button--secondary Button--small Button m-0 mr-md-0";
// trigger scrolling to bottom when button is clicked
button.addEventListener('click', function () {
window.scrollTo(0, document.body.scrollHeight);
});
// add the button to the page
document.body.appendChild(button);
// show the button only when not at the bottom
window.addEventListener("scroll", function() {
if (window.pageYOffset + window.innerHeight < document.body.scrollHeight) {
button.style.display = "block";
} else {
button.style.display = "none";
}
});
}
function EnsureCreateTransPRButtonOnPR() {
const MARK = 'create-trans-pr-button';
// Check if the button already exists
if (document.querySelector(`button[${ATTR}="${MARK}"]`)) {
return;
}
// Find the header actions container
var headerActions = document.querySelector(".gh-header-actions");
if (!headerActions) {
return;
}
// Create a button element
var button = document.createElement("button");
button.innerHTML = "Create Translation PR";
button.setAttribute(
"class",
"flex-md-order-2 Button--secondary Button--small Button m-0 mr-md-0"
);
button.setAttribute(ATTR, MARK);
headerActions.appendChild(button);
// Add event listener to the button
button.addEventListener("click", function () {
// Call the function to create translation PR
EnsureToken();
CreateTransPR();
});
}
function Init() {
const url = window.location.href;
// If we are on the PR list page, add the comment button and file link
if (url.includes('/pulls')) {
const observer = new MutationObserver(() => {
document.querySelectorAll('div[id^="issue_"]').forEach((element) => {
EnsureFileLink(element);
})
EnsureCommentButton();
});
const config = { childList: true, subtree: true };
observer.observe(document, config);
}
// If we are on the PR details page, add the scroll to top and bottom buttons
if (url.includes('/pull/')) {
EnsureScrollToTopButton();
EnsureScrollToBottomButton();
EnsureCommentButtonOnPR();
const observer = new MutationObserver(() => {
EnsureCommentButtonOnPR();
});
const targetNode = document.body;
const observerOptions = { childList: true, subtree: true };
observer.observe(targetNode, observerOptions);
// If we are on the PR details page of pingcap/docs-cn or pingcap/docs, add the CreateTranslationPR button
if (url.includes('pingcap/docs-cn/pull') || url.includes('pingcap/docs/pull')) {
EnsureCreateTransPRButtonOnPR();
const observerCreateTransPR = new MutationObserver(() => {
EnsureCreateTransPRButtonOnPR();
});
observerCreateTransPR.observe(targetNode, observerOptions);
}
}
}
Init();
})();