GitHub JsDelivr Link

open github jsdelivr link

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

  1. // ==UserScript==
  2. // @name GitHub JsDelivr Link
  3. // @namespace https://github.com/hungtcs
  4. // @version 2024-12-10
  5. // @description open github jsdelivr link
  6. // @author hungtcs
  7. // @license MIT
  8. // @match https://github.com/**/*
  9. // @icon https://camo.githubusercontent.com/6a5d2046028682a99b5fa88ef0f3399c9bced1d514179686a3973a323bccbf44/68747470733a2f2f7777772e6a7364656c6976722e636f6d2f69636f6e5f323536783235362e706e67
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. const iconUrl = 'https://camo.githubusercontent.com/6a5d2046028682a99b5fa88ef0f3399c9bced1d514179686a3973a323bccbf44/68747470733a2f2f7777772e6a7364656c6976722e636f6d2f69636f6e5f323536783235362e706e67'
  17.  
  18. function run() {
  19. const copyPathButton = document.querySelector('button[aria-label="Copy path"]');
  20. if (!copyPathButton) {
  21. return;
  22. }
  23.  
  24. const url = new URL(window.location.href);
  25. const { pathname } = url;
  26. const isFile = pathname.includes('/blob/')
  27. const isFolder = pathname.includes('/tree/')
  28.  
  29. let link;
  30. if (isFolder) {
  31. const index = pathname.indexOf('/tree/');
  32. const author = pathname.slice(1, index);
  33. let rest = pathname.slice(index + 6);
  34. const version = rest.slice(0, rest.indexOf('/'))
  35. const filepath = rest.slice(rest.indexOf('/'));
  36. link = `https://cdn.jsdelivr.net/gh/${ author }@${version}${filepath}/`;
  37. } else if (isFile) {
  38. const index = pathname.indexOf('/blob/');
  39. const author = pathname.slice(1, index);
  40. let rest = pathname.slice(index + 6);
  41. const version = rest.slice(0, rest.indexOf('/'))
  42. const filepath = rest.slice(rest.indexOf('/'));
  43. link = `https://cdn.jsdelivr.net/gh/${ author }@${version}${filepath}`;
  44. } else {
  45. return;
  46. }
  47.  
  48. let actionButton = document.querySelector('button[aria-label="Open JsDelivr Link"]')
  49. if (!actionButton) {
  50. const copyPathButtonWrapper = copyPathButton.parentElement;
  51. const container = copyPathButtonWrapper.parentElement
  52. const actionNode = copyPathButtonWrapper.cloneNode(true)
  53. actionButton = actionNode.querySelector('button')
  54. actionButton.setAttribute('title', 'Open JsDelivr Link')
  55. actionButton.setAttribute('aria-label', 'Open JsDelivr Link')
  56. actionButton.innerHTML = `<img src=${iconUrl} style="height: 16px;" />`;
  57. container.appendChild(actionNode);
  58. }
  59.  
  60.  
  61. actionButton.onclick = () => {
  62. window.open(link);
  63. };
  64. }
  65.  
  66. setInterval((event) => {
  67. run();
  68. }, 1000);
  69. })();