1DM+ Native Network Interceptor (Monkeypatch)

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

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

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

Tendrás que instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Tendrás que instalar una extensión como Tampermonkey antes de poder instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Advertisement:

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

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);

})();