首页 / 话题 / 前端 / 文章详情

JS禁止查看网页源代码

一直在的 2022-09-03 | 阅读(1160) | 评论(0

查看源代码的几种方法:

  1. 直接按F12
  2. Ctrl+Shift+I查看
  3. 鼠标点击右键查看
  4. Ctrl+u=view-source:+url

以上的几种方法都可以查看到网站的源代码,我们可以通过使用JavaScript来屏蔽掉这三种状态从而实现禁止查看源代码效果。

 <span class="hljs-tag"><<span class="hljs-name">script</span>></span><span class="javascript">
        <span class="hljs-built_in">window</span>.onload = <span class="hljs-function"><span class="hljs-keyword">function</span>() </span>{
        <span class="hljs-built_in">document</span>.onkeydown = <span class="hljs-function"><span class="hljs-keyword">function</span>() </span>{
            <span class="hljs-keyword">var</span> e = <span class="hljs-built_in">window</span>.event 
 <span class="hljs-built_in">arguments</span>[<span class="hljs-number">0</span>];
            <span class="hljs-comment">//屏蔽F12</span>
            <span class="hljs-keyword">if</span>(e.keyCode == <span class="hljs-number">123</span>) {
                <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
             <span class="hljs-comment">//屏蔽Ctrl+Shift+I</span>
            } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>((e.<a target="_blank" href="https://www.ymkuz.com/tag/786.html" title="View all posts in ctrl">ctrl</a>Key) && (e.shiftKey) && (e.keyCode == <span class="hljs-number">73</span>)) {
                <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
             <span class="hljs-comment">//屏蔽Shift+F10</span>
            } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>((e.shiftKey) && (e.keyCode == <span class="hljs-number">121</span>)){
                 <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
        <span class="hljs-comment">//屏蔽Ctrl+U</span>
            } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>((e.ctrlKey) && (e.keyCode == <span class="hljs-number">85</span>)){
                     <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
            }
        };
        <span class="hljs-comment">//屏蔽右键单击</span>
        <span class="hljs-built_in">document</span>.oncontextmenu = <span class="hljs-function"><span class="hljs-keyword">function</span>() </span>{
    alert(<span class="hljs-string">"右键被禁止,复制内容请按CTRL+C!"</span>);
        <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
        }
    }
</span><span class="hljs-tag"></<span class="hljs-name">script</span>></span>
经过JS压缩后的代码如下:
<span class="hljs-tag"><<span class="hljs-name">script</span>></span><span class="javascript"><span class="hljs-built_in">window</span>.onload=<span class="hljs-function"><span class="hljs-keyword">function</span>()</span>{<span class="hljs-built_in">document</span>.onkeydown=<span class="hljs-function"><span class="hljs-keyword">function</span>()</span>{<span class="hljs-keyword">var</span> e=<span class="hljs-built_in">window</span>.event
<span class="hljs-built_in">arguments</span>[<span class="hljs-number">0</span>];<span class="hljs-keyword">if</span>(e.keyCode==<span class="hljs-number">123</span>){<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>}<span class="hljs-keyword">else</span>{<span class="hljs-keyword">if</span>((e.ctrlKey)&&(e.shiftKey)&&(e.keyCode==<span class="hljs-number">73</span>)){<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>}<span class="hljs-keyword">else</span>{<span class="hljs-keyword">if</span>((e.shiftKey)&&(e.keyCode==<span class="hljs-number">121</span>)){<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>}<span class="hljs-keyword">else</span>{<span class="hljs-keyword">if</span>((e.ctrlKey)&&(e.keyCode==<span class="hljs-number">85</span>)){<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>}}}}};<span class="hljs-built_in">document</span>.oncontextmenu=<span class="hljs-function"><span class="hljs-keyword">function</span>()</span>{alert(<span class="hljs-string">"右键被禁止,复制内容请按CTRL+C!"</span>);<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>}};</span><span class="hljs-tag"></<span class="hljs-name">script</span>></span> 

真正能实现源代码的屏蔽,单纯的JS是不可能完全屏蔽的!这些脚本只能简单防止小白,对计算机老鸟、大神是无法作用的。并且现在很多浏览器自带有查看网页源代码的功能。

其他相关

一、屏蔽F12 审查元素

<span class="hljs-built_in">document</span>.onkeydown = <span class="hljs-function"><span class="hljs-keyword">function</span>()</span>{
    <span class="hljs-keyword">if</span>(<span class="hljs-built_in">window</span>.event && <span class="hljs-built_in">window</span>.event.keyCode == <span class="hljs-number">123</span>) {
        alert(<span class="hljs-string">"F12被禁用"</span>);
        event.keyCode=<span class="hljs-number">0</span>;
        event.returnValue=<span class="hljs-literal">false</span>;
    }
    <span class="hljs-keyword">if</span>(<span class="hljs-built_in">window</span>.event && <span class="hljs-built_in">window</span>.event.keyCode == <span class="hljs-number">13</span>) {
        <span class="hljs-built_in">window</span>.event.keyCode = <span class="hljs-number">505</span>;
    }
    <span class="hljs-keyword">if</span>(<span class="hljs-built_in">window</span>.event && <span class="hljs-built_in">window</span>.event.keyCode == <span class="hljs-number">8</span>) {
        alert(str+<span class="hljs-string">"n请使用Del键进行字符的删除操作!"</span>);
        <span class="hljs-built_in">window</span>.event.returnValue=<span class="hljs-literal">false</span>;
    }
} 

除了屏蔽这个,我们还有其他有趣的设置:

二、屏蔽右键菜单

<span class="hljs-built_in">document</span>.oncontextmenu = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">event</span>)</span>{
<span class="hljs-keyword">if</span>(<span class="hljs-built_in">window</span>.event){
event = <span class="hljs-built_in">window</span>.event;
}<span class="hljs-keyword">try</span>{
<span class="hljs-keyword">var</span> the = event.srcElement;
<span class="hljs-keyword">if</span> (!((the.tagName == <span class="hljs-string">"INPUT"</span> && the.type.toLowerCase() == <span class="hljs-string">"text"</span>) 
 the.tagName == <span class="hljs-string">"TEXTAREA"</span>)){
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}
<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
}<span class="hljs-keyword">catch</span> (e){
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}
} 
三、屏蔽粘贴
<span class="hljs-built_in">document</span>.onpaste = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">event</span>)</span>{
<span class="hljs-keyword">if</span>(<span class="hljs-built_in">window</span>.event){
event = <span class="hljs-built_in">window</span>.event;
}<span class="hljs-keyword">try</span>{
<span class="hljs-keyword">var</span> the = event.srcElement;
<span class="hljs-keyword">if</span> (!((the.tagName == <span class="hljs-string">"INPUT"</span> && the.type.toLowerCase() == <span class="hljs-string">"text"</span>) 
 the.tagName == <span class="hljs-string">"TEXTAREA"</span>)){
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}
<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
}<span class="hljs-keyword">catch</span> (e){
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}
} 
四、屏蔽复制
<span class="hljs-built_in">document</span>.oncopy = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">event</span>)</span>{
<span class="hljs-keyword">if</span>(<span class="hljs-built_in">window</span>.event){
event = <span class="hljs-built_in">window</span>.event;
}<span class="hljs-keyword">try</span>{
<span class="hljs-keyword">var</span> the = event.srcElement;
<span class="hljs-keyword">if</span>(!((the.tagName == <span class="hljs-string">"INPUT"</span> && the.type.toLowerCase() == <span class="hljs-string">"text"</span>) 
 the.tagName == <span class="hljs-string">"TEXTAREA"</span>)){
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}
<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
}<span class="hljs-keyword">catch</span> (e){
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}
} 
五、屏蔽剪切
<span class="hljs-built_in">document</span>.oncut = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">event</span>)</span>{
<span class="hljs-keyword">if</span>(<span class="hljs-built_in">window</span>.event){
event = <span class="hljs-built_in">window</span>.event;
}<span class="hljs-keyword">try</span>{
<span class="hljs-keyword">var</span> the = event.srcElement;
<span class="hljs-keyword">if</span>(!((the.tagName == <span class="hljs-string">"INPUT"</span> && the.type.toLowerCase() == <span class="hljs-string">"text"</span>) 
 the.tagName == <span class="hljs-string">"TEXTAREA"</span>)){
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}
<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
}<span class="hljs-keyword">catch</span> (e){
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}
} 

这种很适合小说网站,毕竟版权珍贵,被别人随意copy走内容就不好了

六、屏蔽选中

<span class="hljs-built_in">document</span>.onselectstart = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">event</span>)</span>{
<span class="hljs-keyword">if</span>(<span class="hljs-built_in">window</span>.event){
event = <span class="hljs-built_in">window</span>.event;
}<span class="hljs-keyword">try</span>{
<span class="hljs-keyword">var</span> the = event.srcElement;
<span class="hljs-keyword">if</span> (!((the.tagName == <span class="hljs-string">"INPUT"</span> && the.type.toLowerCase() == <span class="hljs-string">"text"</span>) 
 the.tagName == <span class="hljs-string">"TEXTAREA"</span>)){
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}
<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
} <span class="hljs-keyword">catch</span> (e) {
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}
} 

相关阅读
评论(0)
请登录
请登录后发表评论