Select to decode base64

Automatically decodes Base64 encoded text

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Select to decode base64
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Automatically decodes Base64 encoded text
// @author       raculus
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
document.addEventListener('mouseup', handleSelection);
document.addEventListener('keyup', handleSelection);

function handleSelection() {
    const selectedText = window.getSelection().toString();

    if (selectedText) {
        console.log('선택된 텍스트:', selectedText);

        // Base64 디코딩 시도
        const decodedText = decodeBase64(selectedText);

        if (decodedText) {
            console.log('Base64 디코딩 성공:', decodedText);

            replaceSelectedContent(decodedText);
        } else {
            console.log('선택된 텍스트는 Base64 형식이 아닙니다.');
        }
    }
}

// Base64 디코딩 함수
function decodeBase64(text) {
    try {
        return atob(text);
    } catch (e) {
        return null; // Base64 디코딩 실패 시 null 반환
    }
}

// 선택된 내용을 대체하는 함수 (텍스트 또는 링크)
function replaceSelectedContent(content) {
    const selection = window.getSelection();

    if (!selection.rangeCount) return;

    const range = selection.getRangeAt(0);
    range.deleteContents(); // 기존 선택 영역의 텍스트 삭제

    let newNode;
    if ( isValidURL(content) ) {
        // <a> 태그 생성 및 설정
        newNode = document.createElement('a');
        newNode.href = content;
        newNode.textContent = content;
        newNode.style.color = 'blue'; // 링크 스타일 설정 (선택 사항)
        newNode.style.textDecoration = 'underline'; // 밑줄 추가
    } else {
        // 일반 텍스트 노드 삽입
        newNode = document.createTextNode(content);
    }

    range.insertNode(newNode);

    // 선택 영역 초기화
    selection.removeAllRanges();
    const newRange = document.createRange();
    newRange.selectNode(newNode);
    selection.addRange(newRange);
}

// URL 유효성 검증 함수
function isValidURL(string) {
    try {
        new URL(string);
        return true;
    } catch (_) {
        return false;
    }
}
})();