Greasy Fork is available in English.

1DM+ Native Network Interceptor (Monkeypatch)

Aggressively intercepts programmatic navigation and clicks to route to 1DM+.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, Greasemonkey alebo Violentmonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey alebo Userscripts.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie správcu používateľských skriptov.

(Už mám správcu používateľských skriptov, nechajte ma ho nainštalovať!)

Advertisement:

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

(Už mám správcu používateľských štýlov, nechajte ma ho nainštalovať!)

Advertisement:

// ==UserScript==
// @name         1DM+ Native Network Interceptor (Monkeypatch)
// @namespace    http://violentmonkey.net/
// @version      2.0
// @description  Aggressively intercepts programmatic navigation and clicks to route to 1DM+.
// @match        *://*/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    // Target the specific 1DM+ package
    const packageName = 'idm.internet.download.manager.plus';
    
    // Heuristics for what looks like a standard file
    const fileExtensions = /\.(apk|zip|rar|7z|mp4|mkv|avi|mp3|flac|pdf|iso|jpg|jpeg|png|webp)$/i;

    function fireIntent(url) {
        // We cannot route in-memory Blobs or Data URIs to an external Android app
        if (!url || url.startsWith('blob:') || url.startsWith('data:')) {
            console.warn("1DM+ Interceptor: Cannot route blob/data URIs via Intent. The browser must handle this.");
            return false;
        }
        
        const targetUrl = url.replace(/^https?:\/\//, '');
        const scheme = url.startsWith('https') ? 'https' : 'http';
        const intentUrl = `intent://${targetUrl}#Intent;scheme=${scheme};package=${packageName};end`;
        
        console.log("Network Intercepted! Routing to 1DM+:", intentUrl);
        window.location.href = intentUrl;
        return true;
    }

    function isDownload(url, isAnchor) {
        if (!url) return false;
        if (fileExtensions.test(url)) return true;
        // Catch API endpoints that contain 'download' (like Unsplash)
        if (isAnchor || url.toLowerCase().includes('download')) return true; 
        return false;
    }

    // 1. Monkeypatch window.open
    // Catches sites that attempt to open a new tab directly to a file or API endpoint
    const originalWindowOpen = window.open;
    window.open = function(url, target, features) {
        if (isDownload(url, false)) {
            if (fireIntent(url)) return null; // Kill the native window open
        }
        return originalWindowOpen.call(window, url, target, features);
    };

    // 2. Monkeypatch HTMLAnchorElement.prototype.click
    // Catches modern Single Page Applications (SPAs) that dynamically create a hidden <a> tag and click it
    const originalAnchorClick = HTMLAnchorElement.prototype.click;
    HTMLAnchorElement.prototype.click = function() {
        if (isDownload(this.href, this.hasAttribute('download'))) {
            if (fireIntent(this.href)) return; // Kill the native click execution
        }
        return originalAnchorClick.call(this);
    };

    // 3. Global capture phase listener for standard DOM clicks
    document.addEventListener('click', function(event) {
        const target = event.target.closest('a');
        if (!target || !target.href) return;

        if (isDownload(target.href, target.hasAttribute('download'))) {
            event.preventDefault();
            event.stopPropagation();
            fireIntent(target.href);
        }
    }, true);

})();