javascript - 如何组合这 2 个 Greasemonkey 脚本?

标签 javascript greasemonkey

我需要将这 2 个脚本结合起来用于 GM。一个从列表中打开新页面,另一个单击“关注”按钮。

脚本 1:How to open a list of pages automatically and sequentially?

脚本 2:How do I click on this button with Greasemonkey?

我尝试自己组合它们,但未能创建一个完全重新加载页面的工作脚本,即使它们按顺序放入列表中(如果您阅读其他问题,您就会明白我的意思)。

这是我尝试过的方法,但它无法按预期工作,因为它无法正确重新加载页面并继续执行其任务:

// ==UserScript==
// @name    Follow People on INK361
// @description Follow People from our FB Page's list INK361
// @include     http://ink361.com*
// @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 major design
    change introduced in GM 1.0.
    It restores the sandbox.
*/

var urlsToLoad  = [
'http://ink361.com/#/users/30742610/photos',
'http://ink361.com/#/users/193869245/photos',
'http://ink361.com/#/users/215062853/photos',
'http://ink361.com/#/users/218295575/photos'
];

/*--- Since many of these sites load large pictures, Chrome's and 
    Firefox's injection may fire a good deal before the image(s) 
    finish loading.
    So, insure script fires after load:
*/

//--- Catch new pages loaded by WELL BEHAVED ajax.
window.addEventListener ("hashchange", FireTimerA,  false);

function FiretimerA () {
    waitForKeyElements ("a.simplebutton:contains('follow')", FireTimer());
}


function FireTimer (jNode) {

    if ( ! /^\s*follow\s*$/i.test () ) {   
        return false;
    }

    var clickEvent  = document.createEvent ('MouseEvents');
    clickEvent.initEvent ('click', true, true);
    jNode[0].dispatchEvent (clickEvent);
    GotoNextURL();
}

function GotoNextURL () {
    var numUrls     = urlsToLoad.length;
    var urlIdx      = urlsToLoad.indexOf (location.href);
    urlIdx++;
    if (urlIdx >= numUrls)
        urlIdx = 0;

    location.href   = urlsToLoad[urlIdx];
}

最佳答案

合并这些脚本应该很简单:只需标准化元数据 block 并将一个脚本的 javascript 粘贴到另一个脚本的后面即可。

确定合并脚本的真正目的后,我们得到:

// ==UserScript==
// @name        _Follow People on INK361
// @description Follow People from our FB Page's list INK361
// @include     http://ink361.com/#/users/*
// @exclude     http://ink361.com/#/users/223888036*
// @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 major design
    change introduced in GM 1.0.
    It restores the sandbox.
*/

var urlsToLoad  = [
    'http://ink361.com/#/users/14565576/photos'
    , 'http://ink361.com/#/users/218217815/photos'
    , 'http://ink361.com/#/users/202670894/photos'
    , 'http://ink361.com/#/users/201771644/photos'
    , 'http://ink361.com/#/users/217243779/photos'
    , 'http://ink361.com/#/users/218295748/photos'
    , 'http://ink361.com/#/users/218273533/photos'
    , 'http://ink361.com/#/users/30742610/photos'
    , 'http://ink361.com/#/users/193869245/photos'
    , 'http://ink361.com/#/users/215062853/photos'
];

/*--- Operation:
    1) If the button is "follow" then it clicks it.
    2) If the button is, or becomes "unfollow", then go to the next page.
    3) If there is no button or the button stops working, it stays on the current page.
*/

//--- Note that contains() is CASE-SENSITIVE.
waitForKeyElements (
    "#relationship a.simplebutton:contains('follow')",
    clickOnFollowButton
);

function clickOnFollowButton (jNode) {

    if ( /^\s*follow\s*$/i.test (jNode.text() ) ) {
        //--- Button is "follow"; click it.

        var clickEvent  = document.createEvent ('MouseEvents');
        clickEvent.initEvent ('click', true, true);
        jNode[0].dispatchEvent (clickEvent);
    }
    else if ( /^\s*unfollow\s*$/i.test (jNode.text() ) ) {
        //--- Unfollow button is already (or now) set.  Go to next page.
        jNode.text ("palate cleanser");
        GotoNextURL ();
    }

    return true;    //--- This node is reused, never mark it as found.
}


function GotoNextURL () {
    var numUrls     = urlsToLoad.length;
    var urlIdx      = urlsToLoad.indexOf (location.href);
    urlIdx++;

    //-- Don't loop the list of sites.
    if (urlIdx < numUrls) {
        location.assign (urlsToLoad[urlIdx]);
    }
}
<小时/>

但这假设单击“关注”按钮使屏幕保持在同一页面上。 是吗?(是)

我无法登录该站点,因此无法完全测试该脚本。如果它不起作用,请列出控制台(Firebug 或 Firefox)显示的所有错误消息,并准确描述其行为方式。

关于javascript - 如何组合这 2 个 Greasemonkey 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12340231/

相关文章:

php - HTML ajax 响应不执行 javascript 函数

javascript - 如何在单击按钮时从数组中删除项目

javascript - 如何创建自己的交互式全景网站

javascript - innerHTML 不会在每个 keydown 事件中改变

Greasemonkey:使用@updateURL 脚本检查更新的频率

javascript - Jquery 用户界面 : Draggable drag event not working

javascript - 使用 Javascript 验证动态生成的表单

javascript - Tampermonkey,等待在另一个选项卡操作系统中设置可以读取的值

javascript - 执行 php 后如何加载用户脚本?

database - 使用 Greasemonkey 时存储数据