VS Code Extension Downloader

Adds a button to download VS Code extensions directly from the marketplace.

目前为 2025-03-20 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name VS Code Extension Downloader
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Adds a button to download VS Code extensions directly from the marketplace.
  6. // @author You
  7. // @match https://marketplace.visualstudio.com/items*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Function to check if it's an extension details page (basic check)
  16. if (!document.querySelector(".ux-item-name")) {
  17. return;
  18. }
  19.  
  20. // Delay execution to allow page to load
  21. setTimeout(getExtensionInfoAndInsertButton, 1000); // 1-second delay
  22.  
  23. function getExtensionInfoAndInsertButton() {
  24. const url = new URL(window.location.href);
  25. const identifier = url.searchParams.get('itemName');
  26.  
  27. if (!identifier) {
  28. console.error("Could not find extension identifier in the URL.");
  29. return;
  30. }
  31.  
  32. const publisher = identifier.split('.')[0];
  33. const extensionName = identifier.split('.')[1];
  34.  
  35. // Extract version from the specified table cell (more robust selector)
  36. const versionElement = document.querySelector('td[role="definition"][aria-labelledby^="version"]');
  37. const version = versionElement ? versionElement.textContent.trim() : "";
  38.  
  39. if (!version) {
  40. console.warn("Could not find extension version. Using an empty string.");
  41. }
  42.  
  43. const extensionInfo = {
  44. version: version,
  45. publisher: publisher,
  46. extensionName: extensionName, // Store extension name separately
  47. identifier: identifier,
  48. getDownloadUrl: function() {
  49. return `https://marketplace.visualstudio.com/_apis/public/gallery/publishers/${this.publisher}/vsextensions/${this.extensionName}/${this.version}/vspackage`;
  50. },
  51. getFileName: function() {
  52. return [this.identifier, "_", this.version, ".vsix"].join("");
  53. },
  54. getDownloadButton: function() {
  55. const button = document.createElement("a");
  56. button.innerHTML = "Download VSIX";
  57. button.href = "javascript:void(0);";
  58. button.style.fontFamily = "wf_segoe-ui,Helvetica Neue,Helvetica,Arial,Verdana";
  59. button.style.display = "inline-block";
  60. button.style.padding = "2px 5px";
  61. button.style.background = "darkgreen";
  62. button.style.color = "white";
  63. button.style.fontWeight = "bold";
  64. button.style.margin = "2px 5px";
  65. button.setAttribute("data-url", this.getDownloadUrl());
  66. button.setAttribute("data-filename", this.getFileName());
  67. button.onclick = function(event) {
  68. event.preventDefault(); // Prevent the default action
  69. const downloadUrl = this.getAttribute("data-url");
  70. window.open(downloadUrl, '_blank'); // Open in a new tab
  71. };
  72. return button;
  73. }
  74. };
  75.  
  76. const installButtonContainer = document.querySelector(".installButtonContainer");
  77. if (installButtonContainer) {
  78. installButtonContainer.parentNode.insertBefore(extensionInfo.getDownloadButton(), installButtonContainer);
  79. } else {
  80. console.error("Could not find install button container.");
  81. }
  82. }
  83. })();