WF Explorer Next Planet Button

Adds a next planet button for probes which launches fleet to next planet. For www.war-facts.com . Idea from old script, written from scratch for new interface.

目前为 2015-05-08 提交的版本。查看 最新版本

// ==UserScript==
// @name        WF Explorer Next Planet Button
// @namespace   https://greasyfork.org/en/users/10321-nikitas
// @description Adds a next planet button for probes which launches fleet to next planet. For www.war-facts.com . Idea from old script, written from scratch for new interface.
// @include     http://*.war-facts.com/fleet.php*
// @version     2
// @grant       none
// ==/UserScript==




$(window).load(function(){

    var classificationNode = document.getElementById('fleetClass');
    var isExplorer = document.evaluate("//text()[contains(.,'Explorer') or contains(.,'Sentry') or contains(.,'Probe Rush')]", classificationNode, null, XPathResult.BOOLEAN_TYPE, null).booleanValue;


    if (isExplorer){

        var info = document.getElementById('navData').getElementsByTagName('div')[4];
        var infoSpan = info.getElementsByTagName('span')[0];

        // not  containing world in the span.
        var isAtSystemEntrance  =  ! (document.evaluate("//text()[contains(.,'World:')]", infoSpan, null, XPathResult.BOOLEAN_TYPE, null).booleanValue);



        //alert("Is explorer = " + isExplorer);
        //alert("Is at system Entrance = " + isAtSystemEntrance);
        var currentPlanet = info.getElementsByTagName('a')[0].innerHTML;


        //alert(currentPlanet);

        var optionGroup = document.getElementById('target1').getElementsByTagName('optgroup')[0].getElementsByTagName('option');

        var i = 0 , found = false,  optionsLength = optionGroup.length;
        var nextPlanetOption, finishedSystem = false;



        //if I am at system entrance
        if (isAtSystemEntrance){
            found = true;
            nextPlanetOption = optionGroup[0].value;
        }


        //if PlanetLess system
        if (optionsLength == 0 ) {
            found = true;
            finishedSystem = true;
        }


        // If I am at a planet, Find next planet through the local target option list

        while ( (i < optionsLength) && (found == false) ) {

            if (optionGroup[i].innerHTML == currentPlanet ){
//            alert("Checking option number " + i);

                found = true;

                if ( i == optionsLength -1 ){
                    finishedSystem = true;
                } else {
                    nextPlanetOption = optionGroup[i+1].value;
                }
            }

            i++;

        }


//   alert("finished system = " + finishedSystem);
//   alert("next planet = " + nextPlanetOption);


//       alert("Reached finished System");

        if (finishedSystem) {
//            alert("Inside finished System");
            document.getElementById('missionData').innerHTML += '<input  class = "greenbutton darkbutton" type="button" id="nextPlanetButton" value = "Done" onclick="openStarMap()" />';
            document.getElementById('nextPlanetButton').addEventListener('click', openStarMap, false);
        } else {
//            alert("Inside NOT finished System");
            document.getElementById('missionData').innerHTML += '<input  class = "greenbutton darkbutton" type="button" id="nextPlanetButton" value = "Next Planet" onclick="selectNextPlanet()" />';
            document.getElementById('nextPlanetButton').addEventListener('click', selectNextPlanet, false);

        }

    }



    function selectNextPlanet(){
        jQuery('#target1').val(nextPlanetOption).trigger ('change');

        unsafeWindow.hasLaunched = false;
        window.setTimeout(launchFleet,100);
    }

    function openStarMap(){
        var starMapLink;
        if (isAtSystemEntrance){
            starMapLink  = document.getElementById('navData').getElementsByTagName('div')[10].getElementsByTagName('a')[0].href;
        } else {
            starMapLink  = document.getElementById('navData').getElementsByTagName('div')[11].getElementsByTagName('a')[0].href;
        }

        starMapLink = starMapLink.substring(19, starMapLink.length - 3 );   //Keep only the link, throw away the functions

        // mapWin is war-facts.com function to open javascript map
        mapWin(starMapLink);
    }



    function launchFleet(){
        var launchButton = document.getElementById('mButton').getElementsByTagName('input')[0];

        if (launchButton) { //So Button has been created
            getMission('launch');   //Launch Fleet
            unsafeWindow.hasLaunched = true;
        } else {
            window.setTimeout(launchFleet,100);
        }




    }


});








/*---
From https://gist.github.com/BrockA/2625891
waitForKeyElements():  A utility function, for Greasemonkey scripts,
 that detects and handles AJAXed content.

 Usage example:

 waitForKeyElements (
 "div.comments"
 , commentCallbackFunction
 );

 //--- Page-specific function to do what we want when the node is found.
 function commentCallbackFunction (jNode) {
 jNode.text ("This comment changed by waitForKeyElements().");
 }

 IMPORTANT: This function requires your script to have loaded jQuery.
 */
function waitForKeyElements (
    selectorTxt,    /* Required: The jQuery selector string that
     specifies the desired element(s).
     */
    actionFunction, /* Required: The code to run when elements are
     found. It is passed a jNode to the matched
     element.
     */
    bWaitOnce,      /* Optional: If false, will continue to scan for
     new elements even after the first match is
     found.
     */
    iframeSelector  /* Optional: If set, identifies the iframe to
     search.
     */
) {
    var targetNodes, btargetsFound;

    if (typeof iframeSelector == "undefined")
        targetNodes     = $(selectorTxt);
    else
        targetNodes     = $(iframeSelector).contents ()
            .find (selectorTxt);

    if (targetNodes  &&  targetNodes.length > 0) {
        btargetsFound   = true;
        /*--- Found target node(s).  Go through each and act if they
         are new.
         */
        targetNodes.each ( function () {
            var jThis        = $(this);
            var alreadyFound = jThis.data ('alreadyFound')  ||  false;

            if (!alreadyFound) {
                //--- Call the payload function.
                var cancelFound     = actionFunction (jThis);
                if (cancelFound)
                    btargetsFound   = false;
                else
                    jThis.data ('alreadyFound', true);
            }
        } );
    }
    else {
        btargetsFound   = false;
    }

    //--- Get the timer-control variable for this selector.
    var controlObj      = waitForKeyElements.controlObj  ||  {};
    var controlKey      = selectorTxt.replace (/[^\w]/g, "_");
    var timeControl     = controlObj [controlKey];

    //--- Now set or clear the timer as appropriate.
    if (btargetsFound  &&  bWaitOnce  &&  timeControl) {
        //--- The only condition where we need to clear the timer.
        clearInterval (timeControl);
        delete controlObj [controlKey]
    }
    else {
        //--- Set a timer, if needed.
        if ( ! timeControl) {
            timeControl = setInterval ( function () {
                    waitForKeyElements (    selectorTxt,
                        actionFunction,
                        bWaitOnce,
                        iframeSelector
                    );
                },
                300
            );
            controlObj [controlKey] = timeControl;
        }
    }
    waitForKeyElements.controlObj   = controlObj;
}