npmjs

npm下载量查看

当前为 2019-11-22 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         npmjs
// @description  npm下载量查看
// @namespace    npm_script
// @version      1.0
// @author       vizo
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js
// @include      *://*npmjs.com/search*
// @run-at       document-end
// @grant        GM_addStyle
// @grant        GM_setValue
// @grant        GM_getValue
// @noframes

// @grant        GM_getResourceText
// @grant        GM_xmlhttpRequest
// @connect      *

// ==/UserScript==

'use strict'

GM_addStyle(`
  .spr7s {
    color: #f00;
    font-size: 12px;
    font-weight: normal;
    margin-left: 10px;
    font-family: Arial;
    font-style: italic;
  }
`)

$(function () {
  
  function initScript() {
    let observer = new MutationObserver((mutations, observer) => {
      eachItem()
    })
    observer.observe(document.querySelector('.pt2-ns'), {
      childList: true,   //子节点的变动
      attributes: false,  //属性的变动
      characterData: true,  //节点内容或节点文本的变动
      subtree: false,   //是否将该观察器应用于该节点的所有后代节点
    })
    eachItem()
  }
  
  function reqItemDetail(url, domTit) {
    GM_xmlhttpRequest({
      url,
      method: 'get',
      onload: function(xhr) {
        try {
          let text = xhr.response
          let downNum = text.replace(/[\n\r\f]/g, '').replace(/.+?pb1\">([\d\,]+)\<\/p>.+/g, '$1')
          domTit.append(`<span class="spr7s">${downNum}</span>`)
        } catch (e) {}
      },
    })
  }
  
  // 遍历item
  function eachItem() {
    $('.pt2-ns .pl1-ns').each(function() {
      let tis = $(this)
      let url = tis.find('.flex-row a').attr('href')
      let h3 = tis.find('.flex-row a h3.fw6')
      reqItemDetail(url, h3)
    })
  }
  
  initScript()
})