How to make chrome extension hotkey work for ALL tabs? - javascript

I'm using this code in the contents script to implement a hotkey in my Chrome Extension:
window.addEventListener("keydown", function(event) {
var modifier = event.ctrlKey || event.metaKey;
if (modifier && event.shiftKey && event.keyCode == 80) {
// stuff happens here
}
});
Is there any way to make this hotkey work on Chrome tabs like the New Tab, Extensions, Settings etc. without using NPAPI?

You can use the new chrome.experimental.keybinding api and make your hotkey gobal http://code.google.com/chrome/extensions/trunk/experimental.keybinding.html

Related

window.clipboardData.setData('text','') is not working in chrome

i want to prevent print screen in my website.
i used
but its not working in chrome and mozillafirefox
$(document).keyup(function(e){
if(!e) e = window.event;
var keyCode = e.which || e.keyCode;
if (keyCode == 44) {
if(window.clipboardData) {
window.clipboardData.setData('text', '');
}
}
});
Thank you
There are keystrokes you can't capture on many operating system/browser combinations. PrintScreen is very, very likely to be one of them.
You just can't do this. If your site can be displayed in a browser, screenshots can be taken of it (also copies of it can be stored locally and manipulated, the content can be scraped via non-browser technologies, etc., etc.).

Catch save (Ctrl/Cmd+S) keyboard shortcut in the browser?

Is there a cross-browser way or cross browser solution to capture the "Save" keyboard shortcut in the browser? For example, in GMail if you use CMD + S it will save the email and not prompt the browser to download the webpage (default behavior). I would rather use a cross browser solution than add event listeners and write it from scratch.
One cross browser solution is to check out keycode.js. Less hassle to worry about looking for browser caveats and such.
I know it's old, but it still works. You can check out its demo too!
Looking at the demo, and the header comments in the .js, something like this?
document.onkeydown = function(e) {
e = e || window.event
var k = KeyCode.translate_event(e);
if( k.ctrl && !k.alt && !k.shift && k.code == 83 ) //83 is the code for s
save(); //Magic function
if(e.preventDefault) {
e.preventDefault();
}
return false;
};

Transforming Enter key pressed event into tab key pressed event in custom textbox (it has to be multibrowser)

We have a dll of custom web controls based on the System.Web.UI ones. I've been asked to make it so when the user hits enter, the tab event would be fired instead. I've done this:
Me.Attributes.Remove("onkeydown")
Me.Attributes.Add("onkeydown", "if(event.keyCode == 13) {event.keyCode = 9; return event.keyCode;}")
If Me.TextMode = Web.UI.WebControls.TextBoxMode.MultiLine Then
Me.Attributes.Remove("onkeydown")
End If
But it only works on IE. When i tested it on FF and Chrome, the enter event generates postback. I've tried other solutions:
<script type="text/javascript">
function catchEnter(e){
var theKey=0;
e=(window.event)?event:e;
theKey=(e.keyCode)?e.keyCode:e.charCode;
if(theKey=="13"){ // 13 is the key code for ENTER
// here is where your code will go
//return false; // return false if you want to...
//...halt submission of form (call this function onsubmit)
}
}
</script>
function checkKey(e) {
var event = window.event ? window.event : e;
if (true) {
alert(event.keyCode)
}
}
And also:
function displayunicode(e){
var unicode=e.keyCode? e.keyCode : e.charCode
But i have to use Attributes.Add to insert the javascript. I can write down a whole javascript function using Attributes.Add? When i try to use these functions, i ignore the 'script' tags and only put the javascript code. Am i doing something wrong here?
Which one of these codes would work? (IE, FF and Chrome)

How to Disable Function keys using Javascript?

I'm having a page where i need to disable the Function keys mainly F12(Developertools).
I'm showing some sensitive data in the page so at any case i cannot make the users see the html and take the hidden fields.
I checked some javascript which is working for almost all the keys except the Function keys like f1, f12 etc.
Is there anyway that i can disable these buttons in the browser?
document.onkeyup = KeyCheck;
function KeyCheck() {
var KeyID = event.keyCode;
alert(KeyID);
switch (KeyID) {
case 123: //F12 KEY CODE
alert('hello');
return false;
break;
}
}
This is the code which im using for overriding the key. When I searched, the keycode of F12 key is 123 and im using the same code for overriding it. But unfortunately its not even hitting the "CASE" and the message box is not appearing when pressing F12, F1 etc buttons.
Please Help me in this.
There is no reliable way to prevent users from tampering with your javascript data, when you've sent it. Always use server-side checks to verify the returned data.
People can still use the browser's menu to enable the dev console. Or through right-click --> "Inspect Element", or by using hotkeys to open different sections of the console, then tabbing to another page in the console, or by using one of the hotkeys I've failed to mention.
Or, they can simply disable javascript. (Or edit the javascript to disable the block)
Now, you can be a little more thorough in disabling whatever button's functionality, by adding a:
event.preventDefault() in your event listener, but still, it's unreliable.
document.onkeydown = KeyCheck;
It worked.
No, you can't diasble view source/developer tools or any other application level functionality of the browser via JavaScript on the page.
There are plenty ways to see source of the web page. You are up to very hard task to restrict all external parties to access/store/view your HTML. Here is partial list of other things you'll have to disable:
proxies, including HTTP debuggers/proxies like Fiddler or ones that are built in to browsers.
direct GET requests from console tools like curl.
all sorts of web crawlers, including search engines like Google.
Use HTTPS and do not send sensetive information unless strictly required is the much easier way of protecting it than trying to limit what users can do with their machines.
Try this out:
<script language="JavaScript">
document.onkeypress = function (event) {
event = (event || window.event);
if (event.keyCode == 123) {
//alert('No F-12');
return false;
}
}
document.onmousedown = function (event) {
event = (event || window.event);
if (event.keyCode == 123) {
//alert('No F-keys');
return false;
}
}
document.onkeydown = function (event) {
event = (event || window.event);
if (event.keyCode == 123) {
//alert('No F-keys');
return false;
}
}
</script>
This Code works Perfectly for me to Disable Right Click and Disable F12
<script language=JavaScript>
var message="You Have No Permission";
function clickIE4(){
if (event.button==2){
alert(message);
return false;
}
}
function clickNS4(e){
if (document.layers||document.getElementById&&!document.all){
if (e.which==2||e.which==3){
alert(message);
return false;
}
}
}
if (document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById){
document.onmousedown=clickIE4;
}
document.oncontextmenu=new Function("alert(message);return false")
When user press F12 key, browsers developer tool bar will open in the below portion of the browser.
By using the developer tool bar user can see the design, javascript code and corresponding css applied to the controls in the page. To prevent the user to do that we will hide the developer tool bar.
Here is the code

Disable backspace with DOM Level 3 Listeners

I am making a page in which a user must be able to type. The default function of backspace is to go back a page, however I need to prevent this and assign my own function to it.
The problem is actually preventing backspace. I can capture it however I can not prevent it.
I am using Level 3 event listeners. event.preventDefault() did not work for me and neither did return false.
I have tried this also:
function onunload(e) {
if (e.keyCode == 37) return false;
return true;
}
and
<body onunload="return false;">
However the first basically does
return confirm("false");
and the second does nothing?
Preventing backspace key altogether might not be a good idea, assume you make a typo and want to fix it in a text box! So you need to disable backspace only if someone is not typing in a text input!
This might work in all major browsers:
document.onkeydown = function (e) {
e = e || window.event;
if (e.keyCode == 8) { // Disable Backspace
var el = document.activeElement;
if (!((el.nodeName == "INPUT" && (el.type == "text" || el.type == "password")) || el.nodeName == "TEXTAREA")) {
e.preventDefault();
e.stopPropagation();
return false;
}
}
};
DOM3 event model spec comes with backward compatibility so I don't think this might be an issue, however this is the DOM3 modified version of the above code:
function KeyDownHandler(e) {
if (e.keyCode == 8) { // Disable Backspace
var el = document.activeElement;
if (!((el.nodeName == "INPUT" && (el.type == "text" || el.type == "password")) || el.nodeName == "TEXTAREA")) {
e.preventDefault();
e.stopPropagation();
}
}
};
if (document.attachEvent) document.attachEvent("onkeydown", KeyDownHandler);
else document.addEventListener("keydown", KeyDownHandler);
A DOM3 event registration like the one above might be a great pain, John Resig has a good implementation on this, you could however use a standard cross browser library like JQuery which works fine in all major browsers!
AFAIR, one may prevent default browser shortcut actions by returning false in keypress in Firefox only (and last version I tested, was 3.x).
beforeunload and unload are specific events and string should be returned in their handlers. For security reasons, browser may only display a confirmation dialog with message provided (that string). Moreover, Firefox since version 4 even ignores that string and always displays standard message.
There's no way to silently prevent user from leaving page.
BTW backspace don't work as history.back() in Ubuntu.
Use a keydown handler on the document level:
document.onkeydown = function(e){
if (e.keyCode===8) {
return false;
}
};

Categories

Resources