Greasy Fork

Greasy Fork is available in English.

Sourcehut: copy commit reference

Adds a "Copy commit reference" button to every commit page on Sourcehut websites.

当前为 2024-04-24 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Sourcehut: copy commit reference
// @namespace    https://andrybak.dev
// @license      AGPL-3.0-only
// @version      1
// @description  Adds a "Copy commit reference" button to every commit page on Sourcehut websites.
// @homepageURL  https://github.com/rybak/copy-commit-reference-userscript
// @supportURL   https://github.com/rybak/copy-commit-reference-userscript/issues
// @author       Andrei Rybak
// @match        https://git.sr.ht/*/commit/*
// @icon         https://sourcehut.org/logo.png
// @require      https://cdn.jsdelivr.net/gh/rybak/userscript-libs@dc32d5897dcfa40a01c371c8ee0e211162dfd24c/waitForElement.js
// @require      https://cdn.jsdelivr.net/gh/rybak/copy-commit-reference-userscript@ccc10169f256c8d7fec77844fad7dfcd7abd429d/copy-commit-reference-lib.js
// @grant        none
// ==/UserScript==

/*
 * Copyright (C) 2024 Andrei Rybak
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

(function () {
	'use strict';

	const LOG_PREFIX = '[Sourcehut: copy commit reference]:';

	function error(...toLog) {
		console.error(LOG_PREFIX, ...toLog);
	}

	function warn(...toLog) {
		console.warn(LOG_PREFIX, ...toLog);
	}

	function info(...toLog) {
		console.info(LOG_PREFIX, ...toLog);
	}

	function debug(...toLog) {
		console.debug(LOG_PREFIX, ...toLog);
	}

	/*
	 * Implementation for sourcehut.org, sr.ht, etc.
	 */
	class Sourcehut extends GitHosting {
		// mandatory:

		getTargetSelector() {
			return 'html body div.container div.row div.col-md-2 div.mb-3';
		}

		getFullHash() {
			const commitSelfLink = document.querySelector('a[id^="log-"]');
			return commitSelfLink.id.slice(4);
			// also in document.querySelector('html body div.container div.row div.col-md-10 div.event-list div.event div').childNodes[0].textContent.trimStart().slice(0, 40);
		}

		async getDateIso(hash) {
			const commitSelfLinkTimestamp = document.querySelector('a[id^="log-"] span');
			return commitSelfLinkTimestamp.title.slice(0, 10);
		}

		async getCommitMessage(hash) {
			return document.querySelector('.commit').innerText;
		}

		// optional:

		getButtonText() {
			return 'copy reference';
		}

		wrapButtonContainer(innerContainer) {
			innerContainer.classList.add('btn', 'btn-default', 'btn-block');
			innerContainer.style.padding = 0;
			return innerContainer;
		}

		wrapButton(button) {
			button.classList.add('btn', 'btn-default');
			button.style.borderStyle = 'none';
			button.style.width = '100%';
			return button;
		}
	}

	CopyCommitReference.runForGitHostings(new Sourcehut());
})();