VS Code Extension Downloader

在 VS Code Marketplace 页面添加直接下载按钮

  1. // ==UserScript==
  2. // @name VS Code Extension Downloader
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description 在 VS Code Marketplace 页面添加直接下载按钮
  6. // @author Trae AI
  7. // @match https://marketplace.visualstudio.com/items?itemName=*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. async function init() {
  16. // 获取 itemName 参数
  17. const urlParams = new URLSearchParams(window.location.search);
  18. const itemName = urlParams.get('itemName');
  19. if (!itemName) return;
  20.  
  21. // 分割 publisher 和 extension 名称
  22. const [publisher, extension] = itemName.split('.');
  23.  
  24. // 创建下载按钮
  25. const downloadButton = document.createElement('button');
  26. downloadButton.innerHTML = '下载最新版本';
  27. downloadButton.style.cssText = `
  28. background-color: #0078d4;
  29. color: white;
  30. border: none;
  31. padding: 8px 16px;
  32. border-radius: 4px;
  33. cursor: pointer;
  34. position: fixed;
  35. top: 20px;
  36. right: 20px;
  37. z-index: 10000;
  38. `;
  39.  
  40. // 点击事件处理
  41. downloadButton.addEventListener('click', async () => {
  42. // 跳转到版本历史页面
  43. window.location.hash = 'version-history';
  44.  
  45. // 等待版本历史表格加载
  46. await new Promise(resolve => setTimeout(resolve, 1000));
  47.  
  48. // 获取最新版本号
  49. const versionCell = document.querySelector('.version-history-table-body .version-history-container-row:first-child .version-history-container-column:first-child');
  50. if (!versionCell) {
  51. alert('无法获取版本号');
  52. return;
  53. }
  54.  
  55. const version = versionCell.textContent.trim();
  56. const downloadUrl = `https://marketplace.visualstudio.com/_apis/public/gallery/publishers/${publisher}/vsextensions/${extension}/${version}/vspackage`;
  57.  
  58. // 创建下载
  59. const link = document.createElement('a');
  60. link.href = downloadUrl;
  61. link.download = `${itemName}-${version}.vsix`;
  62. document.body.appendChild(link);
  63. link.click();
  64. document.body.removeChild(link);
  65. });
  66.  
  67. // 添加按钮到页面
  68. document.body.appendChild(downloadButton);
  69. }
  70.  
  71. // 启动脚本
  72. init();
  73. })();