-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbookmarklet-pr-links.js
57 lines (52 loc) · 2.02 KB
/
bookmarklet-pr-links.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
const regexp = /https:\/\/github.com\/github\/([^/]*)/;
const matches_array = regexp.exec(window.location.href);
const repo = matches_array[1];
var pr_number = document.querySelector("h1.gh-header-title span ~ span").innerHTML;
pr_number = pr_number.substring(1); // Remove "#" from start of PR number
var branch = document.querySelector("span.head-ref").innerText
branch = branch.replace(/.*:/, ''); // Remove everything before a colon
branch = branch.replace('/', ''); // Strip out all "/" characters
if (repo === "docs-internal") {
branch = branch.substring(0, 9); // Take just the 1st 9 characters
}
var url_start = "https://" + repo + "-" + pr_number + "--" + branch + ".herokuapp.com/en/";
var url_dotcom = url_start + "free-pro-team@latest";
var url_ghes = url_start + "enterprise-server@latest";
var url_ae = url_start + "github-ae@latest";
var dotcom = "dotcom";
var ghes = "GHES";
var ae = "AE";
const file_info = document.querySelectorAll("div.file-info");
for (var i = 0; i < file_info.length; i++) {
var link = file_info[i].querySelector('a').title;
if (link.search("data/") === 0) {
continue;
}
else {
var regex = /\.md$/;
var markdownfile = link.search(regex) >= 0;
if (markdownfile) {
console.log("link: " + link);
link = link.replace(regex, "");
link = link.replace(/^content/, "");
link = link.replace(/\/index/, "");
var span = document.createElement("SPAN");
span.style.fontFamily = "-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif";
span.innerHTML = " View: ";
span = addLink(span, dotcom, url_dotcom + link);
span.innerHTML += " / ";
span = addLink(span, ghes, url_ghes + link);
span.innerHTML += " / ";
span = addLink(span, ae, url_ae + link);
file_info[i].appendChild(span);
}
}
}
function addLink(span, link_name, link_href) {
var anchor = document.createElement("A");
anchor.innerHTML = link_name;
anchor.href = link_href;
anchor.target = '_blank';
span.appendChild(anchor);
return span;
}