当前位置:网站首页>Method of copying web page content and automatically adding copyright information (compatible with ie, Firefox and chrome)

Method of copying web page content and automatically adding copyright information (compatible with ie, Firefox and chrome)

2022-06-23 17:48:00 It workers

The method of automatically adding copyright information to the contents of the copying network

You can find many similar codes on the Internet , Join the web body Part of it .

For example, the following code :

// Copy content and add copyright information automatically  
document.body.oncopy = function ()  
{ 
    setTimeout( 
        function () 
        { 
            var text = clipboardData.getData("text"); 
            if (text) 
            { 
                text = text + "\r\n Original text 【*****】, Reprint please keep the original link :"+location.href; 
                clipboardData.setData("text", text); 
            } 
        }, 
        100 
    ) 
} 

This code can realize , Don't forget to write in js File or write directly in the page

<script ="text/javascript"> Code </script> Include .

After adding the above code , Someone else is on any page of your website , Copy anything with a word , The copyright information will be automatically brought when pasting .

But the downside of this code is : stay IE6 Upper test passed , And in the Firefox、Opera There is no effect on the browser .

To solve this problem , I have consulted many experts , I also checked a lot of information on the Internet , It's finally settled , Here I want to say , Du Niang is really strong .

Compatible codes are posted below :

// Copy content and add copyright information automatically  
 var Sys = {}; 
    var ua = navigator.userAgent.toLowerCase(); 
    if( window.ActiveXObject ) 
    { 
        document.body.oncopy=function() 
        { 
            event.returnValue = false; 
            var t=document.selection.createRange().text; 
            var s="\r\n Original text [****]  Reprint please keep the original link :"+location.href; 
            clipboardData.setData('Text',t+'\r\n'+s); 
        } 
    } 
    else 
    { 
        function addLink() 
        { 
            var body_element = document.getElementsByTagName('body')[0]; 
            var selection; 
            selection = window.getSelection(); 
            var pagelink = "  Original text ****  Reprint please keep the original link :"+document.location.href; 
 
            var copytext = selection + pagelink; 
            var newdiv = document.createElement('div'); 
            newdiv.style.position='absolute'; 
            newdiv.style.left='-99999px'; 
            body_element.appendChild(newdiv); 
            newdiv.innerHTML = copytext; 
            selection.selectAllChildren(newdiv); 
            window.setTimeout 
            ( 
                function() 
                { 
                    body_element.removeChild(newdiv); 
                },0 
            ); 
        } 
        document.oncopy = addLink; 
    } 

This code has been tested , Compatible , There may be some deficiencies , If found , Leave a comment below , Discuss together 、 improvement .

Better say something , Pay attention to the coding , If the copyright information added to the copied content is found to be garbled , You can check for coding problems yourself .

原网站

版权声明
本文为[It workers]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/01/202201051804123417.html