1DM+ Native Network Interceptor (Monkeypatch)

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

Advertisement:

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

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

})();