Github - Open with VSCode

Fixes "Open with Visual Studio" button to open with VSCode instead

当前为 2023-10-21 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Github - Open with VSCode
// @namespace    V@no
// @description  Fixes "Open with Visual Studio" button to open with VSCode instead
// @match        https://github.com/*
// @version      yy.m.d-HHmmss
// @license      MIT
// @run-at       document-end
// @grant        none
// ==/UserScript==

(() =>
{
	"use strict";
	/**
	 * This function fixes the link to open a GitHub repository in Visual Studio Code.
	 * It replaces the data-open-app attribute value from "visual-studio" to "vscode",
	 * adds " Code" to the link text, and updates the href attribute to launch VS Code
	 * with the clone URL of the repository.
	 * @returns {boolean} Returns true if the link element is not found.
	 */
	const fixLink = () =>
	{
		const elLink = document.querySelector('[data-open-app="visual-studio"]');
		if (!elLink)
			return true;

		elLink.dataset.openApp = "vscode"; // display "Launching Visual Studio Code..." message
		elLink.innerHTML += " Code";
		const cloneURL = (document.querySelector(".input-group > .form-control") || {}).value;
		if (cloneURL)
			elLink.href = "vscode://vscode.git/clone?url=" + encodeURI(cloneURL);
		else
			elLink.href = elLink.href.replace("git-client://", "vscode://vscode.git/");

		//we don't need keep observing DOM changes
		observer.disconnect();
	};

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