简体   繁体   English

如何结合这两个Greasemonkey脚本?

[英]How do I combine these 2 Greasemonkey scripts?

I need to combine these 2 scripts for GM. 我需要结合这两个通用脚本。 One opens new pages from a list, and the other clicks on the 'follow' button. 一个打开一个列表中的新页面,另一个单击“关注”按钮。

Script 1: How to open a list of pages automatically and sequentially? 脚本1: 如何自动顺序打开页面列表?

Script 2: How do I click on this button with Greasemonkey? 脚本2: 如何使用Greasemonkey单击此按钮?

I've tried to combine them by myself but failed to create a working script that fully reloads pages, even they are put sequentially in the list (if you read the other question you'll understand what I mean). 我试图将它们自己合并,但是未能创建一个可以完全重新加载页面的工作脚本,即使它们被顺序地放在列表中(如果您阅读其他问题,您也会明白我的意思)。

This is what I've tried but it doesn't work as expected since it doesn't reload properly the page and go on with its tasks: 这是我尝试过的方法,但是由于无法正确地重新加载页面并继续执行其任务,因此无法正常工作:

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

Merging those scripts should be straightforward: Just standardize the metadata block and paste one script's javascript after the other's. 合并这些脚本 应该很简单:只需对元数据块进行标准化,然后将一个脚本的javascript粘贴到另一个脚本之后。

After determining the true purpose of the merged script, we get: 确定合并脚本的真正目的后,我们得到:

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

But this assumes that clicking the "Follow" button keeps the screen on the same page. 但这假设单击“跟随”按钮可使屏幕保持在同一页面上。 Does it? 可以? (Yes) (是)

I can't login to that site, so I can't test the script fully. 我无法登录该站点,因此无法完全测试脚本。 If it doesn't work, please list any error messages displayed by the console (Firebug or Firefox), and describe exactly how it behaves. 如果它不起作用,请列出控制台(Firebug或Firefox)显示的所有错误消息,并准确描述其行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM