Button to copy the raw file URL | Github

Adds a button at the end of each file row to copy the raw file URL

目前為 2022-10-30 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         	Button to copy the raw file URL | Github
// @name:de			Button zum Kopieren der Raw-Datei URL | Github
// @namespace  		https://greasyfork.org/users/928242
// @version      	1.1
// @description  	Adds a button at the end of each file row to copy the raw file URL
// @description:de  Fügt am Ende jeder Dateizeile eine Schaltfläche zum Kopieren der Raw File URL hinzu
// @author       	Kamikaze (https://github.com/Kamiikaze)
// @supportURL      https://github.com/Kamiikaze/Tampermonkey/issues
// @match        	https://github.com/*
// @icon         	https://www.google.com/s2/favicons?sz=64&domain=github.com
// @run-at       	document-ready
// @license      	MIT
// ==/UserScript==


const waitForFilelist = setInterval(() => {
	const fileListContainer = document.querySelector("div.Box > div.js-details-container.Details div")

	if ( !fileListContainer ) return

	const fileList = fileListContainer.children

	if ( fileList < 1 ) return

	appendButtons(fileList)

}, 1000)

function appendButtons(fileList) {
	for ( let i = 0; i < fileList.length; i++ ) {
		const file = fileList[i]

		if (
			file.classList.contains("sr-only") ||
			file.childElementCount !== 4
		) continue;

		const fileUrl = file.querySelector('div:nth-child(2) .js-navigation-open').href
		const rawFileUrl = fileUrl.replace('/blob/', '/raw/')

		file.append(creatyCopyButton(rawFileUrl))
	}
	// clearInterval(waitForFilelist)
};

function creatyCopyButton(copyText) {
	const copy2clipboard = `
		<clipboard-copy aria-label="Copy" value="test value" data-view-component="true" class="" tabindex="0" role="button" title="Copy raw file url">
			<svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-copy">
			<path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path>
			</svg>
		</clipboard-copy>`

	const copyButton = document.createElement('div')
	copyButton.setAttribute('role', 'gridcell')
	copyButton.style = "margin-left: 10px;"
	copyButton.innerHTML = copy2clipboard
	copyButton.children[0].value = copyText
	copyButton.children[0].style = "cursor: pointer;"

	return copyButton
}