Using javascript to create a keylistener hotkey fo

2019-07-28 03:17发布

问题:

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 in a userscript.

the Problem:
How do I make a hotkey execute the unsafeWindow.Otomatislike function? If so could anyone show me how to combine these codes. I'm pretty certain my symantics are what make it fail.

100% working like status code

   body = document.body;
if(body != null) {
    div = document.createElement("div");
    div.setAttribute('id','like2');
    div.style.position = "fixed";
    div.style.display = "block";
    div.style.width = "125px"; 
    div.style.opacity= 0.90;
    div.style.bottom = "+42px";
    div.style.left = "+6px";
    div.style.backgroundColor = "#eceff5";
    div.style.border = "1px solid #94a3c4";
    div.style.padding = "2px";
    div.innerHTML = "<img src='http://g1203.hizliresim.com/v/n/3pnck.png' width='16' height='14' align='absmiddle' />&nbsp;&nbsp;<a onclick='OtomatisLike()'>Like</a>"

    body.appendChild(div);

    unsafeWindow.OtomatisLike = function() {

        input = document.getElementsByTagName("input");
        for(i = 0; i < input.length; i++) {
            myID = input[i].getAttribute("data-profileid");
            if(myID != null && myID.indexOf("14226545351") >= 0)
                input[i].click();
        }

    };
}

this code creates a small button in the bottom left corner to like the redbull page when on it. ie. http://vvcap.net/db/CTHpxz8qeuuZX1yy5tEd.htp

problem I would like to add a keyboard listener to this instead of the secondary div "button" I've styled. http://vvcap.net/db/M0XbpT5Sw8BmOtfAHJ4m.htp

I've found some sample code that look revelvent; however, I'm having a lot of troble figuring out how to implement it:hotkey citation

    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) {
         alert('Keyboard shortcuts are cool!');
         return false;
    }
}

While this code makes logical sense I'm totally unable to implement it. 17 = ctnl key while 96 == numpad 0. therefore the hotkey == ctnl + 0 (numpad). of course the alert would be replaced with the facebook like function

FINAL CODE ANSWERED

    // ==UserScript==
// @name            Facebook Like By Virgan
// @namespace               http://userscripts.org/scripts/show/123303
// @version         0.3
// @copyright               Virgan
// @description             Auto Like And Confirm (mass) | You can mass like and confirm
// @author          Virgan (http://userscripts.org/users/430638)
// @icon            https://lh4.googleusercontent.com/-2A1Jpr4-1qM/TxPUbMq8IQI/AAAAAAAAAIU/_50N6LEgkxE/h120/FB.png
// @require         http://code.jquery.com/jquery.min.js
// @include         http://www.facebook.com/*
// @include         https://www.facebook.com/*
// @exclude         htt*://developers.facebook.com/*
//
// Copyright (c) 2012, Virgan
// Auto Like/Unlike, And Another Function.


// ==/UserScript==

// ==Profile==
unsafeWindow.OtomatisLike = OtomatisLike;
function OtomatisLike() 
    {   
        input = document.getElementsByTagName("input");
        for(i = 0; i < input.length; i++) 
            {
                myID = input[i].getAttribute("data-profileid");
                if(myID != null && myID.indexOf("14226545351") >= 0)
                input[i].click();
            }

    }


$(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()

            return false;
        }
    }

});

Thank you for all your help if you want to know more about the project pdf presetnation

回答1:

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

<html lang="en">
<head>
<title>test - keylistener</title>
<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    function OtomatisLike() { alert('alert fired from html page') }
</script>
</head>
<body>
    <button id="btnstart">click to start</button>
    <script type="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==

function OtomatisLike() { 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!');
            return false;
        }
    }

});

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