JS + CSS защита контента на странице от копирования

Последнее обновление: 29 июня, 2021

Без рубрики

content-no-copy.php:

<script ENGINE="text/javascript">


    
    function enableContextMenu(toggle = true) {

        if (toggle) {
            document.oncontextmenu = () => true;
        } else {
            document.oncontextmenu = event => {
                if (event.target.tagName.match(/INPUT|TEXTAREA/i)) {
                    return true;
                }
                return false;
            };
        }
    }


    // Add access to copy content from site
    function addAccessToContent() {
        all.forEach(element => {
            element.style.userSelect = "initial";
        });
    }


    function enableBrowserToolTip(toggle = true) {


        if (toggle) {
            document.onkeydown = () => true;
        } else {
            document.onkeydown = key => {

                // Disable f12
                if (key.keyCode == 123) {
                    return false;
                }

                // disable ctrl+shift+I key
                if (key.ctrlKey && key.shiftKey && key.keyCode == 73) {
                    return false;
                }

                // disable ctrl+shift+J key
                if (key.ctrlKey && key.shiftKey && key.keyCode == 74) {
                    return false;
                }

                // disable ctrl+U key
                if (key.ctrlKey && key.keyCode == 85) {
                    return false;
                }

                // disable ctrl+U key
                if (key.altKey && key.metaKey && (key.keyCode == 'I'.charCodeAt(0) || key.keyCode == 'i'.charCodeAt(0))) {
                    return false;
                }
            };
        }

    }

    


    const all = document.querySelectorAll("*");
    // Insert button selector for button that will give u the right.
    const getRightButton = document.querySelector("[buttonSelector]");
    let currentIp = "<? echo include(__DIR__ . "/get_ip.php"); ?>";

    enableContextMenu(false);
    enableBrowserToolTip(false);
    if (currentIp == "<? echo $_SERVER['REMOTE_ADDR']; ?>") {
        addAccessToContent();
        enableContextMenu();
        enableBrowserToolTip();
    }


    // Get right to copy text listener
    getRightButton.addEventListener("click", () => {

        const ip = "<? echo $_SERVER['REMOTE_ADDR']; ?>";
        const post = new XMLHttpRequest();
        const formData = new FormData();
        const proccesing = "https://idma.ltd/data/content-no-copy/rewrite_ip.php";

        formData.append("ip", ip);
        post.open("POST", proccesing);
        post.send(formData);
        currentIp = ip;
        addAccessToContent();
        enableContextMenu();
        enableBrowserToolTip();
    });
</script>


<style>
    * {
        -webkit-user-select: none;
        -khtml-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        -o-user-select: none;
        user-select: none;
    }

    input,
    textarea {
        -webkit-user-select: initial;
        -khtml-user-select: initial;
        -moz-user-select: initial;
        -ms-user-select: initial;
        -o-user-select: initial;
        user-select: initial;
    }
</style>

get_ip.php:

<?
$file = __DIR__."/ip";
$f = fopen($file,"r");
return trim(strval(fgets($f)));

rewrite_ip.php

<? 

$file = __DIR__."/ip";
$ip = $_POST["ip"];
file_put_contents($file,$ip);