I have a webpage (ruby on rails) with some text that I would like to protect the content in order to block the user to copy it (ctrl+c).
Of course that I have some "protection" but it's not enough
$(function(){
$('a.fancybox').fancybox();
$('.prevent_select').disableSelection();
});
$.support.selectstart = "onselectstart" in document.createElement("div");
$.fn.disableSelection = function() {
return this.bind( ( $.support.selectstart ? "selectstart" : "mousedown" ) +
".ui-disableSelection", function( event ) {
event.preventDefault();
});
};
If the user uses CTRL+A all the text is selected. I have in my mind two ways in order to protect it. But I would like to share with you in order to have some feedback.
Is it possible to use an swf (Macromedia flash) in order to show the text? (some GEM?)
Is it possible to "delete" always the user buffer when the webpage detects that the user is using ctrl+c?
Do you have other ideas?
I really appreciate your feedback and help.
You can draw text using a canvas. Example: http://jsfiddle.net/ExWei/hZmM2/
You can place transparent image on text zone. (using a CSS)
Script for blocking CTRL - U, CTRL - C, CTRL - A:
document.ondragstart = test;
document.onselectstart = test;
document.oncontextmenu = test;
function test()
{
return false;
}
document.oncontextmenu;
function catchControlKeys(event)
{
var code=event.keyCode ? event.keyCode : event.which ? event.which : null;
if (event.ctrlKey)
{
// Ctrl+U
if (code == 117) return false;
if (code == 85) return false;
// Ctrl+C
if (code == 99) return false;
if (code == 67) return false;
// Ctrl+A
if (code == 97) return false;
if (code == 65) return false;
}
}
For using this script change
<body>
to
<body onkeypress="return catchControlKeys(event)">
Related
How can I hide JavaScript's behavior from the user when I prohibit printing?
I was able to prevent the print button from being pressed on that web page by writing the following inside the head tag of the html file:
<script type="text/javascript">
document.onkeydown = keys;
function keys()
{
switch (event.keyCode)
{
case 82: // Ctrl + R
if( event.ctrlKey )
{
event.keyCode = 0;
return false;
}
case 80: // Ctrl + P
if( event.ctrlKey )
{
event.keyCode = 0;
alert('Hello.'); //If this code was none, Ctrl and P can be executed,
return false;
}
break;
}
}
</script>
This Ctrl+P is strange.
If I do not intervene something before
return false;
such as
alert();
the print screen will appear.
However like
case 82: // Ctrl + R
if( event.ctrlKey )
{
event.keyCode = 0;
return false; //without alert codes,this code can be executed.
}
The script can be executed without alert codes.It is strange thing for me.
If an alert message is seen, the user will easily see that it is running in JavaScript or something, so if possible, I want something invisible to the user side;
What should I do?
Do I have to show alert message to user like:
alert('Please do not print.');
I want some help.Thanks.
The simplest and most standard way to implement this is with Event#preventDefault:
window.addEventListener('keydown', (e) => {
if (e.key === 'p' && e.ctrlKey) {
e.preventDefault()
}
})
I am working on a project in which I need to intercept a paste action that is being performed on a div (must be a div, can't be a text box). Right now I am binding the event after the div has focus (you've clicked on the div):
$('#result').unbind().click(function () {
$(this).focus();
$('#result').unbind().on('paste', function () {
console.log('paste behaviour detected!');
});
}); //NOTE: I have also tried. result.bind, result.unbind.bind, onpast="function()"
//(in the HTML), and a couple of other things.
I have also tried changing around the flow of the class (no change).
One more thing. I am using chrome/opera to develop. When I test this on firefox it works just fine. Is there anything I can try to fix this or did I stumble upon a bug?
NOTE: I am leaving out info about the project for simplicity, but if you need more context I can provide it.
Edit: I am pasting in to a div so there is no rightclick>paste button. This is solely with ctrl+v.
You can detect the combination of ctrl/cmd + v keys as well:
$(document).ready(function() {
var ctrlDown = false,
ctrlKey = 17,
cmdKey = 91,
vKey = 86;
$(document).keydown(function(e) {
if (e.keyCode == ctrlKey || e.keyCode == cmdKey) {
ctrlDown = true;
}
}).keyup(function(e) {
if (e.keyCode == ctrlKey || e.keyCode == cmdKey) {
ctrlDown = false;
}
});
$(document).keydown(function(e) {
if (ctrlDown && e.keyCode == vKey) {
alert('PASTE DETECTED');
}
});
});
});
https://jsfiddle.net/ufskbo0a/1/
You can use the clipboardData api in most browsers to get the data:
window.clipboardData.getData('Text')
http://caniuse.com/#search=clipboardData
I'm trying to avoid users to use the back button, it was so far so good, until I try to get the context menu, right clicking over Chrome's title bar I've seen other pages that doesn´t have the same options I do.
I used this code:
< script type="text/javascript">
function disableBackButton() {
window.history.forward(1);
}
setTimeout("disableBackButton()", 0);
window.oncontextmenu = function() { return false }
</ script>
< script type="text/javascript">
if (typeof window.event != 'undefined')
document.onkeydown = function() {
if (event.srcElement.tagName.toUpperCase() != 'INPUT')
return (event.keyCode != 8);
}
else
document.onkeypress = function(e) {
if (e.target.nodeName.toUpperCase() != 'INPUT')
return false;
}
</ script>
< script type="text/javascript">
document.onkeydown = chkEvent
var formInUse = false;
function chkEvent(e) {
var keycode;
if (window.event) keycode = window.event.keyCode; //*** for IE ***//
else if (e) keycode = e.which; //*** for Firefox ***//
if (keycode == 8) {
return false;
}
}
</ script>
< body onload="disableBackButton();" >
It seems to me that there is not much to do and that's OS territory, but worth a try if someone knows about this
Kind Regards
George
You absolutely CANNOT and MUST NOT be allowed to do anything outside your webpage sandbox. Even if you accidentally find a way in some browser to do that, do not rely on it, as it is will be viewed as huge security risk and promptly patched or reduced to some hidden-flag activated kludge by developers. Just drop the idea as it stands completely against what browser's sandboxed environment tries to accomplish, so it won't work in any public project.
Basically sometimes I need to show a form that is pre-populated with a record. Depending on the users privileges, he may or may not be able to edit the data.
The problem I'm encountering is that sometimes a user will try to edit a textbox that's been disabled by clicking on it and hitting the "backspace" button to edit the text. This causes the browser to go back one page... Annoying.
If it's asp .net you can simply do it like this:
<script language=javascript>
function cancelBack()
{
if ((event.keyCode == 8 ||
(event.keyCode == 37 && event.altKey) ||
(event.keyCode == 39 && event.altKey))
&&
(event.srcElement.form == null || event.srcElement.isTextEdit == false)
)
{
event.cancelBubble = true;
event.returnValue = false;
}
}
</script>
<body onkeydown=cancelBack()>
You need to catch the keyboard event in javascript and stop it from executing. What server-side code you are using (ASP.NET) doesn't make a difference.
window.onkeydown = function(event) {
if(event.keyCode == 8)
return false;
}
Just tested in Chrome and it seems to work
Place this under in the document ready function if you have one
window.onkeydown = function (event)
{
if (event.keyCode == 8) {
return false;
}
}
I have 3 fields on a page and when the user has the cursor focused on two of them I want a certain button on the page to be the one thats pressed when they hit enter. I am using
<script language="JavaScript">
function stopRKey(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if ((evt.keyCode == 13) && (node.type == "text")) { return false; }
}
document.onkeypress = stopRKey;
</script>
To halt the default behaviour I just cant see how to modify it to let certain keypresses through. I did look at a tutorial http://asquare.net/javascript/tests/KeyCode.html but it wasnt obvious what is happening.
Anybody know how to do this or a tutorial that goes over this?
Use jQuery (this should work):
$('#input_id').keypress(function(event) {
if (event.keyCode == '13') {
// press correct button
}
});
Make one for each of your fields and point toward the correct button.
If you want to skip jQuery, its still really simple:
<script language="javascript">
document.getElementById('myInput1').addEventListener('keypress', handlePress);
document.getElementById('myInput2').addEventListener('keypress', handlePress);
function handlePress(evt)
{
if( evt.keyCode == 13 )
{
// execute your function here
console.log('Enter pressed while in desired text input')
}
}
</script>