NPM Package Size from BundlePhobia

12/12/2021, 12:45:13 PM

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        NPM Package Size from BundlePhobia
// @namespace   Violentmonkey Scripts
// @match       https://www.npmjs.com/package/*
// @grant       GM_xmlhttpRequest
// @version     1.2
// @author      dutzi
// @license     MIT
// @description 12/12/2021, 12:45:13 PM
// ==/UserScript==

function prettyPrintSize(size) {
  if (size < 1000) {
    return size.toLocaleString('en') + ' bytes'
  } else {
    return (Math.floor(size / 100) / 10).toLocaleString('en') + ' kB'
  }
}

const interval = setInterval(async () => {
  const container = document.querySelector('[title="Copy Command to Clipboard"]').parentElement.parentElement
  
  if (!container) {
    return
  }
  
  const el = document.createElement('a')
  el.innerHTML = 'Fetching stats from BundlePhobia...'
  container.prepend(el)  

  clearInterval(interval)
  
  let prevLocation = '';
  
  setInterval(updateStats, 100)
  updateStats()
  
  function updateStats() {
    if (prevLocation === window.location.href) {
      return
    }
    
    prevLocation = window.location.href;
    
    const packageName = window.location.href.split('?')[0].split('/').slice(4).join('/')

    el.href = `https://bundlephobia.com/package/${packageName}`
    
    Object.assign(el.style, {
      textDecoration: 'none',
      display: 'block',
      border: '2px solid #65c3f8',
      color: '#65c3f8',
      padding: '0.75rem 1rem',
      borderRadius: '4px',
      transition: 'all 0.5s ease-out'
    })

    GM_xmlhttpRequest({
      method: 'GET',
      url: `https://bundlephobia.com/api/size?package=${packageName}`,
      onload: ({response}) => {
        const data = JSON.parse(response);
        const { size, gzip } = data;
        el.innerHTML = `Size: <strong>${prettyPrintSize(size)}</strong>, Gzipped: <strong>${prettyPrintSize(gzip)}</strong>`  
      }
    })    
  }
  
}, 100)