使用javascript为facebook页面“喜欢”创建keylistener热键

时间:2012-04-18 18:19:16

标签: javascript jquery keyboard-shortcuts greasemonkey userscripts

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

问题:
 如何使热键执行unsafeWindow.Otomatislike函数?如果是这样,任何人都可以告诉我如何组合这些代码。我很确定我的语法是失败的原因。

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

    };
}

此代码在左下角创建一个小按钮,以便在其上显示redbull页面。即。 http://vvcap.net/db/CTHpxz8qeuuZX1yy5tEd.htp

问题我想为此添加一个键盘监听器,而不是我设计的辅助div“按钮”。 http://vvcap.net/db/M0XbpT5Sw8BmOtfAHJ4m.htp

我发现了一些看起来很重要的示例代码;但是,我有很多想法如何实现它: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;
    }
}

虽然这段代码具有逻辑意义,但我完全无法实现它。 17 = ctnl键而96 == numpad 0.因此热键== ctnl + 0(小键盘)。当然,警报将被替换为类似功能的脸书

最终法典解答

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

});

感谢您的帮助 如果您想了解有关项目的更多信息pdf presetnation

1 个答案:

答案 0 :(得分:2)

下面:
我在其中创建了一个包含函数OtomatisLike()的HTML页面。

<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后,当您点击按钮时,您会看到警告说“从html页面发出警报”。

然后我创建了一个改变该功能的greasemonkey脚本

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

});

现在启用GM,您可以单击按钮或按 ctrl 0 来调用函数OtomatisLike(),该函数已替换为脚本中的函数现在将警告“从油脂中发出警报”。