// ==UserScript==
// @name Artifactview
// @version 0.1.0
// @description Adds a "View artifact" button to GitHub/Gitea/Forgejo CI artifacts using the Artifactview instance {{{root_domain}}}
// @author ThetaDev
// @icon {{{main_url}}}/favicon.ico
// @homepageURL {{{main_url}}}
// @run-at document-idle
// @grant none
// @require https://greasyfork.org/scripts/28721-mutations/code/mutations.js?version=1108163
{{~#each forge_urls}}
// @match {{{this}}}
{{~/each}}
// ==/UserScript==
// Copyright (C) 2024 ThetaDev, MIT License
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
const AV_HOST = "{{{root_domain}}}";
const NO_HTTPS = {{{no_https}}};
const ALIASES = {{ @json_pretty aliases }};
function encodeDomain(s, bias) {
// Check if the character at the given position is in the middle of the string
// and it is not followed by escape seq numbers or further escapable characters
const isMidSingle = (str, pos) => {
if (pos === 0) return false;
const nc = str[pos + 1];
return nc && !nc.match(/^[0-2\-\._]$/)
};
// Escape dashes
let buf = "";
let last_pos = 0;
while (true) {
let pos = s.indexOf("-", last_pos);
if (pos < 0) break;
buf += s.substring(last_pos, pos);
if (bias === "-" && isMidSingle(s, pos)) {
buf += "-";
} else {
buf += "-1";
}
last_pos = pos + 1;
}
buf += s.substring(last_pos);
// Replace special chars [._]
let buf2 = "";
last_pos = 0;
for (let i = 0; i < buf.length; i++) {
if (buf[i] === "." || buf[i] === "_") {
if (buf[i] === bias && isMidSingle(buf, i)) {
buf2 += "-";
} else if (buf[i] == ".") {
buf2 += "-0";
} else {
buf2 += "-2";
}
} else {
buf2 += buf[i];
}
}
return buf2;
}
function queryURL(host, user, repo, run, artifact) {
const h = ALIASES[host] ?? encodeDomain(host, ".");
return `http${NO_HTTPS ? "" : "s"}://${h}--${encodeDomain(user, "-")}--${encodeDomain(repo, "-")}--${run}-${artifact}.${AV_HOST}`;
}
const url = new URL(window.location.href);
const m = url.pathname.match(/^\/([\w\-\.]+)\/([\w\-\.]+)\/actions\/runs\/(\d+)/);
const ICON = ``;
if (m) {
if (url.host === "github.com") {
const init = () => document.querySelectorAll(`a[data-test-selector="download-artifact-button"]:not([data-has-view-button="true"])`).forEach((elm) => {
const artifact = elm.getAttribute("href").match(/\d+$/)[0];
elm.insertAdjacentHTML("beforebegin", `${ICON}`);
elm.setAttribute("data-has-view-button", "true");
});
document.addEventListener("ghmo:container", init);
init();
} else {
const rav = document.getElementById("repo-action-view");
new MutationObserver((changes, observer) => {
if (changes.find((c) => Array.from(c.addedNodes).find((n) => n.className === "job-artifacts"))) {
document.querySelectorAll(".job-artifacts-item").forEach((elm, i) => {
const delBtn = elm.querySelector(".job-artifacts-delete");
if (delBtn) {
const wrapper = document.createElement("div");
wrapper.classList.add("tw-flex", "tw-gap-4", "tw-justify-end");
wrapper.innerHTML = `${ICON}`;
elm.insertAdjacentElement("beforeend", wrapper);
wrapper.appendChild(delBtn);
}
});
observer.disconnect();
}
}).observe(rav, { childList: true, subtree: true });
}
}