SOOP 채널 프로필 & 배너 이미지 다운로드

Soop 채널(방송국)의 프로필 이미지와 배너 이미지를 다운로드

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         SOOP 채널 프로필 & 배너 이미지 다운로드
// @namespace    http://tampermonkey.net/
// @version      2.7
// @description  Soop 채널(방송국)의 프로필 이미지와 배너 이미지를 다운로드
// @author       WakViewer
// @match        https://ch.sooplive.co.kr/*
// @icon         https://res.sooplive.co.kr/afreeca.ico
// @grant        GM_download
// @grant        unsafeWindow
// @grant        GM_addStyle
// @grant        GM_xmlhttpRequest
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @run-at       document-end
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    let shortcutKey = GM_getValue('shortcutKey', 'Y').toLowerCase();

    // Helper function to download an image
    function downloadImage(url, filename) {
        GM_download({
            url: url,
            name: filename,
            onerror: (err) => {
                console.error(`Failed to download ${filename}:`, err);
            }
        });
    }

    // Function to show toast message using Soop's default style
    function showToast(message) {
        const toastContainer = document.querySelector('#toastr > div');
        if (toastContainer) {
            const toastMessage = document.createElement('div');
            toastMessage.classList.add('warning');
            toastMessage.innerHTML = `<p>${message.replace(/\n/g, '<br>')}</p><button type="button" class="close"><span>닫기</span></button>`;
            toastContainer.appendChild(toastMessage);

            setTimeout(() => {
                toastMessage.remove();
            }, 3000);
        } else {
            console.warn('Toast container not found!');
        }
    }

    // Get streamer name for filenames
    function getStreamerName() {
        const nameElement = document.querySelector('#bs-navi > div > article.article_bj_box > section > div > div > h2');
        return nameElement ? nameElement.textContent.trim() : 'streamer';
    }

    // Functions to download profile and banner images
    function downloadProfile() {
        const streamerName = getStreamerName();
        const profileImg = document.querySelector('#bs-navi > div > article.article_bj_box > section > a > div > img');
        if (profileImg && profileImg.src) {
            downloadImage(profileImg.src, `${streamerName} 프로필.jpg`);
            showToast('프로필 이미지 다운로드 완료!');
        } else {
            showToast('프로필 이미지가 없습니다!');
        }
    }

    function downloadBanner() {
        const streamerName = getStreamerName();
        const bannerSection = document.querySelector('#bs-contents > article.bs-contents-header.expansion > section.ch-art');
        if (bannerSection) {
            const bannerStyle = getComputedStyle(bannerSection).backgroundImage;
            const bannerUrl = bannerStyle.match(/url\((['"]?)(https?:\/\/[^\)]+)\1\)/)?.[2] || bannerStyle.match(/url\((['"]?)(\/\/[^\)]+)\1\)/)?.[2];
            const defaultBanner = 'https://res.sooplive.co.kr/images/bj/img_ch-default.png';

            if (bannerUrl && !bannerUrl.includes(defaultBanner)) {
                const fullBannerUrl = bannerUrl.startsWith('//') ? 'https:' + bannerUrl : bannerUrl;
                downloadImage(fullBannerUrl, `${streamerName} 배너.jpg`);
                showToast('배너 이미지 다운로드 완료!');
            } else {
                showToast('배너 이미지가 없습니다!');
            }
        } else {
            showToast('배너 이미지가 없습니다!');
        }
    }

    function downloadBoth() {
        downloadProfile();
        downloadBanner();
    }

    // Shortcut key setting function
    function setShortcutKey() {
        const newKey = prompt('새로운 단축키를 입력하세요!\n\n( 예: Y, Ctrl+Y, Alt+Y, F7 )', shortcutKey);
        if (newKey) {
            GM_setValue('shortcutKey', newKey.toLowerCase());
            shortcutKey = newKey.toLowerCase();
            showToast(`단축키 설정 완료: ${newKey}`);
        } else {
            showToast('단축키 설정이 취소되었습니다.');
        }
    }

    // Register the download commands in the GM menu
    GM_registerMenuCommand('프로필/배너 모두 다운로드', downloadBoth);
    GM_registerMenuCommand('프로필 이미지만 다운로드', downloadProfile);
    GM_registerMenuCommand('배너 이미지만 다운로드', downloadBanner);
    GM_registerMenuCommand('단축키 설정', setShortcutKey);

    // Shortcut key event listener
    document.addEventListener('keydown', function(event) {
        let keyPressed = '';
        if (event.ctrlKey) keyPressed += 'ctrl+';
        if (event.shiftKey) keyPressed += 'shift+';
        if (event.altKey) keyPressed += 'alt+';
        keyPressed += event.key.toLowerCase();

        if (keyPressed === shortcutKey) {
            downloadBoth();
        }
    });
})();