rstoenescu / WB-for-39th

// ==UserScript==
// @name        WB-for-39th
// @namespace   39th.wb
// @author      TheXpert [156292]
// @description No description :)
// @include     *.torn.com/factions.php?step=your*
// @version     1.0.0
// @require     http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js
// ==/UserScript==

'use strict';

this.$ = this.jQuery = jQuery.noConflict(true);

function injectWarHTML(id)
{
    // this function is depreciated and not used anymore because of new torn rules
    var prefix = location.protocol === 'https:' ? 'https' : 'http';
    $.get(location.protocol + "//www.torn.com/factions.php?rfcv=" + getRFC() + "&step=getChainWarData&warID=" + id, function(data){
      var targets = $("ul.f-war-list li.descriptions")
      var parent = $('<li class="descriptions" style="margin-top: 5px; margin-bottom: 5px"></li>')
      parent.append(data)
      parent.find("ul.member-list li div span.t-red").each(function (i, div) {
        $(div).parent().parent().remove()
      })
      var progress = $(parent.find("div.progress-wrap div.progress-cont")[1]).attr('title')
      var pInt = parseFloat(progress)
      parent.find("div.status-desc div.f-left div.cont").html('<strong>' + progress + '</strong> - ' + (100 - pInt).toFixed(2) + '%')
      parent.attr('progress', pInt)
      parent.find("div.status-desc").css({borderRadius: '5px 5px 0 0'})

      if (targets.length === 0) {
        $("ul.f-war-list").prepend(parent);
        return
      }

      for (i = 0; i < targets.length; i++) {
        var target = $(targets[i])
        if (pInt >= parseFloat(target.attr('progress'))) {
          parent.insertBefore(target)
          return
        }
      }

      parent.insertAfter(target)
    });
}

function getWarIds () {
  var warIds = []
  $('li.not-member').each(function (i, div) {
    warIds.push($(div).attr('warid'))
  })
  return warIds
}

function onWBReady (cb) {
    setTimeout(function () {
        if ($('div.f-msg.red').length > 0) {
            return cb()
        }
        onWBReady(cb)
    }, 1000)
}

function init () {
  var wars = getWarIds()
  $('div.faction-respect-wars-wp li.not-member').each(function (i, div) {
    $(div).remove()
  })
  $('div.faction-respect-wars-wp li.inactive').each(function (i, div) {
    $(div).remove()
  })
  wars.forEach(function (id) {
    injectWarHTML(id)
  })

  var refreshLink = $("<div class='btn' style='width: 100px; margin: 10px 0 5px; cursor: pointer'>Refresh</div>")
  refreshLink.click(function() {
    $("ul.f-war-list li.descriptions").remove()
    wars.forEach(function (id) {
      injectWarHTML(id)
    })
  })
  refreshLink.insertAfter($("div.f-msg.red"))
}

if (location.href.indexOf('factions.php?step=your') !== -1) {
  onWBReady(init);
}