1DM+ Native Network Interceptor (Monkeypatch)

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

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Advertisement:

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

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

})();