Last active
December 20, 2024 09:23
-
-
Save ahmedazhar05/9e011d217c11ae68bb8dc4cdb9c309bf to your computer and use it in GitHub Desktop.
Convert npm, github and wordpress-svn repositories into JsDelivr CDN with this bookmarklet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript:(function(){ | |
const host = window.location.hostname; | |
const path = window.location.pathname; | |
let destination_path = ""; | |
switch(host) { | |
case 'themes.svn.wordpress.org': | |
destination_path = '/themes'; | |
case 'plugins.svn.wordpress.org': | |
destination_path = `/wp${destination_path}${path}`; | |
break; | |
case 'github.com': | |
case 'raw.github.com': | |
case 'raw.githubusercontent.com': | |
const path_segments = path.split('/'); | |
if(path_segments.length < 3) { | |
alert(`JsDelivr CDN doesn't work on this GitHub URL: ${host}${path}`); | |
return; | |
} | |
const index = host.startsWith('raw.') ? 3 : 4; | |
destination_path = `/gh${path_segments.slice(0, 3).join('/')}`; | |
destination_path += path_segments.length > 3 ? `@${path_segments.slice(index).join('/')}` : "/"; | |
if(!host.startsWith('raw.github') && path_segments[3] === 'tree') destination_path += '/'; | |
break; | |
case 'www.npmjs.com': | |
case 'npmjs.com': | |
const file_path = new URLSearchParams(window.location.search).get('activeTab')?.toLowerCase() === 'code' ? document.getElementById('tabpanel-explore').getElementsByTagName('h2')[0].textContent.slice(window.__context__.context.packageVersion.name.length + 1) : "/"; | |
const subpath = `/package/${window.__context__.context.packageVersion.name}/v/`; | |
const version = window.location.pathname.startsWith(`${subpath}${window.__context__.context.packageVersion.version}`) ? `@${window.__context__.context.packageVersion.version}` : window.location.pathname.startsWith(subpath) ? `@${window.location.pathname.match(new RegExp(`${subpath}([\\d\\.]+)$`))[1]}` : ""; | |
destination_path = `/npm/${window.__context__.context.packageVersion.name}${version}${file_path}`; | |
break; | |
default: | |
alert(`JsDelivr CDN doesn't work on this host: ${host}`); | |
return; | |
} | |
window.location = `https://cdn.jsdelivr.net${destination_path}`; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment