Xem nguồn trang web

Hiển thị và tìm kiếm mã nguồn của trang web hiện tại trong một cửa sổ modal và cho phép sao chép mã nguồn.

// ==UserScript==
// @name         Xem nguồn trang web
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Hiển thị và tìm kiếm mã nguồn của trang web hiện tại trong một cửa sổ modal và cho phép sao chép mã nguồn.
// @license      MIT
// @author       TieuThanhNhi
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Tạo một biểu tượng nút nhỏ để hiển thị nguồn của trang web
    var viewSourceButton = document.createElement('button');
    viewSourceButton.innerHTML = '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M3 12L21 12M21 12L14 5M21 12L14 19" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>';
    viewSourceButton.style.position = 'fixed';
    viewSourceButton.style.bottom = '20px';
    viewSourceButton.style.right = '20px';
    viewSourceButton.style.zIndex = '9999';
    viewSourceButton.style.backgroundColor = '#007bff';
    viewSourceButton.style.color = '#ffffff';
    viewSourceButton.style.border = 'none';
    viewSourceButton.style.borderRadius = '50%';
    viewSourceButton.style.width = '40px';
    viewSourceButton.style.height = '40px';
    viewSourceButton.style.padding = '0';
    viewSourceButton.style.cursor = 'pointer';
    viewSourceButton.style.display = 'flex';
    viewSourceButton.style.justifyContent = 'center';
    viewSourceButton.style.alignItems = 'center';
    viewSourceButton.addEventListener('click', function() {
        // Tạo một cửa sổ modal
        var modal = document.createElement('div');
        modal.style.position = 'fixed';
        modal.style.top = '0';
        modal.style.left = '0';
        modal.style.width = '100%';
        modal.style.height = '100%';
        modal.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
        modal.style.zIndex = '99999';
        modal.style.display = 'flex';
        modal.style.flexDirection = 'column';
        modal.style.justifyContent = 'center';
        modal.style.alignItems = 'center';
        modal.addEventListener('click', function(event) {
            if (event.target === modal) {
                document.body.removeChild(modal);
            }
        });

        // Tạo một phần tử mới để chứa nội dung modal
        var modalContent = document.createElement('div');
        modalContent.style.backgroundColor = '#ffffff';
        modalContent.style.padding = '20px';
        modalContent.style.borderRadius = '10px';
        modalContent.style.maxWidth = '90%';
        modalContent.style.maxHeight = '90%';
        modalContent.style.overflow = 'auto';
        modalContent.style.display = 'flex';
        modalContent.style.flexDirection = 'column';
        modalContent.style.justifyContent = 'center';
        modalContent.style.alignItems = 'center';

        // Hiển thị mã nguồn HTML của trang web trong cửa sổ modal
        var sourceCode = document.createElement('pre');
        sourceCode.innerHTML = escapeHTML(document.documentElement.outerHTML);
        sourceCode.style.backgroundColor = '#f0f0f0';
        sourceCode.style.padding = '15px';
        sourceCode.style.borderRadius = '5px';
        sourceCode.style.fontFamily = 'monospace';
        sourceCode.style.overflowX = 'auto';
        sourceCode.style.width = '100%';
        sourceCode.style.marginBottom = '20px';

        // Thêm ô tìm kiếm
        var searchInput = document.createElement('input');
        searchInput.type = 'text';
        searchInput.placeholder = 'Tìm kiếm trong mã nguồn';
        searchInput.style.padding = '10px';
        searchInput.style.border = '1px solid #ccc';
        searchInput.style.borderRadius = '5px';
        searchInput.style.width = '100%';
        searchInput.addEventListener('input', function() {
            highlightText(sourceCode, searchInput.value);
        });
        modalContent.appendChild(searchInput);

        // Thêm thông tin tác giả và giấy phép
        var authorInfo = document.createElement('p');
        authorInfo.innerHTML = 'Script by TieuThanhNhi';
        authorInfo.style.fontWeight = 'bold';
        authorInfo.style.marginBottom = '10px';

        var licenseInfo = document.createElement('p');
        licenseInfo.innerHTML = 'License: MIT';
        licenseInfo.style.fontStyle = 'italic';
        licenseInfo.style.marginBottom = '10px';

        // Thêm nút sao chép
        var copyButton = document.createElement('button');
        copyButton.innerHTML = 'Sao chép mã nguồn';
        copyButton.style.backgroundColor = '#28a745';
        copyButton.style.color = '#ffffff';
        copyButton.style.border = 'none';
        copyButton.style.borderRadius = '5px';
        copyButton.style.padding = '10px 20px';
        copyButton.style.cursor = 'pointer';
        copyButton.style.marginBottom = '20px';
        copyButton.addEventListener('click', function() {
            copyToClipboard(sourceCode.innerText);
            alert('Đã sao chép mã nguồn!');
        });

        // Tạo nút đóng
        var closeButton = document.createElement('button');
        closeButton.innerHTML = 'Đóng';
        closeButton.style.backgroundColor = '#dc3545';
        closeButton.style.color = '#ffffff';
        closeButton.style.border = 'none';
        closeButton.style.borderRadius = '5px';
        closeButton.style.padding = '10px 20px';
        closeButton.style.cursor = 'pointer';
        closeButton.addEventListener('click', function() {
            document.body.removeChild(modal);
        });

        modalContent.appendChild(authorInfo);
        modalContent.appendChild(licenseInfo);
        modalContent.appendChild(sourceCode);
        modalContent.appendChild(copyButton);
        modalContent.appendChild(closeButton);
        modal.appendChild(modalContent);
        document.body.appendChild(modal);
    });
    document.body.appendChild(viewSourceButton);

    // Hàm để tránh việc mã HTML bị chèn vào trang web
    function escapeHTML(html) {
        return html.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
    }

     // Hàm tô đậm văn bản tìm kiếm trong mã nguồn
    function highlightText(element, searchText) {
        var regex = new RegExp(searchText, 'gi');
        var sourceCode = element.innerHTML;
        var highlightedCode = sourceCode.replace(regex, function(match) {
            return '<span style="background-color: yellow;">' + match + '</span>';
        });
        element.innerHTML = highlightedCode;
    }

    // Hàm sao chép vào clipboard
    function copyToClipboard(text) {
        var tempTextArea = document.createElement('textarea');
        tempTextArea.value = text;
        tempTextArea.style.position = 'fixed';
        tempTextArea.style.top = '-9999px';
        tempTextArea.style.left = '-9999px';
        document.body.appendChild(tempTextArea);
        tempTextArea.select();
        document.execCommand('copy');
        document.body.removeChild(tempTextArea);
    }
})();