Skip to content

Instantly share code, notes, and snippets.

@ahmedazhar05
Last active January 9, 2024 08:05
Show Gist options
  • Save ahmedazhar05/9e011d217c11ae68bb8dc4cdb9c309bf to your computer and use it in GitHub Desktop.
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
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 version = path.startsWith(`/package/${window.__context__.context.packageVersion.name}/v/${window.__context__.context.packageVersion.version}`) ? `@${window.__context__.context.packageVersion.version}` : "";
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