NpmPackageRepo

See if npm package repo is forked from others

  1. // ==UserScript==
  2. // @name NpmPackageRepo
  3. // @namespace mailto:fish404hsif@gmail.com
  4. // @version 0.1.0
  5. // @description See if npm package repo is forked from others
  6. // @author fish-404
  7. // @match https://www.npmjs.com/package/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=npmjs.com
  9. // @homepage https://github.com/fish-404/UserScriptsStyles/tree/main/npm/Scripts
  10. // @license MIT
  11. // @grant GM.xmlHttpRequest
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16. let link = document.querySelector('#repository-link').innerText.substring(11);
  17. let repo = document.querySelector('#repository');
  18.  
  19. const notFoundHtml = '<img src="https://badgen.net/badge/status/404/red" alt="Repository Not Found Badge"/>'
  20.  
  21. if (link) {
  22. GM.xmlHttpRequest( {
  23. method: "GET",
  24. url: `https://api.github.com/repos/${link}`,
  25. headers: {
  26. "accept": "application/vnd.github+json"
  27. },
  28. onload: function(response) {
  29. if (response.status = '200') {
  30. if (response.statusText === "Not Found") {
  31. insertBadge(repo, notFoundHtml);
  32. }
  33. else {
  34. let cur = JSON.parse(response.response);
  35. let curForkHtml = `<img src="https://badgen.net/badge/Forks/${cur.forks}/blue" alt="Repository Total Fork Badge"/>`;
  36. if (cur.fork === true) {
  37. insertBadge(repo, curForkHtml);
  38. insertBadge(repo,
  39. `<a href="${cur.parent.html_url}">
  40. <img src="https://badgen.net/badge/Parent Forks/${cur.parent.forks}/orange" alt="Respository Parent Fork Badge" />
  41. </a>`);
  42. }
  43. else {
  44. insertBadge(repo, curForkHtml);
  45. }
  46. }
  47. }
  48. }
  49. });
  50. }
  51. else {
  52. console.info("Repository link not found");
  53. }
  54. })();
  55.  
  56. function insertBadge(element, htmlText) {
  57. element.insertAdjacentHTML('afterend', htmlText);
  58. }