Download Media with Cobalt API

Adds a context menu item to download media from supported video sites using a direct URL.

  1. // ==UserScript==
  2. // @name Download Media with Cobalt API
  3. // @namespace https://github.com/tizee/tempermonkey-download-with-cobalt
  4. // @version 1.1
  5. // @description Adds a context menu item to download media from supported video sites using a direct URL.
  6. // @author tizee
  7. // @match *://*.bilibili.com/video/*
  8. // @match *://*.instagram.com/p/*
  9. // @match *://*.instagram.com/reels/*
  10. // @match *://*.instagram.com/reel/*
  11. // @match *://*.twitter.com/*/status/*
  12. // @match *://*.twitter.com/*/status/*/video/*
  13. // @match *://*.twitter.com/i/spaces/*
  14. // @match *://*.x.com/*/status/*
  15. // @match *://*.x.com/*/status/*/video/*
  16. // @match *://*.x.com/i/spaces/*
  17. // @match *://*.reddit.com/r/*/comments/*/*
  18. // @match *://*.soundcloud.com/*
  19. // @match *://*.soundcloud.app.goo.gl/*
  20. // @match *://*.tumblr.com/post/*
  21. // @match *://*.tumblr.com/*/*
  22. // @match *://*.tumblr.com/*/*/*
  23. // @match *://*.tumblr.com/blog/view/*/*
  24. // @match *://*.tiktok.com/*
  25. // @match *://*.vimeo.com/*
  26. // @match *://*.youtube.com/watch?*
  27. // @match *://*.youtu.be/*
  28. // @match *://*.youtube.com/shorts/*
  29. // @match *://*.vk.com/video*
  30. // @match *://*.vk.com/*?w=wall*
  31. // @match *://*.vk.com/clip*
  32. // @match *://*.vine.co/*
  33. // @match *://*.streamable.com/*
  34. // @match *://*.pinterest.com/pin/*
  35. // @match *://*.xiaohongshu.com/explore/*
  36. // @grant GM_registerMenuCommand
  37. // @grant GM_openInTab
  38. // @grant GM_getValue
  39. // @grant GM_setValue
  40. // @license MIT
  41. // ==/UserScript==
  42.  
  43. (function() {
  44. 'use strict';
  45.  
  46. const defaultApiUrl = 'cobalt.tools'; // Removed https:// prefix
  47. let apiUrl = GM_getValue('apiurl', defaultApiUrl); // Load from storage
  48.  
  49. // Function to set/update the API URL
  50. function setApiUrl() {
  51. let newApiUrl = prompt("Enter Cobalt API URL (e.g., cobalt.tools):", apiUrl); // Adjusted prompt
  52. if (newApiUrl !== null) {
  53. apiUrl = newApiUrl;
  54. GM_setValue('apiurl', apiUrl);
  55. alert(`API URL set to: ${apiUrl}`);
  56. }
  57. }
  58.  
  59. // Add menu command to configure the API URL
  60. GM_registerMenuCommand("Set Cobalt API URL", setApiUrl);
  61.  
  62.  
  63. function downloadItem(targetUrl) {
  64. let au = apiUrl;
  65. if (!au.startsWith("https://") && !au.startsWith("http://")) au = `https://` + au; // Add protocol if missing
  66. if (!au.endsWith("/")) au = au + '/'; // Add trailing slash if missing
  67. au = au + `?u=${encodeURIComponent(targetUrl)}`; // Properly encode the target URL
  68.  
  69. GM_openInTab(au, { active: true });
  70. }
  71.  
  72.  
  73. GM_registerMenuCommand("Download Media (Cobalt API)", () => {
  74. downloadItem(window.location.href);
  75. });
  76.  
  77. })();