Securing Blog Posts in Blogger
All the following codes go into the Layout section in your Blogger admin screen.
Create a new HTML/JavaScript Gadget and simply copy/paste the code blocks you want to use.
How to Disable Text Selection
Use this code block to disable text selection. When selection itself cannot happen, copying won’t be possible. Trying hitting Ctrl + C or Ctrl + V while browsing the blog and you’ll see the difference.
//disable Text Selection and Copying<script src='demo-to-prevent-copy-paste-on-blogger_files/googleapis.js'>
</script>
<script type='text/javascript'>
if (typeof document.onselectstart!="undefined" ) {
document.onselectstart=new Function ("return false" );
}
else {
document.onmousedown=new Function ("return false" );
document.onmouseup=new Function ("return true" );
}
</script>
How to Disable Right Clicking
Use this code block to disable right clicking option.
//disable right click menu
<script language=javascript>
function blockOne() {
if (document.all) {
return false;
}
}
function blockTwo(e) {
if (document.layers||(document.getElementById&&!document.all))
{
if (e.which==2||e.which==3) {
return false;
}
}
}
if (document.layers){
document.captureEvents(Event.mousedown);
document.onmousedown=blockTwo;
}
else {
document.onmouseup=blockTwo;
document.oncontextmenu=blockOne;
}
document.oncontextmenu=new Function("return false");
</script>
How to Disable Viewing Page Source (Ctrl + U)
Use this code block in your Blogger blog gadget to disable the keyboard combination Ctrl + U, which is the keyboard shortcut to view page source. Although, this is an extra level security, there are ways to get around this which we’ll see later in this guide.
// disable viewing page source
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
document.onkeydown = function(e) {
if (e.ctrlKey &&
(e.keyCode === 67 ||
e.keyCode === 86 ||
e.keyCode === 85 ||
e.keyCode === 117)) {
alert('Content is protected\nYou cannot view the page source.');
return false;
} else {
return true;
}
};
$(document).keypress("u",function(e) {
if(e.ctrlKey)
{
return false;
}
else
{
return true;
}
});
</script>
Finally, use this code block if you want to disable the F12 key which lets the user to access Developer Tools in the Chrome Web browser. Not just that, when you use this code block, it also disables the Ctrl + Shift + I keyboard combination which is the same as pressing the F12 key to open Developer Tools.
//disable F12 Key and Ctrl + shift + I combination
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).keydown(function (event) {
if (event.keyCode == 123) { // Prevent F12
alert('Content is protected\nYou cannot view the Dev Tools.');
return false;
} else if (event.ctrlKey && event.shiftKey && event.keyCode == 73) { // Prevent Ctrl+Shift+I
alert('Content is protected\nYou cannot view the Dev Tools.');
return false;
}
});
</script>
All in One Code Block
Here is the ultimate code block that combines all the above four codes. This does all the following:
- Disables right click context menu
- Disables text selection, copying (Ctrl + C, V)
- Disables viewing page source (Ctrl + U)
- Disables viewing Developer Tools (F 12 and Ctrl + Shift + I key combinations)
//disable Text Selection and Copying<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src='demo-to-prevent-copy-paste-on-blogger_files/googleapis.js'>
</script>
<script type='text/javascript'>
if (typeof document.onselectstart!="undefined" ) {
document.onselectstart=new Function ("return false" );
}
else {
document.onmousedown=new Function ("return false" );
document.onmouseup=new Function ("return true" );
}
</script>
//==========================================================
//disable right click menu
<script>
function blockOne() {
if (document.all) {
return false;
}
}
function blockTwo(e) {
if (document.layers||(document.getElementById&&!document.all))
{
if (e.which==2||e.which==3) {
return false;
}
}
}
if (document.layers){
document.captureEvents(Event.mousedown);
document.onmousedown=blockTwo;
}
else {
document.onmouseup=blockTwo;
document.oncontextmenu=blockOne;
}
document.oncontextmenu=new Function("return false");
</script>
//==============================================================
// disable viewing page source
<script>
document.onkeydown = function(e) {
if (e.ctrlKey &&
(e.keyCode === 67 ||
e.keyCode === 86 ||
e.keyCode === 85 ||
e.keyCode === 117)) {
alert('Content is protected\nYou cannot view the page source.');
return false;
} else {
return true;
}
};
$(document).keypress("u",function(e) {
if(e.ctrlKey)
{
return false;
}
else
{
return true;
}
});
//=============================================================
//disable F12 Key and Ctrl + shift + I combination
$(document).keydown(function (event) {
if (event.keyCode == 123) { // Prevent F12
alert('Content is protected\nYou cannot view the Dev Tools.');
return false;
} else if (event.ctrlKey && event.shiftKey && event.keyCode == 73) { // Prevent Ctrl+Shift+I
alert('Content is protected\nYou cannot view the Dev Tools.');
return false;
}
});
</script>
No comments:
Post a Comment