使用javascript为facebook页面创建keylistener热键;例如;

使用javascript为facebook页面创建keylistener热键;例如;,javascript,jquery,keyboard-shortcuts,greasemonkey,userscripts,Javascript,Jquery,Keyboard Shortcuts,Greasemonkey,Userscripts,我正在为学校做一个艺术项目,其中一个用户拥抱一个物体,并在Facebook上获得“喜欢”的状态。一切都很正常;但是,我需要在用户脚本中设置一个键盘侦听器 问题: 如何使热键执行unsafeWindow.like函数?如果是的话,谁能告诉我如何组合这些代码。我很确定是我的症状让它失败了 100%工作与状态代码类似 body = document.body; if(body != null) { div = document.createElement("div"); div.

我正在为学校做一个艺术项目,其中一个用户拥抱一个物体,并在Facebook上获得“喜欢”的状态。一切都很正常;但是,我需要在用户脚本中设置一个键盘侦听器

问题:
如何使热键执行unsafeWindow.like函数?如果是的话,谁能告诉我如何组合这些代码。我很确定是我的症状让它失败了

100%工作与状态代码类似

   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();
        }

    };
}
虽然这段代码合乎逻辑,但我完全无法实现它。17=ctnl键,而96=numpad 0。因此,热键==ctnl+0(numpad)。当然,该警报将被类似facebook的功能所取代

最终回答的代码

    // ==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;
        }
    }

});
/==UserScript==
//@name-Facebook-Like作者:维根
//@名称空间http://userscripts.org/scripts/show/123303
//@version 0.3
//@维根版权所有
//@description Auto Like And Confirm(mass)|您可以进行mass Like And Confirm
//@作者维根(http://userscripts.org/users/430638)
//@图标https://lh4.googleusercontent.com/-2A1Jpr4-1qM/TxPUbMq8IQI/AAAAAAAAAIU/_50N6LEgkxE/h120/FB.png
//@需要http://code.jquery.com/jquery.min.js
//@包括http://www.facebook.com/*
//@包括https://www.facebook.com/*
//@exclude htt*://developers.facebook.com/*
//
//版权所有(c)2012,维根
//自动喜欢/不喜欢,以及另一个功能。
//==/UserScript==
//==配置文件==
unsafeWindow.otomatilike=otomatilike;
类函数()
{   
输入=document.getElementsByTagName(“输入”);
对于(i=0;i=0)
输入[i]。单击();
}
}
$(窗口)。加载(函数(){
var isCtrl=false;
document.onkeyup=函数(e){
如果(e.which==17)isCtrl=false;
}
document.onkeydown=函数(e){
如果(e.which==17)isCtrl=true;
如果(例如which==96&&isCtrl==true){
类似于
返回false;
}
}
});
谢谢你的帮助 如果您想了解有关该项目的更多信息,请点击此处:
我创建了一个HTML页面,其中包含一个函数
otomatilike()

<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>

现在启用GM后,您可以单击按钮或按ctrl 0调用函数
otmatilike()
,该函数已被脚本中的函数替换,现在将发出警报“从greasemonkey触发的警报”。

tyty我可以用“like status code”循环替换警报吗?当然可以。如果它对你有帮助,请随意标记为正确答案:)我完全会的!我并不是真的喜欢被喜欢。。。你的剧本太棒了!我爱你!但是你能检查一下更新后的代码,看看我的语义哪里是完全错误的吗?不幸的是,我现在无法访问facebook。我回家后会检查你的代码是否有问题。非常感谢你的关心RASG!这里有一个链接到脚本,如果需要的话!我要在这上面转一下头。。但我真的迷路了。再次感谢您的关心
<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>
// ==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;
        }
    }

});