|
| 1 | +!(async function () { |
| 2 | + const regexp = /https:\/\/github.com\/github\/([^\/]*)\/pull\/\d*\/files/; |
| 3 | + |
| 4 | + if (!window.location.href.match(regexp)) { |
| 5 | + window.alert("You're not on a PR 'Files changed' page. 🙃"); |
| 6 | + return |
| 7 | + } |
| 8 | + |
| 9 | + let conversation_url = window.location.href.replace(/files.*/g, ""); |
| 10 | + |
| 11 | + // get the preview deployment URL by loading the 'Conversation' page, and searching for the 'View deployment' link |
| 12 | + let deployment_url = await fetch(conversation_url) |
| 13 | + .then(function (response) { |
| 14 | + return response.text(); |
| 15 | + }) |
| 16 | + .then(function (html) { |
| 17 | + // Convert the HTML string into a document object |
| 18 | + var parser = new DOMParser(); |
| 19 | + var doc = parser.parseFromString(html, "text/html"); |
| 20 | + |
| 21 | + var elements = doc.getElementsByClassName("TimelineItem"); |
| 22 | + // Find the element that is a link that contains the text "View deployment" |
| 23 | + for (var i = 0; i < elements.length; i++) { |
| 24 | + var element = elements[i]; |
| 25 | + var links = element.getElementsByTagName("a"); |
| 26 | + for (var j = 0; j < links.length; j++) { |
| 27 | + var link = links[j]; |
| 28 | + if (link.innerText.match(/View deployment/)) { |
| 29 | + // Get the href of the link |
| 30 | + var deployment_url = link.getAttribute("href"); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + // This should return the last link that contains the text "View deployment" (there might be multiple ones if there are multiple deployments) |
| 35 | + return deployment_url; |
| 36 | + }); |
| 37 | + if (deployment_url == null) { |
| 38 | + window.alert("No preview deployment found! 😭"); |
| 39 | + return; |
| 40 | + } |
| 41 | + // strip any trailing slash from deployment_url |
| 42 | + deployment_url = deployment_url.replace(/\/$/, ""); |
| 43 | + |
| 44 | + var url_fpt = deployment_url + "/en"; |
| 45 | + var url_ghec = deployment_url + "/en/enterprise-cloud@latest"; |
| 46 | + var url_ghes = deployment_url + "/en/enterprise-server@latest"; |
| 47 | + var url_ae = deployment_url + "/en/github-ae@latest"; |
| 48 | + var fpt = "FPT"; |
| 49 | + var ghes = "GHES"; |
| 50 | + var ghec = "GHEC"; |
| 51 | + var ae = "AE"; |
| 52 | + |
| 53 | + const file_info = document.querySelectorAll("div.file-info"); |
| 54 | + for (var i = 0; i < file_info.length; i++) { |
| 55 | + var link = file_info[i].querySelector("a").title; |
| 56 | + if (link.search("data/") === 0) { |
| 57 | + continue; |
| 58 | + } else { |
| 59 | + var regex = /\.md$/; |
| 60 | + var markdownfile = link.search(regex) >= 0; |
| 61 | + if (markdownfile) { |
| 62 | + console.log("link: " + link); |
| 63 | + link = link.replace(regex, ""); |
| 64 | + link = link.replace(/^content/, ""); |
| 65 | + link = link.replace(/\/index/, ""); |
| 66 | + var span = document.createElement("SPAN"); |
| 67 | + span.style.fontFamily = |
| 68 | + "-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"; |
| 69 | + span.innerHTML = " View: "; |
| 70 | + |
| 71 | + span = addLink(span, fpt, url_fpt + link); |
| 72 | + span.innerHTML += " / "; |
| 73 | + span = addLink(span, ghec, url_ghec + link); |
| 74 | + span.innerHTML += " / "; |
| 75 | + span = addLink(span, ghes, url_ghes + link); |
| 76 | + span.innerHTML += " / "; |
| 77 | + span = addLink(span, ae, url_ae + link); |
| 78 | + |
| 79 | + file_info[i].appendChild(span); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + function addLink(span, link_name, link_href) { |
| 85 | + var anchor = document.createElement("A"); |
| 86 | + anchor.innerHTML = link_name; |
| 87 | + anchor.href = link_href; |
| 88 | + anchor.target = "_blank"; |
| 89 | + span.appendChild(anchor); |
| 90 | + return span; |
| 91 | + } |
| 92 | +})(); |
0 commit comments