Greasy Fork

Greasy Fork is available in English.

豆包&即梦图片下载无水印

禁用豆包和剪映AI平台的右键菜单,恢复浏览器默认的右键菜单,并支持图片下载

当前为 2024-12-20 提交的版本,查看 最新版本

// ==UserScript==
// @name         豆包&即梦图片下载无水印
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  禁用豆包和剪映AI平台的右键菜单,恢复浏览器默认的右键菜单,并支持图片下载
// @author       11208596
// @match        https://www.doubao.com/*
// @match        https://jimeng.jianying.com/ai-tool/*
// @grant        GM_download
// @grant        GM_xmlhttpRequest
// @license      UNLICENSED
// ==/UserScript==

(function () {
    'use strict';

    // 获取当前网站域名
    const currentDomain = window.location.hostname;

    if (currentDomain.includes('doubao')) {
        // 监听右键点击事件
        document.addEventListener('contextmenu', function (event) {
            if (event.target.tagName.toLowerCase() === 'img') {
                const imgElement = event.target;
                let imageUrl = imgElement.src;

                if (imageUrl) {
                    downloadImage(imageUrl);
                    event.preventDefault();
                }
            }
        }, true);

        // 下载图片的函数
        function downloadImage(imageUrl) {
            const fileName = getFileNameFromUrl(imageUrl);

            // 使用 GM_xmlhttpRequest 下载图片
            GM_xmlhttpRequest({
                method: 'GET',
                url: imageUrl,
                responseType: 'blob',
                headers: {
                    'Accept': 'image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
                    'Accept-Encoding': 'gzip, deflate, br',
                    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
                    'Referer': 'https://www.doubao.com/',
                    'Origin': 'https://www.doubao.com',
                    'User-Agent': navigator.userAgent
                },
                onload: function(response) {
                    if (response.status === 200) {
                        // 创建Blob URL并下载
                        const blob = response.response;
                        const url = URL.createObjectURL(blob);
                        const link = document.createElement('a');
                        link.href = url;
                        link.download = fileName;
                        document.body.appendChild(link);
                        link.click();
                        document.body.removeChild(link);
                        setTimeout(() => URL.revokeObjectURL(url), 100);
                        console.log('下载成功:', fileName);
                    } else {
                        console.error('下载失败:', response.status);
                    }
                },
                onerror: function(error) {
                    console.error('请求失败:', error);
                }
            });

            console.log(`正在下载图片: ${imageUrl}`);
        }

        // 从 URL 中提取文件名
        function getFileNameFromUrl(url) {
            // 移除查询参数
            url = url.split('?')[0];
            const urlParts = url.split('/');
            let fileName = urlParts[urlParts.length - 1];

            // 如果文件名包含处理参数,提取基础文件名
            if (fileName.includes('~')) {
                fileName = fileName.split('~')[0];
            }

            // 确保文件名有正确的扩展名
            if (!fileName.endsWith('.jpeg') && !fileName.endsWith('.jpg')) {
                fileName += '.jpeg';
            }

            return fileName;
        }
    } else if (currentDomain.includes('jianying')) {
        // 监听右键点击事件
        document.addEventListener('contextmenu', function (event) {
            if (event.target.tagName.toLowerCase() === 'img') {
                const imgElement = event.target;
                let imageUrl = imgElement.src;

                if (imageUrl) {
                    downloadJianyingImage(imageUrl);
                    event.preventDefault();
                }
            }
        }, true);

        // 下载剪映图片的函数
        function downloadJianyingImage(imageUrl) {
            const fileName = getJianyingFileName(imageUrl);

            // 使用 GM_xmlhttpRequest 下载图片
            GM_xmlhttpRequest({
                method: 'GET',
                url: imageUrl,
                responseType: 'blob',
                headers: {
                    'Accept': 'image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
                    'Accept-Encoding': 'gzip, deflate, br',
                    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
                    'Referer': 'https://jimeng.jianying.com/',
                    'Origin': 'https://jimeng.jianying.com',
                    'User-Agent': navigator.userAgent
                },
                onload: function(response) {
                    if (response.status === 200) {
                        // 创建Blob URL并下载
                        const blob = response.response;
                        const url = URL.createObjectURL(blob);
                        const link = document.createElement('a');
                        link.href = url;
                        link.download = fileName;
                        document.body.appendChild(link);
                        link.click();
                        document.body.removeChild(link);
                        setTimeout(() => URL.revokeObjectURL(url), 100);
                        console.log('下载成功:', fileName);
                    } else {
                        console.error('下载失败:', response.status);
                    }
                },
                onerror: function(error) {
                    console.error('请求失败:', error);
                }
            });

            console.log(`正在下载图片: ${imageUrl}`);
        }

        // 从 URL 中提取剪映图片文件名
        function getJianyingFileName(url) {
            const urlParts = url.split('/');
            let fileName = urlParts[urlParts.length - 1].split('?')[0];
            if (!fileName.match(/\.(jpg|jpeg|png|gif|webp)$/i)) {
                fileName += '.jpg';
            }
            return fileName;
        }
    }
})();