Update nightly.link buttons to 1.1

SPA-like page navigation is now handled
This commit is contained in:
blankie 2022-09-01 20:28:33 +07:00
parent e2acd5235b
commit 35c7f30c14
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
1 changed files with 44 additions and 20 deletions

View File

@ -1,10 +1,9 @@
// ==UserScript== // ==UserScript==
// @name nightly.link buttons // @name nightly.link buttons
// @namespace blankie-scripts // @namespace blankie-scripts
// @match http*://github.com/*/*/runs/* // @match http*://github.com/*
// @match http*://github.com/*/*/actions/runs/*
// @grant none // @grant none
// @version 1.0 // @version 1.1
// @author blankie // @author blankie
// @run-at document-end // @run-at document-end
// @description Show buttons to nightly.link on artifacts and build logs // @description Show buttons to nightly.link on artifacts and build logs
@ -12,12 +11,11 @@
const INSTANCE_URL = "https://nightly.link"; const INSTANCE_URL = "https://nightly.link";
function pageUpdated() { function addArtifactsButton(artifactsDiv) {
console.log("awo"); if (artifactsDiv === null) {
let artifactsDiv = document.evaluate( return false;
"//div[@class = 'mx-0 mx-md-1'][h4[@class = 'text-bold']][div[@class = 'color-fg-muted text-small']]", document) }
.iterateNext();
if (artifactsDiv !== null) {
let artifactsButton = document.createElement("a"); let artifactsButton = document.createElement("a");
artifactsButton.className = "btn"; artifactsButton.className = "btn";
// https://stackoverflow.com/a/17228764 // https://stackoverflow.com/a/17228764
@ -25,23 +23,49 @@ function pageUpdated() {
artifactsButton.innerText = "View on nightly.link"; artifactsButton.innerText = "View on nightly.link";
artifactsButton.href = INSTANCE_URL + location.pathname; artifactsButton.href = INSTANCE_URL + location.pathname;
artifactsDiv.prepend(artifactsButton); artifactsDiv.prepend(artifactsButton);
return true;
} }
let githubLogsButton = document.querySelector("a.js-steps-dropdown-raw-logs"); function addLogsButton(githubLogsButton, logsHeader) {
let logsHeader = document.querySelector("div.js-checks-log-toolbar");
if (githubLogsButton !== null) { if (githubLogsButton !== null) {
let nightlyLogsButton = document.createElement("a"); let nightlyLogsButton = document.createElement("a");
nightlyLogsButton.className = "pl-5 dropdown-item btn-link"; nightlyLogsButton.className = "pl-5 dropdown-item btn-link";
nightlyLogsButton.innerText = "View logs on nightly.link"; nightlyLogsButton.innerText = "View logs on nightly.link";
nightlyLogsButton.href = INSTANCE_URL + location.pathname + ".txt"; nightlyLogsButton.href = INSTANCE_URL + location.pathname + ".txt";
githubLogsButton.parentNode.append(nightlyLogsButton); githubLogsButton.parentNode.append(nightlyLogsButton);
return true;
} else if (logsHeader !== null) { } else if (logsHeader !== null) {
let logsLink = document.createElement("a"); let logsLink = document.createElement("a");
logsLink.innerText = "View on nightly.link"; logsLink.innerText = "View on nightly.link";
logsLink.href = INSTANCE_URL + location.pathname + ".txt"; logsLink.href = INSTANCE_URL + location.pathname + ".txt";
logsHeader.append(logsLink); logsHeader.append(logsLink);
return true;
}
return false;
}
function handleNewPage() {
let artifactsDiv = document.body.querySelector("div#artifacts div.mx-0.mx-md-1");
let githubLogsButton = document.body.querySelector("a.js-steps-dropdown-raw-logs");
let logsHeader = document.body.querySelector("div.js-checks-log-toolbar");
addArtifactsButton(artifactsDiv) || addLogsButton(githubLogsButton, logsHeader);
};
handleNewPage();
function observerCallback(mutationList, observer) {
for (mutation of mutationList) {
if (mutation.type !== "childList") {
continue;
}
for (node of mutation.addedNodes) {
if (node.nodeName === "BODY") {
handleNewPage();
return;
}
}
} }
} }
// TODO: function properly on SPA-like navigation let observer = new MutationObserver(observerCallback);
pageUpdated(); observer.observe(document.body.parentNode, {childList: true});