kone
소미소프트
소미소프트

코네 base64 오토 디코더

유틸
39,344 views · 22 points

클로드는 신이야!

1

딱 코네에서 base64 디코딩만하고 url만 하이퍼링크화 시켜주는 게 필요해서

클로드 한발 뽑아주니 원큐에 되더라

tampermonkey나 viloetmonkey에 추가해주면 됨

// ==UserScript==
// @name         Base64 Auto Decoder
// @namespace    http://tampermonkey.net/
// @version      1.1
// @author       you
// @match        *://*/*
// @run-at       document-idle
// @noframes
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  if (window.__b64DecoderLoaded) return;
  window.__b64DecoderLoaded = true;

  const DECODED_MARK = 'data-b64decoded';

  /* ============================== 설정 ============================== */
  const TARGET_SELECTORS = ['#post-article', '#post-comment', '.article-body', '.title'];
  const MAX_DECODE = 5;
  const MIN_B64_LEN = 7;

  const B64_CANDIDATE = new RegExp('[A-Za-z0-9+/]{' + MIN_B64_LEN + ',}={0,2}', 'g');
  const URL_REGEX = /((?:https?:\/\/|www\.)[^\s<>"'()]+)/gi;

  /* ============================== 유틸 ============================== */
  function looksLikeBase64(str) {
    return (
      str.length >= MIN_B64_LEN &&
      str.length % 4 !== 1 &&
      /^[A-Za-z0-9+/]+={0,2}$/.test(str)
    );
  }

  function isProbablyText(str) {
    if (!str) return false;
    let bad = 0;
    for (const ch of str) {
      const c = ch.codePointAt(0);
      if (c < 0x09 || (c > 0x0d && c < 0x20) || c === 0x7f) bad++;
    }
    return bad / str.length < 0.1;
  }

  function decodeOnce(str) {
    if (!looksLikeBase64(str)) return null;
    try {
      let s = str;
      const rem = s.length % 4;
      if (rem === 2) s += '==';
      else if (rem === 3) s += '=';
      const binary = atob(s);
      const bytes = Uint8Array.from(binary, (c) => c.charCodeAt(0));
      return new TextDecoder('utf-8', { fatal: true }).decode(bytes);
    } catch (e) {
      return null;
    }
  }

  function fullyDecode(str) {
    let current = str;
    let best = null;
    for (let i = 0; i < MAX_DECODE; i++) {
      const decoded = decodeOnce(current);
      if (decoded === null) break;
      if (!isProbablyText(decoded)) break;
      best = decoded;
      current = decoded;
    }
    return best;
  }

  /* ============================== DOM 조립 ============================== */
  function buildDecodedNodes(text) {
    const nodes = [];
    let last = 0;
    let m;
    URL_REGEX.lastIndex = 0;
    while ((m = URL_REGEX.exec(text)) !== null) {
      let url = m[0];
      const trailMatch = url.match(/[.,;:!?)\]}'"]+$/);
      let trail = '';
      if (trailMatch) {
        trail = trailMatch[0];
        url = url.slice(0, url.length - trail.length);
      }
      const start = m.index;
      if (start > last) nodes.push(document.createTextNode(text.slice(last, start)));

      const a = document.createElement('a');
      a.href = url.toLowerCase().startsWith('http') ? url : 'https://' + url;
      a.textContent = url;
      a.target = '_blank';
      a.rel = 'noopener noreferrer';
      nodes.push(a);

      if (trail) nodes.push(document.createTextNode(trail));
      last = start + m[0].length;
    }
    if (last < text.length) nodes.push(document.createTextNode(text.slice(last)));
    return nodes;
  }

  function processTextNode(node) {
    const text = node.nodeValue;
    if (!text) return;

    let match;
    let last = 0;
    let changed = false;
    const frag = document.createDocumentFragment();
    B64_CANDIDATE.lastIndex = 0;

    while ((match = B64_CANDIDATE.exec(text)) !== null) {
      const candidate = match[0];
      const decoded = fullyDecode(candidate);
      if (decoded === null) continue;

      changed = true;
      if (match.index > last) {
        frag.appendChild(document.createTextNode(text.slice(last, match.index)));
      }
      for (const n of buildDecodedNodes(decoded)) frag.appendChild(n);
      last = match.index + candidate.length;
    }

    if (!changed) return;
    if (last < text.length) frag.appendChild(document.createTextNode(text.slice(last)));

    const wrapper = document.createElement('span');
    wrapper.setAttribute(DECODED_MARK, '1');
    wrapper.appendChild(frag);
    node.parentNode.replaceChild(wrapper, node);
  }

  function processContainer(container) {
    const walker = document.createTreeWalker(container, NodeFilter.SHOW_TEXT, {
      acceptNode(node) {
        const p = node.parentNode;
        if (!p) return NodeFilter.FILTER_REJECT;
        const tag = p.nodeName;
        if (tag === 'SCRIPT' || tag === 'STYLE' || tag === 'A' || tag === 'TEXTAREA') {
          return NodeFilter.FILTER_REJECT;
        }
        if (node.parentElement && node.parentElement.closest('[' + DECODED_MARK + ']')) {
          return NodeFilter.FILTER_REJECT;
        }
        if (!node.nodeValue || !node.nodeValue.trim()) return NodeFilter.FILTER_REJECT;
        return NodeFilter.FILTER_ACCEPT;
      },
    });
    const targets = [];
    let n;
    while ((n = walker.nextNode())) targets.push(n);
    targets.forEach(processTextNode);
  }

  /* ====================== 실행 / 동적 콘텐츠 감시 ====================== */

  let observer = null;
  let scheduled = false;

  function runOnce() {
    TARGET_SELECTORS.forEach((sel) => {
      document.querySelectorAll(sel).forEach((el) => processContainer(el));
    });
  }

  function startObserve() {
    if (!observer) observer = new MutationObserver(() => scheduleRun());
    const root = document.body || document.documentElement;
    if (root) observer.observe(root, { childList: true, subtree: true, characterData: true });
  }

  function scheduleRun() {
    if (scheduled) return;
    scheduled = true;
    requestAnimationFrame(() => {
      scheduled = false;
      if (observer) observer.disconnect();
      runOnce();
      startObserve();
    });
  }

  runOnce();
  startObserve();
})();

22
7 comments
ㅁㅊ 너무 편하고
딱 원하는 기능하고 id나 클래스만 지정해주니 알잘딱해줌
ㄳㄳ 근데 가끔 복호화된 결과물이 두번 연속 나오는 경우가 있긴한데 왜 그런거임?
난 그런거 못봤는데
두번 되는 게시물 링크좀
All
Title
Body