Skip to content Skip to sidebar Skip to footer

Using Javascript To Create A Keylistener Hotkey For Facebook Page "like"

I'm doing an art project for school where a user hugs an object and gets a 'liked' status on Facebook. Everything pretty much works; however, I need to set up a keyboard listener

Solution 1:

Here: I created an HTML page with a function OtomatisLike() in it.

<htmllang="en"><head><title>test - keylistener</title><scriptsrc="http://code.jquery.com/jquery.min.js"type="text/javascript"></script><scripttype="text/javascript">functionOtomatisLike() { alert('alert fired from html page') }
</script></head><body><buttonid="btnstart">click to start</button><scripttype="text/javascript">
        $('#btnstart').click(function() { OtomatisLike() })
    </script></body></html>

With GM disabled, you will see the alert say "alert fired from html page" when you click the button.

Then i created a greasemonkey script to change that function

// ==UserScript==// @name        TESTE 2// @require     http://code.jquery.com/jquery.min.js// @include     file://*// ==/UserScript==functionOtomatisLike() { alert('alert fired from greasemonkey') }
unsafeWindow.OtomatisLike = OtomatisLike;

$(window).load(function(){

    var isCtrl = false;
    document.onkeyup=function(e) {
        if(e.which == 17) isCtrl=false;
    }
    document.onkeydown=function(e){
        if(e.which == 17) isCtrl=true;
        if(e.which == 96 && isCtrl == true) {
            OtomatisLike()
            //alert('Keyboard shortcuts are cool!');returnfalse;
        }
    }

});

Now with GM enabled, you can click the button or press ctrl0 to call the function OtomatisLike(), which was replaced by the one in the script and now will alert "alert fired from greasemonkey".

Post a Comment for "Using Javascript To Create A Keylistener Hotkey For Facebook Page "like""