Last active
March 31, 2026 09:17
-
-
Save wbxl2000/89a878a32fbdb690bdd02e392fa4768d to your computer and use it in GitHub Desktop.
npm-file-downloader: A Tampermonkey script that supports downloading single npm files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==UserScript== | |
| // @name npm-file-downloader | |
| // @video https://youtu.be/6BqphFJ69-g | |
| // @namespace http://tampermonkey.net/ | |
| // @version 2026-03-31 | |
| // @description A Tampermonkey script that supports downloading single npm files (Fixed for new npm UI) | |
| // @author qer | |
| // @match https://www.npmjs.com/* | |
| // @icon https://github.com/user-attachments/assets/7ccadb13-ee47-4206-88bf-050a087988d8 | |
| // ==/UserScript== | |
| (function () { | |
| 'use strict'; | |
| const mainButtonCssText = ` | |
| background-color: #0969da; | |
| color: white; | |
| padding: 8px 16px; | |
| border: none; | |
| border-radius: 6px; | |
| font-size: 14px; | |
| font-weight: 500; | |
| cursor: pointer; | |
| transition: background-color 0.2s ease; | |
| margin-left: 10px; | |
| box-shadow: 0 1px 3px rgba(0,0,0,0.12); | |
| `; | |
| const downloadButtonCssText = ` | |
| color: #0969da; | |
| text-decoration: none; | |
| font-size: 14px; | |
| padding: 4px 8px; | |
| border-radius: 4px; | |
| transition: background-color 0.2s ease; | |
| `; | |
| const showDownloadLinksButton = document.createElement('button'); | |
| showDownloadLinksButton.innerHTML = 'Show Download Links'; | |
| showDownloadLinksButton.style.cssText = mainButtonCssText; | |
| addButtonClickEffect(showDownloadLinksButton); | |
| showDownloadLinksButton.onclick = showDownloadLinks; | |
| addShowDownloadLinksButton(); | |
| function addButtonClickEffect(button) { | |
| button.addEventListener('mouseover', function () { | |
| this.style.backgroundColor = '#0557c5'; | |
| }); | |
| button.addEventListener('mouseout', function () { | |
| this.style.backgroundColor = '#0969da'; | |
| }); | |
| button.addEventListener('mousedown', function () { | |
| this.style.transform = 'scale(0.98)'; | |
| }); | |
| button.addEventListener('mouseup', function () { | |
| this.style.transform = 'scale(1)'; | |
| }); | |
| } | |
| function addShowDownloadLinksButton() { | |
| const tabListA = document.querySelector('ul[role="tablist"]')?.querySelectorAll('a'); | |
| if (!tabListA) return; | |
| for (const a of tabListA) { | |
| a.onclick = () => { | |
| if (a.getAttribute('href') === '?activeTab=code') { | |
| addButtonToPage(); | |
| } else showDownloadLinksButton.remove(); | |
| }; | |
| } | |
| const link = document.querySelector('a[href="?activeTab=code"]'); | |
| if (!link) return; | |
| const ariaSelected = link.getAttribute('aria-selected'); | |
| console.log('ariaSelected', ariaSelected); | |
| if (ariaSelected === 'true') { | |
| addButtonToPage(); | |
| } | |
| } | |
| function addButtonToPage() { | |
| // 尝试多种方式添加按钮 | |
| // 1. 尝试找到 table 的父容器 | |
| const table = document.querySelector('table[aria-labelledby="file-explorer-heading"]'); | |
| if (table && table.parentElement) { | |
| // 检查是否已添加 | |
| if (!table.parentElement.querySelector('[data-nfd-button]')) { | |
| showDownloadLinksButton.setAttribute('data-nfd-button', 'true'); | |
| table.parentElement.insertBefore(showDownloadLinksButton, table); | |
| return; | |
| } | |
| } | |
| // 2. 备用方案:尝试原来的位置 | |
| try { | |
| const target = document.querySelector('#main div')?.childNodes[1]?.querySelectorAll('li')?.[1]; | |
| if (target && !target.querySelector('[data-nfd-button]')) { | |
| showDownloadLinksButton.setAttribute('data-nfd-button', 'true'); | |
| target.appendChild(showDownloadLinksButton); | |
| } | |
| } catch (e) { | |
| console.log('Could not add button to legacy location'); | |
| } | |
| } | |
| function info() { | |
| const url = new URL(window.location.href); | |
| const segments = url.pathname.split('/').filter(Boolean); // ['package', '@scope', 'name', ...] or ['package', 'name', ...] | |
| let packageName, rest; | |
| if (segments[1]?.startsWith('@')) { | |
| packageName = `${segments[1]}/${segments[2]}`; | |
| rest = segments.slice(3); | |
| } else { | |
| packageName = segments[1]; | |
| rest = segments.slice(2); | |
| } | |
| const version = rest[0]?.startsWith('v') ? rest[0].replace(/^v\//, '').replace(/^v/, '') : 'latest'; | |
| return { packageName, version }; | |
| } | |
| function getCurrentPath() { | |
| const h2 = document.querySelector('#file-explorer-heading'); | |
| if (!h2) return ''; | |
| // h2.innerText 格式: "/@scope/name/" 或 "/@scope/name/sub/dir/" 或 "/name/" 或 "/name/sub/" | |
| const text = h2.innerText.replace(/^\//, '').replace(/\/$/, ''); // 去掉首尾 / | |
| const parts = text.split('/'); | |
| if (parts[0]?.startsWith('@')) { | |
| // scoped package: @scope/name/sub/dir → sub/dir | |
| return parts.slice(2).join('/'); | |
| } | |
| // non-scoped: name/sub/dir → sub/dir | |
| return parts.slice(1).join('/'); | |
| } | |
| function showDownloadLinks() { | |
| const { packageName, version } = info(); | |
| console.log('Package Name:', packageName); | |
| console.log('Version:', version); | |
| const domain = 'https://nfd.qer.im'; | |
| const path = getCurrentPath(); | |
| console.log('Path:', path); | |
| // 获取文件列表 | |
| const table = document.querySelector('table[aria-labelledby="file-explorer-heading"]'); | |
| if (!table) { | |
| alert('Failed to find file list table'); | |
| return; | |
| } | |
| const rows = table.querySelectorAll('tbody tr'); | |
| console.log('Found rows:', rows.length); | |
| if (rows.length === 0) { | |
| alert('No files found in the list'); | |
| return; | |
| } | |
| rows.forEach((row, index) => { | |
| // 检查是否已添加过下载按钮 | |
| if (row.querySelector('a[data-nfd-download]')) return; | |
| const cells = row.querySelectorAll('td'); | |
| if (cells.length < 2) return; | |
| const nameCell = cells[0]; | |
| const typeCell = cells[1]; | |
| const button = nameCell.querySelector('button'); | |
| if (!button) return; | |
| const fileName = button.textContent; | |
| const fileType = typeCell.textContent?.trim().toLowerCase() || ''; | |
| const isFolder = fileType === 'folder'; | |
| if (fileName === '../') return; | |
| const downloadButton = document.createElement('a'); | |
| downloadButton.style.cssText = downloadButtonCssText; | |
| downloadButton.setAttribute('data-nfd-download', 'true'); | |
| if (isFolder) { | |
| downloadButton.textContent = '(Folder)'; | |
| downloadButton.style.cssText += ` | |
| color: #666; | |
| cursor: not-allowed; | |
| opacity: 0.7; | |
| `; | |
| } else { | |
| downloadButton.textContent = 'Download'; | |
| downloadButton.style.cursor = 'pointer'; | |
| downloadButton.href = `${domain}/api/download?package=${packageName}&path=${path}&file=${encodeURIComponent(fileName)}&version=${version}`; | |
| downloadButton.addEventListener('click', function (event) { | |
| event.preventDefault(); | |
| const loadingIndicator = document.createElement('span'); | |
| loadingIndicator.textContent = 'Downloading...'; | |
| loadingIndicator.style.marginLeft = '5px'; | |
| this.parentNode.insertBefore(loadingIndicator, this.nextSibling); | |
| this.style.pointerEvents = 'none'; | |
| this.style.opacity = '0.5'; | |
| setTimeout(() => { | |
| loadingIndicator.remove(); | |
| this.style.pointerEvents = 'auto'; | |
| this.style.opacity = '1'; | |
| window.location.href = this.href; | |
| }, 2000); | |
| }); | |
| } | |
| // 将下载按钮添加到文件名单元格 | |
| nameCell.style.display = 'flex'; | |
| nameCell.style.alignItems = 'center'; | |
| nameCell.appendChild(downloadButton); | |
| }); | |
| } | |
| }()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment