1

I'm trying to make a script for Greasemonkey to automatically pin when a Pinterest window is opened.

CODE:

body = document.body;

if (body != null) {
    var prepare = document.getElementsByTagName("button");
    var buttons = "";

    for (var i = 0; i < prepare.length; i++){
        if (prepare[i].getAttribute("data-text-pin-it")!=null) {
            buttons = prepare[i];
            break;
        }
    }
    buttons.click();
}

It gets executed, if I set a console.log at the end of the code is printed, but the button is not clicked!

If I execute the same instructions by Firebug console I get the desired effect.

4

1 回答 1

0

毫无疑问,Pinterest 按钮是由 AJAX 添加的——在您的脚本运行很久之后。

使用 AJAX 补偿技术等待它。这是一个完整的脚本,它使用 jQuery 的强大功能和waitForKeyElements() 实用程序来单击该按钮:

// ==UserScript==
// @name     YOUR_SCRIPT_NAME
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

waitForKeyElements ("button[data-text-pin-it]", clickPinterestBtn);

function clickPinterestBtn (jNode) {
    jNode[0].click ();
}
于 2013-02-08T14:20:17.133 回答