Auto Simya

심챈 자동 복호화/국룰입력/다운(Kiosk, Mega, GoogleDrive, goFile)

// ==UserScript==
// @name         Auto Simya
// @name:ko      심야 자동 식당
// @namespace    http://tampermonkey.net/
// @description  심챈 자동 복호화/국룰입력/다운(Kiosk, Mega, GoogleDrive, goFile)
// @version      9.5
// @author       김머시기
// @match        https://kiosk.ac/c/*
// @match        https://kio.ac/c/*
// @match        https://arca.live/b/*
// @match        https://mega.nz/*
// @match        https://gofile.io/d/*
// @match        https://workupload.com/*
// @match        https://drive.google.com/file/d/*
// @match        https://drive.google.com/drive/folders/*
// @match        https://drive.usercontent.google.com/download?id*
// @icon         https://lh3.google.com/u/0/d/18OVO7VmnwIuHK6Ke-z7035wKFmMKZ28W=w1854-h959-iv1
// @grant        GM.setValue
// @grant        GM.getValue
// @require      https://openuserjs.org/src/libs/sizzle/GM_config.js
// @grant        GM.registerMenuCommand
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @grant        GM_getValue
// @grant        GM_setValue
// @license      MIT
// @run-at       document-end
// ==/UserScript==
'use strict';
let chkp = [,,,, atob('c21wZW9wbGU=')], Down_Option, PageLoading = [], isT = [,,], MenuID = [null, null], host = document.URL.split('/')[2], pw = [atob('c21wZW9wbGU='),
// =============================== Settings =======================================
// 추가하길 원하는 비밀번호 따옴표 - 쉼표로 구분해서 바로 아래줄에 넣으면 됨 ex) '1234', '2024국룰', '!국룰!'

];
PageLoading[0] = 1000;      // 페이지 로딩 시간 조절 (1000당 1초)
Down_Option = 0;           // Kiosk 다운로드 옵션 0:Basic, 1:Fast, 2:일괄 다운로드
// ======================================================================================
async function toggleDown(){
	isT[0]=!isT[0];
	if(!isT[0] && isT[1]){
		isT[1]=false;
		await GM.setValue('isT[1]', isT[1]);
	}
	await GM.setValue('isT[0]', isT[0]);
	updateDown();
	updateTab();
}
async function toggleTab(){
	isT[1]=!isT[1];
	if(!isT[0] && isT[1]){
		isT[0]=true;
		await GM.setValue('isT[0]', isT[0]);
	}
	await GM.setValue('isT[1]', isT[1]);
	updateDown();
	updateTab();
}
function updateDown(){
	if(MenuID[0] !==null)GM_unregisterMenuCommand(MenuID[0]);
	MenuID[0]=GM_registerMenuCommand(`자동 다운로드  ${isT[0] ? 'ON' : 'OFF'}`, toggleDown, { autoClose: false, title: `자동 다운로드 ${isT[0] ? '켜짐' : '꺼짐'}`});
}
function updateTab(){
	if(MenuID[1] !==null)GM_unregisterMenuCommand(MenuID[1]);
	MenuID[1]=GM_registerMenuCommand(`자동 탭 닫기  ${isT[1] ? 'ON' : 'OFF'}`, toggleTab, { autoClose: false, title: `자동 탭 닫기 ${isT[1] ? '켜짐' : '꺼짐'}`});
}
function doDec(){
	if (chkp[3] == chkp[4]) {
		const targets = [
			document.querySelector('body div.article-body > div.fr-view.article-content'), // 본문
			...document.querySelectorAll('div.article-comment#comment, div.article-comment') // 댓글
		];

		function dec(target, reg){
			try {
				while (reg.test(target.innerHTML)) {
					let DECed = reg.exec(target.innerHTML)[0];
					while (DECed.match(/aHR0c[0-9A-Za-z+/-]{8,}[=]{0,2}/g) == null) {
						DECed = atob(DECed);
					}
					DECed = atob(DECed);
					target.innerHTML = target.innerHTML.replace(reg, `<a href=${DECed} target='_blank' rel='noreferrer'>${DECed}</a>`);
				}
			} catch (e) { console.log(e, target); }
		}

		for (const target of targets) {
			if (!target) continue;
			dec(target, /aHR0c[0-9A-Za-z+/-]{8,}[=]{0,2}/);
			dec(target, /YUhSMG[0-9A-Za-z+/-]{8,}[=]{0,2}/);
			dec(target, /WVVoU[0-9A-Za-z+/-]{8,}[=]{1,2}/);
			dec(target, /V1ZWb[0-9A-Za-z+/-]{8,}[=]{0,2}/);
		}

		setTimeout(doDlsiteContextAware, 100);
	}
}

function doDlsiteContextAware() {
    const atc = document.querySelector('.article-body .article-content');
    const titleEl = document.querySelector('.title-row .title') || document.querySelector('.board-title .title');
    if (!atc) return;

    const keywordPattern = /(꺼|거|퍼|RJ|rj|Rj|rJ|VJ|vj|Vj|vJ|DL|dl|Dl|dL)[\s:()\[\]#-]*([0-9]{6,10})/g;
    const fullRJPattern = /\b(RJ|rj|Rj|rJ|VJ|vj|Vj|vJ|퍼|꺼|거|DL|dl|Dl|dL)([0-9]{6,10})\b/g;

    let titleText = '';
    if (titleEl?.childNodes) {
        titleText = Array.from(titleEl.childNodes)
            .map(n => n.textContent.trim())
            .join(' ');
    }

    const bodyText = atc.textContent || '';
    const fullText = titleText + '\n' + bodyText;

    const matchList1 = [...fullText.matchAll(keywordPattern)].map(m => ({ prefix: m[1], code: m[2] }));
    const matchList2 = [...fullText.matchAll(fullRJPattern)].map(m => ({ prefix: m[1], code: m[2] }));

    const linkCodes = Array.from(atc.querySelectorAll('a[href]'))
        .map(a => {
            const match = a.href.match(/(RJ|VJ)([0-9]{6,10})/i);
            return match ? { prefix: match[1].toUpperCase(), code: match[2] } : null;
        })
        .filter(Boolean);

    const splitNodeMatches = [];
    const allNodes = [...atc.querySelectorAll('*')];
    const validPrefixes = ['꺼','거','퍼','RJ','rj','Rj','rJ','VJ','vj','Vj','vJ','DL','dl','Dl','dL'];

    for (const node of allNodes) {
        const children = [...node.childNodes];
        for (let i = 0; i < children.length - 1; i++) {
            const a = children[i], b = children[i + 1];
            const aText = (a.textContent || '').replace(/\s+/g, '');
            const bText = (b.textContent || '').trim();
            if (!/^\d{6,10}$/.test(bText)) continue;
            for (const prefix of validPrefixes) {
                if (aText.includes(prefix)) {
                    splitNodeMatches.push({ prefix, code: bText });
                    break;
                }
            }
        }
    }

    const extraFromTitle = [];
    const match = titleText.match(/^[\[]?([0-9]{6,10})[\]]?/);
    if (match) extraFromTitle.push({ prefix: 'RJ', code: match[1] });

    const allCodes = [
        ...matchList1,
        ...matchList2,
        ...linkCodes,
        ...splitNodeMatches,
        ...extraFromTitle
    ];

    const seen = new Set();
    const uniqueCodes = allCodes.filter(({ code }) => {
        if (seen.has(code)) return false;
        seen.add(code);
        return true;
    });

    if (uniqueCodes.length <= 1) {
        const { code, prefix } = uniqueCodes[0];
        if (!code) return;

        const mappedPrefix = ['vj', 'vJ', 'Vj', 'VJ', '퍼'].includes(prefix.toLowerCase()) ? 'VJ'
                            : ['rj', 'rJ', 'Rj', 'RJ', '꺼', '거', 'dl', 'dL', 'Dl', 'DL'].includes(prefix.toLowerCase()) ? 'RJ'
                            : prefix.toUpperCase();

        const fullCode = `${mappedPrefix}${code}`;
        const link = `https://www.dlsite.com/maniax/work/=/product_id/${fullCode}.html`;

        const existing = atc.querySelector('.dlsite-link-appended');
        if (existing) existing.remove();

        const wrapper = document.createElement('div');
        wrapper.className = 'dlsite-link-appended';
        wrapper.style.marginTop = '15px';
        wrapper.style.padding = '10px 0';
        wrapper.style.borderTop = '1px dashed #aaa';

        const a = document.createElement('a');
        a.href = link;
        a.target = '_blank';
        a.rel = 'noreferrer';
        a.textContent = `[DLsite] ${fullCode}`;
        a.style.display = 'block';
        a.style.marginBottom = '5px';
        a.style.fontWeight = 'bold';
        a.style.color = '#1e90ff';

        wrapper.appendChild(a);
        atc.appendChild(wrapper);
        return;
    }

    const combinedPattern = new RegExp(
        `${keywordPattern.source}|${fullRJPattern.source}`,
        'gi'
    );

    const walk = (node) => {
        if (node.nodeType === Node.TEXT_NODE) {
            const text = node.textContent;
            let lastIndex = 0;
            let match;
            const frag = document.createDocumentFragment();

            while ((match = combinedPattern.exec(text)) !== null) {
                const before = text.slice(lastIndex, match.index);
                const number = match[2] || match[4];
                const prefix = (match[1] || match[3] || '').toLowerCase();
                const mappedPrefix = ['vj', 'vJ', 'Vj', 'VJ', '퍼'].includes(prefix) ? 'VJ'
                                    : ['rj', 'rJ', 'Rj', 'RJ', '꺼', '거', 'dl', 'dL', 'Dl', 'DL'].includes(prefix) ? 'RJ'
                                    : prefix.toUpperCase();
                const fullCode = `${mappedPrefix}${number}`;

                const a = document.createElement('a');
                a.href = `https://www.dlsite.com/maniax/work/=/product_id/${fullCode}.html`;
                a.textContent = fullCode;
                a.target = '_blank';
                a.rel = 'noreferrer';
                a.style.color = '#1e90ff';
                a.style.fontWeight = 'bold';

                if (before) frag.appendChild(document.createTextNode(before));
                frag.appendChild(a);
                lastIndex = match.index + match[0].length;
            }

            if (lastIndex < text.length) {
                frag.appendChild(document.createTextNode(text.slice(lastIndex)));
            }

            if (frag.childNodes.length > 0) {
                node.replaceWith(frag);
            }
        } else if (
            node.nodeType === Node.ELEMENT_NODE &&
            !['A', 'SCRIPT', 'STYLE'].includes(node.tagName)
        ) {
            for (const child of [...node.childNodes]) {
                walk(child);
            }
        }
    };

    walk(atc);
}


async function chkPW(){
	chkp[3]=await GM.getValue('chkp[3]');
	isT[0]=await GM.getValue('isT[0]', true);
	isT[1]=await GM.getValue('isT[1]', false);
	updateDown();
	updateTab();
	if(host=='arca.live'){
		if(chkp[3] !=chkp[4]){
			const chk=prompt(decodeURI(atob('JUVBJUI1JUFEJUVCJUEzJUIwJUVEJTk5JTk1JUVDJTlEJUI4')));
			if(chk?.toLowerCase()==chkp[4]) await GM.setValue('chkp[3]', chkp[4]);
			else {
				GM.setValue('chkp[3]', false);
				alert(decodeURI(atob('JUVBJUI1JUFEJUVCJUEzJUIwJUVDJTlEJUI0JTIwJUVDJTlEJUJDJUVDJUI5JTk4JUVEJTk1JTk4JUVDJUE3JTgwJTIwJUVDJTk1JThBJUVDJThBJUI1JUVCJThCJTg4JUVCJThCJUE0')));
			}
		}
	}
}
async function inputPW(){
	let inputElem=document.querySelector(chkp[0]), btnElem=document.querySelector(chkp[1]);
	if(chkp[3]==chkp[4]){
		try {
			for(let i=0; i < pw.length; i++){
				if(host=='kio.ac'){
					inputElem.value=pw[i];
					inputElem.dispatchEvent(new Event('input', { bubbles: true }));
					inputElem.dispatchEvent(new Event('change', { bubbles: true }));
					setTimeout(()=> {btnElem.click()}, 10);
				} else {
					inputElem.value=pw[i];
					btnElem.click();
				}
				if(i < pw.length - 1){ if(pw[i] !=null && pw[i] !='')await new Promise(res=> setTimeout(res, 800));
				} else { if(isT[0]==true)await setTimeout(DBtn, PageLoading[1]); }
			}
		} catch(e){ await setTimeout(DBtn, PageLoading[1]); }
	}
}
async function kioskdone(){
	try {
		await new Promise(res=> setTimeout(res, 3000));
		for(var i=0, jj=0; jj!=1; i++){
			console.log(i);
			await new Promise(res=> setTimeout(res, 1000));
			if(document.querySelector('.flex.flex-row.text-xs.justify-between div:nth-child(2)').innerText=='done'){
				console.log(i);
				await setTimeout(()=> { window.open('', '_self').close()}, 2000);
				jj++;
			}
		}
	} catch(e){ if(isT[1]==true && isT[2]==true)setTimeout(()=> { window.open('', '_self').close()}, 1000); }
}
async function DBtn(){
	if(isT[0] !== true) return; // 수정 1: 자동 다운로드 OFF 시 즉시 종료
	if(host=='mega.nz')await document.querySelector('.mega-button.positive.resume.js-resume-download').click();
	try {
		let btns = document.querySelectorAll(chkp[2]);
		let delayMultiplier = PageLoading[0] + 600;
		if(btns.length > 1){
			btns.forEach((btn, index) => {
				setTimeout(() => { btn.click(); }, index * delayMultiplier);
			});
		} else if(btns.length === 1){
			btns[0].click();
		}
		if(host=='mega.nz')
			await setTimeout(()=> { document.querySelector('.mega-button.large.positive.download.continue-download').click()}, 2000);
		if(isT[1]==true && isT[2]==true){ // 수정 2: 자동 탭 닫기 kio.ac 포함
			let totalDelay = (btns.length > 1) ? (btns.length * delayMultiplier + 700) : 1300;
			if(host=='kiosk.ac'){
				setTimeout(()=> { kioskdone(); }, totalDelay);
			} else if(host=='kio.ac'){
				setTimeout(()=> { window.open('', '_self').close()}, totalDelay);
			} else {
				setTimeout(()=> { window.open('', '_self').close() }, totalDelay);
			}
		}
	}catch(e){ console.log(e); }
}
function FindPW(){
	const atc=document.querySelector('body div.article-body > div.fr-view.article-content');
	atc.innerHTML=atc.innerHTML.replace(/(&nbsp;)/g, ' ');
	atc.innerHTML=atc.innerHTML.replace(/국룰/g, 'ㄱㄹ');
	let rgx=/[(<p>)]{0,0}[ㄱ-ㅣ가-힣0-9A-Za-z\s~`!^\_+@\#$%&=]{0,}(ㄱㄹ){1,}[ㄱ-ㅣ가-힣0-9A-Za-z\s~`!^\_+@\#($)%&=\(\)(&nbsp;)]{0,}[(</p>)]{0,0}/;
	let regexx=/(대문자)/;
	if(regexx.test(regexx.exec(atc.innerHTML))===true)pw[pw.length]=atob('U01QRU9QTEU');
	function dec(reg){
		try {
			while(reg.test(atc.innerHTML)){
				let DECed=reg.exec(atc.innerHTML)[0].replace(/(ㄱㄹ)/g, '국룰');
				let DECedd=DECed.replace(/\s|[+]|(&nbsp;)|은|는|이|(전부)|(대문자로)|(대문자)|(비밀번호)|(패스워드)|(비번)|(ㅂㅂ)|(암호)|(ㅇㅎ)|(키오스크맘)|(키오스크)/g, '');
				DECedd=DECedd.split(/[(입)(임)(이다)(이고)(이며)(입니다)(임다)(같)(처럼)(틀)]/g)[0];
				let dat=document.querySelector('.date .body').innerText.split(' ')[0];
				let regexa=/(오늘)|(날짜)|(날자)/g;
				if(regexa.test(DECedd)===true){
					DECedd=DECedd.replace(regexa, '');
					let md=[dat.split(/\-/g)[1] + dat.split(/\-/g)[2],dat.split(/\-/g)[1] +'-'+ dat.split(/\-/g)[2],dat.replace(/\-/g, ''),dat];
					for(let i=0;i<4;i++){
						pw[pw.length]=DECedd.replace(/국룰/g,chkp[4])+md[i];
					}
				} else { if(pw.indexOf(DECedd.replace(/국룰/g,chkp[4]))=='-1')pw[pw.length]=DECedd.replace(/국룰/g,chkp[4]); }
				atc.innerHTML=atc.innerHTML.replace(reg, '1ㅂㅁ2ㅈㄴ3ㄷㅇ4ㄱㄹ1qa2ws3ed4rf');
				atc.innerHTML=atc.innerHTML.replace(/(1ㅂㅁ2ㅈㄴ3ㄷㅇ4ㄱㄹ1qa2ws3ed4rf)/g,DECed);
			}
			GM.setValue('pw', pw);
		} catch(e){ console.log(e, atc); }
	}
	dec(/[(<p>)]{0,0}[ㄱ-ㅣ가-힣0-9A-Za-z\s~`!^\_+@\#$%&=]{0,}(ㄱㄹ){1,1}[ㄱ-ㅣ가-힣0-9A-Za-z\s~`!^\_+@\#$%&=]{0,}[(</p>)]{0,0}/);
	setTimeout(aaa, 100);
}
async function aaa(){ pw=await GM.getValue('pw'); }
chkPW();
if(host=='arca.live'){
	FindPW();
	setTimeout(doDec, 10);
}
if(host=='kio.ac'){
	aaa();
	chkp[0]='.overflow-auto.max-w-full.max-h-full.flex-grow.p-1 input:nth-of-type(1)';
	chkp[1]='.flex.flex-col-reverse button:nth-of-type(1)';
	chkp[2]='.p-2.align-middle .flex.align-middle button:nth-of-type(1)';
	isT[2]=true;
	PageLoading[1]=3500;
	setTimeout(inputPW, PageLoading[0] + 2000);
}
if(host=='mega.nz'){
	aaa();
	chkp[0]='#password-decrypt-input';
	chkp[1]='.mega-button.positive.fm-dialog-new-folder-button.decrypt-link-button';
	chkp[2]='.mega-button.positive.js-default-download.js-standard-download';
	PageLoading[1]=4000;
	isT[2]=false;
	setTimeout(inputPW, PageLoading[0] + 1800);
}
if(host=='kiosk.ac'){
	aaa();
	chkp[0]='.input.shadow-xl.flex-grow';
	chkp[1]='.btn.btn-ghost.w-full.mt-2.rounded-md';
	if(Down_Option==2)
		chkp[2]='.flex.justify-between.w-full .flex.gap-2 .btn.btn-ghost';
	else
		chkp[2]='#vexplorer-body .hover.cursor-pointer .flex .dropdown.group button';
	if(Down_Option==0)
		isT[2]=true;
	else
		isT[2]=false;
	PageLoading[1]=3000;
	setTimeout(inputPW, PageLoading[0]);
}
if(host=='workupload.com'){
	aaa();
	chkp[0]='#passwordprotected_file_password';
	chkp[1]='#passwordprotected_file_submit';
	chkp[2]='.btn.btn-prio';
	if(Down_Option==0)
		isT[2]=true;
	else
		isT[2]=false;
	PageLoading[1]=2000;
	setTimeout(inputPW, PageLoading[0]);
}
if(host=='drive.google.com'){
	aaa();
	if(document.URL.split('/')[3] + document.URL.split('/')[4]=='filed')
		window.location.href=`https://drive.usercontent.google.com/download?id=${document.URL.split('/')[5]}&export=download`;
	if(document.URL.split('/')[3] + document.URL.split('/')[4]=='drivefolders'){
		setTimeout(()=> { if(isT[0]==true)setTimeout(()=> { document.querySelector('.pc7nUb.kXQBpc.Dk9rmd:nth-child(2)').click()}, 1500)}, PageLoading[0]);
		chkp[2]='.h-De-Vb.h-De-Y';
		PageLoading[1]=1;
		isT[2]=true;
		setTimeout(()=> { if(isT[0]==true)setTimeout(DBtn, 3500)}, PageLoading[0]);
	}
}
if(host=='drive.usercontent.google.com'){
	aaa();
	chkp[2]='.goog-inline-block.jfk-button.jfk-button-action';
	isT[2]=true;
	setTimeout(()=> { if(isT[0]==true)DBtn()}, 1);
}
if(host=='gofile.io'){
	aaa();
	chkp[0]='#filesErrorPasswordInput';
	chkp[1]='#filesErrorPasswordButton';
	chkp[2]='.btn.btn-outline-secondary.btn-sm.p-1.text-white';
	PageLoading[1]=3000;
	isT[2]=true;
	setTimeout(inputPW, PageLoading[0]);
}