🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Greasy Fork is available in English.

📂 缓存分发状态(共享加速已生效)
🕒 页面同步时间:2026/01/23 14:32:19
🔄 下次更新时间:2026/01/23 15:32:19
手动刷新缓存

GitHub - Linkify package.json dependencies

Turns the names of packages into links to their homepages when looking at a package.json file

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

// ==UserScript==
// @name           GitHub - Linkify package.json dependencies
// @description    Turns the names of packages into links to their homepages when looking at a package.json file
// @author         James Skinner <[email protected]> (http://github.com/spiralx)
// @namespace      http://spiralx.org/
// @version        0.3.1
// @match          *://github.com/*/package.json
// @match          *://github.com/*%2Fpackage.json
// @grant          GM_xmlhttpRequest
// @grant          GM_getValue
// @grant          GM_setValue
// @grant          GM_registerMenuCommand
// @require        https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.js
// ==/UserScript==


(function($) {
  'use strict';
  
  function Cache(key) {
    this._key = key;
    this._data = {};
    this.load();
  }
  Cache.prototype = {
    getValue: function(k) {
      return this._data[k];
    },
    setValue: function(k, v) {
      this._data[k] = v;
      this.save();
    },
    deleteValue: function(k) {
      if (k in this._data) {
        delete this._data[k];
        this.save();
      }
    },
    hasValue: function(k) {
      return k in this._data;
    },
    listValues: function() {
      return Object.keys(this._data).sort();
    },
    clear: function() {
      this._data = {};
      this.save();
    },
    save: function() {
      var s = JSON.stringify(this._data);
      GM_setValue(this._key, s);
      console.info('Cache(' + this._key + ') saved: ' + s);
    },
    load: function(s) {
      try {
        this._data = JSON.parse(s || GM_getValue(this._key));
      }
      catch (ex) {
        this.clear();
      }
    },
    edit: function() {
      var res = window.prompt('Edit cached package URLs', JSON.stringify(this._data, null, 2));
      if (res !== null) {
        try {
          this._data = res ? JSON.parse(res) : {};
          this.save();
        }
        catch (ex) {
          console.warn('Failed to update cache data: %s %o', ex.toString(), ex);
        }
      }
    },
    toString: function() {
      return 'Cache(' + this._key + '): [' + this.listValues.join('\n') + ']';
    },
    dump: function() {
      console.log('Cache(' + this._key + '):\n' + JSON.stringify(this._data, null, 2));
    }
  };


  // --------------------------------------------------------------------------
  
  function extractWebUrl(j) {
    if (!j) {
      return;
    }

    return (typeof j === 'string' ? j : j.url)
      .replace(/^gitlab:(\S+)\/(\S+)$/, 'https://gitlab.com/$1/$2')
      .replace(/^bitbucket:(\S+)\/(\S+)$/, 'https://bitbucket.com/$1/$2')
      .replace(/^gist:(\S+?)$/, 'https://gist.github.com/$1')
      .replace(/^([^\/\s]+)\/([^\/\s]+)$/, 'https://github.com/$1/$2')
      .replace(/^git(\+https)?:\/\//, 'https://')
      .replace(/(\/svn\/trunk|\.git|\/issues)$/, '');
  }


  // --------------------------------------------------------------------------
  
  function getPackageLink(packageName, url, background) {
    return $('<a>')
      .text(packageName)
      .attr('href', url)
      .css('background-color', background || 'white');
  }


  // --------------------------------------------------------------------------

  function getPackageData(packageName, $a) {
    var url = 'http://registry.npmjs.com/' + packageName;

    GM_xmlhttpRequest({
      url: url,
      method: 'GET',
      onload: function(response) {
        var data = JSON.parse(response.responseText),
          homepage = extractWebUrl(data.repository) || extractWebUrl(data.homepage) || extractWebUrl(data.bugs);
          
        if (homepage) {
          $a
            .attr('href', homepage)
            .css('background-color', '#dfd');
          
          cachedHomepages.setValue(packageName, homepage);
        }
        else {
          console.warn(url + ': No homepage found!');
          console.dir(data);
          $a.css('background-color', '#fdd');
        }
      },
      onerror: function(response) {
        console.warn(url + ': ' + response.status + ' ' + response.statusText);
        $a.css('background-color', '#fdd');
      }
    });
  }


  // --------------------------------------------------------------------------
  
  var dependencyKeys = [
    'dependencies',
    'devDependencies',
    'peerDependencies',
    'bundleDependencies',
    'optionalDependencies'
  ];


  // --------------------------------------------------------------------------

  var cachedHomepages = new Cache('github-linkify-urls');
  // console.info(cachedHomepages);

  var
    $cont = $('.file .js-file-line-container'),
    $strings = $cont.find('.pl-s:first-of-type'),
    packageJson = JSON.parse($cont.text());

  dependencyKeys.forEach(function(k) {
    var dependencies = packageJson[k] || {};

    Object.keys(dependencies).forEach(function(packageName) {
      var homepage,
        ns = '"' + packageName + '"',
        $e = $strings.filter(function() {
          return this.textContent.trim() === ns;
        }),
        $a;

      if ($e.length === 0) {
        console.warn('No matching string found for package "' + packageName + '"!');
        return;
      }

      if (cachedHomepages.hasValue(packageName)) {
        homepage = cachedHomepages.getValue(packageName);
        // console.info('Found "' + homepage + '" for package "' + packageName + '" in cache');

        $a = getPackageLink(packageName, homepage, '#dfd');
      }
      else {
        homepage = 'http://www.npmjs.com/package/' + packageName;
        // console.info('No homepage found for package "' + packageName + '"');

        $a = getPackageLink(packageName, homepage);
        getPackageData(packageName, $a);
      }

      $e
        .empty()
        .append('<span class="pl-pds">"</span>')
        .append($a)
        .append('<span class="pl-pds">"</span>');
    });
  });


  // --------------------------------------------------------------------------

  GM_registerMenuCommand('Edit cached packages', function() {
    cachedHomepages.edit();
  }, 'p');

})(jQuery);