Add badge markdown to pre-commit.ci

Add code of badge markdown to CI result pages in pre-commit.ci

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Add badge markdown to pre-commit.ci
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Add code of badge markdown to CI result pages in pre-commit.ci
// @author       eggplants
// @homepage     https://github.com/eggplants
// @match        *://results.pre-commit.ci/run/github/*
// @grant        none
// @license      MIT
// ==/UserScript==
 
/*jshint esversion: 6 */
 
(function() {
    "use strict";
 
    const badge_base = "https://results.pre-commit.ci";
    const result_pathes = document.querySelectorAll("h2.pc-header.mb-3>a");
    const owner = result_pathes[0].innerText;
    const project = result_pathes[1].innerText;
    const branch = result_pathes[2].innerText;
 
    const badge_src = `${badge_base}/badge/github/${owner}/${project}/${branch}.svg`;
    const badge_href = `${badge_base}/latest/github/${owner}/${project}/${branch}`;
 
    const badge_area = document.createElement("div");
 
    const badge_img = document.createElement("img");
    badge_img.src = badge_src;
    const p_img = document.createElement("p");
    p_img.textContent = "badge preview: ";
    p_img.appendChild(badge_img);
    badge_area.appendChild(p_img);
 
    const badge_clip = document.createElement("input");
    badge_clip.value += `[![pre-commit.ci](${badge_src})](${badge_href})`;
    badge_clip.readOnly = true;
    const p_clip = document.createElement("p");
    p_clip.textContent = "markdown badge: ";
    p_clip.appendChild(badge_clip);
    badge_area.appendChild(p_clip);
 
    const target_parent = document.querySelector("div.container.bg-light.text-dark.py-5");
    const target = document.querySelector("div.pc-results.border.rounded.p-2");
    target_parent.insertBefore(badge_area, target);
})();