Github - Open with VSCode

Adds "Open with VSCode" button

As of 09. 05. 2024. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Github - Open with VSCode
// @namespace    V@no
// @description  Adds "Open with VSCode" button
// @match        https://github.com/*
// @version      24.5.9-010909
// @license      MIT
// @run-at       document-end
// @grant        none
// ==/UserScript==

(() =>
{
	"use strict";
	const fixLink = () =>
	{
		// const elUL = document.querySelector(`#__primerPortalRoot__ > div > div > div > ul > div:nth-child(2) > ul[aria-labelledby=":R55ab:"]`);
		const elUL = document.querySelector(`#__primerPortalRoot__ > div > div > div > ul > div > ul`);
		console.log(elUL);
		if (!elUL || elUL.classList.contains("inited"))
			return true;

		const elLi_DownloadZip = elUL.querySelector(`li:last-child`);
		const elLiVStudio = elUL.querySelector(`#\\:rf\\:`);
		if (elLiVStudio)
		{
			const elImg_Vstudio = document.createElement("img");
			elImg_Vstudio.classList.add("mr-2");
			elImg_Vstudio.src = "https://raw.githubusercontent.com/vanowm/userscript_github-open_with_vscode/master/media/vstudio.svg";
			elLiVStudio.prepend(elImg_Vstudio);
		}
		const elLi_VSCode = elLi_DownloadZip.cloneNode(true);
		const elA_VSCode = elLi_VSCode.querySelector("a");
		elA_VSCode.dataset.action = "click:get-repo#showDownloadMessage";
		try
		{
			const hydroClick = JSON.parse(elA_VSCode.dataset.hydroClick);
			hydroClick.payload.feature_clicked = "OPEN_WITH_VSCODE";
			elA_VSCode.dataset.hydroClick = JSON.stringify(hydroClick);
		}
		catch{}
		elA_VSCode.textContent = "Open with VSCode";
		const elImg_Vscode = document.createElement("img");
		elImg_Vscode.classList.add("mr-2");
		elImg_Vscode.src = "https://raw.githubusercontent.com/vanowm/userscript_github-open_with_vscode/master/media/vscode.svg";
		elA_VSCode.prepend(elImg_Vscode);
		// display "Launching Visual Studio Code..." message
		elA_VSCode.dataset.openApp = "vscode";
		const cloneURL = (elUL.parentNode.querySelector(`input`) || {}).value;

		if (cloneURL)
			elA_VSCode.href = "vscode://vscode.git/clone?url=" + encodeURI(cloneURL);
		else
			elA_VSCode.href = elA_VSCode.href.replace("git-client://", "vscode://vscode.git/");

		elLi_DownloadZip.parentNode.insertBefore(elLi_VSCode, elLi_DownloadZip);
		elUL.classList.add("inited");
	};

	const observer = new MutationObserver(fixLink);
	if (fixLink())
		observer.observe(document, {childList: true, subtree: true});
})();