artifactview/templates/userscript.hbs

108 lines
5.4 KiB
Handlebars

// ==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 = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true" class="svg ui text black job-artifacts-icon octicon Button-visual"><path d="M8 2c1.981 0 3.671.992 4.933 2.078 1.27 1.091 2.187 2.345 2.637 3.023a1.62 1.62 0 0 1 0 1.798c-.45.678-1.367 1.932-2.637 3.023C11.67 13.008 9.981 14 8 14c-1.981 0-3.671-.992-4.933-2.078C1.797 10.83.88 9.576.43 8.898a1.62 1.62 0 0 1 0-1.798c.45-.677 1.367-1.931 2.637-3.022C4.33 2.992 6.019 2 8 2ZM1.679 7.932a.12.12 0 0 0 0 .136c.411.622 1.241 1.75 2.366 2.717C5.176 11.758 6.527 12.5 8 12.5c1.473 0 2.825-.742 3.955-1.715 1.124-.967 1.954-2.096 2.366-2.717a.12.12 0 0 0 0-.136c-.412-.621-1.242-1.75-2.366-2.717C10.824 4.242 9.473 3.5 8 3.5c-1.473 0-2.825.742-3.955 1.715-1.124.967-1.954 2.096-2.366 2.717ZM8 10a2 2 0 1 1-.001-3.999A2 2 0 0 1 8 10Z"></path></svg>`;
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", `<a href="${queryURL(url.host, m[1], m[2], m[3], artifact)}" title="View artifact" target="_blank" rel="noopener noreferrer" class="Button Button--iconOnly Button--invisible Button--medium">${ICON}</a>`);
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 = `<a href="${queryURL(url.host, m[1], m[2], m[3], i + 1)}" title="View artifact" target="_blank" rel="noopener noreferrer">${ICON}</a>`;
elm.insertAdjacentElement("beforeend", wrapper);
wrapper.appendChild(delBtn);
}
});
observer.disconnect();
}
}).observe(rav, { childList: true, subtree: true });
}
}