Github - Open with VSCode

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

目前為 2023-10-21 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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      23.10.21-011323
// @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;

		// display "Launching Visual Studio Code..." message
		elLink.dataset.openApp = "vscode";
		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 anymore
		observer.disconnect();
	};

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