Download Media with Cobalt API

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Download Media with Cobalt API
// @namespace    https://github.com/tizee/tempermonkey-download-with-cobalt
// @version      1.1
// @description  Adds a context menu item to download media from supported video sites using a direct URL.
// @author       tizee
// @match        *://*.bilibili.com/video/*
// @match        *://*.instagram.com/p/*
// @match        *://*.instagram.com/reels/*
// @match        *://*.instagram.com/reel/*
// @match        *://*.twitter.com/*/status/*
// @match        *://*.twitter.com/*/status/*/video/*
// @match        *://*.twitter.com/i/spaces/*
// @match        *://*.x.com/*/status/*
// @match        *://*.x.com/*/status/*/video/*
// @match        *://*.x.com/i/spaces/*
// @match        *://*.reddit.com/r/*/comments/*/*
// @match        *://*.soundcloud.com/*
// @match        *://*.soundcloud.app.goo.gl/*
// @match        *://*.tumblr.com/post/*
// @match        *://*.tumblr.com/*/*
// @match        *://*.tumblr.com/*/*/*
// @match        *://*.tumblr.com/blog/view/*/*
// @match        *://*.tiktok.com/*
// @match        *://*.vimeo.com/*
// @match        *://*.youtube.com/watch?*
// @match        *://*.youtu.be/*
// @match        *://*.youtube.com/shorts/*
// @match        *://*.vk.com/video*
// @match        *://*.vk.com/*?w=wall*
// @match        *://*.vk.com/clip*
// @match        *://*.vine.co/*
// @match        *://*.streamable.com/*
// @match        *://*.pinterest.com/pin/*
// @match        *://*.xiaohongshu.com/explore/*
// @grant        GM_registerMenuCommand
// @grant        GM_openInTab
// @grant        GM_getValue
// @grant        GM_setValue
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const defaultApiUrl = 'cobalt.tools'; // Removed https:// prefix
    let apiUrl = GM_getValue('apiurl', defaultApiUrl); // Load from storage

    // Function to set/update the API URL
    function setApiUrl() {
        let newApiUrl = prompt("Enter Cobalt API URL (e.g., cobalt.tools):", apiUrl); // Adjusted prompt
        if (newApiUrl !== null) {
            apiUrl = newApiUrl;
            GM_setValue('apiurl', apiUrl);
            alert(`API URL set to: ${apiUrl}`);
        }
    }

    // Add menu command to configure the API URL
    GM_registerMenuCommand("Set Cobalt API URL", setApiUrl);


    function downloadItem(targetUrl) {
        let au = apiUrl;
        if (!au.startsWith("https://") && !au.startsWith("http://")) au = `https://` + au; // Add protocol if missing
        if (!au.endsWith("/")) au = au + '/'; // Add trailing slash if missing
        au = au + `?u=${encodeURIComponent(targetUrl)}`; // Properly encode the target URL

        GM_openInTab(au, { active: true });
    }


    GM_registerMenuCommand("Download Media (Cobalt API)", () => {
        downloadItem(window.location.href);
    });

})();