1DM+ Native Network Interceptor (Monkeypatch)

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

Advertisement:

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

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

})();