Torn City - Travel Run One Click Update by Xiphias[187717]

Update Travel Run and the Travel Thread with one button click.

目前為 2016-04-26 提交的版本,檢視 最新版本

// ==UserScript==
// @author      Xiphias[187717]
// @name        Torn City - Travel Run One Click Update by Xiphias[187717]
// @namespace   Xiphias[187717]
// @description Update Travel Run and the Travel Thread with one button click.
// @include     http://www.torn.com/index.php
// @include     https://www.torn.com/index.php
// @version     1.0.2
// @grant       none
// ==/UserScript==



function getMarketData() {
	var country = $('.info-msg-cont').children(":visible").text().replace(/\s+/g, " ");
	var hosp_check = country.match("in hospital");
	if (hosp_check) {
		return "hospital";
	} else {
		var market_string = "";
		var market_list = $('.travel-agency-market > ul > li > span');
		if (!market_list.length) {
			return "no market";
		} else {
			market_list.each(function () {
				market_string += $(this).children(":visible").text().replace(/\s+/g, " ") + "\n";
			});

			return country + "\n" + market_string;
		}
	}
}

function ajax_post_TravelRun(market_data) {
	$.ajax({
		type : 'POST',
		url : 'http://travelrun.torncentral.com/update2.php',
		data : {
			'data' : market_data
		},
		error : function (data) {
			//console.log(data);
            $('#travelrun_msg').text("Likely updated TravelRun");
		}, success : function (data) {
            //console.log("succ");
            //console.log(data);
        }
	});
}

function updateTravelRun() {
	var market_data = getMarketData();
	if (market_data == "hospital") {
		$('#travelrun_msg').text("You are in the hospital. You cannot possibly see the market from the hospital you filthy liar.");
	} else if (market_data == "no market") {
		$('#travelrun_msg').text("Error. I cannot seem you find the market on this page. Please try again or contact the script creator");
	} else {
        ajax_post_TravelRun(market_data);
	}
}

function add_update_button() {
    var button = $('<button style="float: right; position: relative;  top: -5px; margin-bottom: 40px;" >Update TravelRun</button>');
  var message_cont = $('<span id="travelrun_msg" style="float: right;  position: relative; left: -5px;" ></span>');
    button.button().click(function( event ) {
        updateTravelRun();
        sendForumPost();
      });
    if (getCountry()) {
        $('.footer > .container').prepend(message_cont);
        $('.footer > .container').prepend(button);
    }
}

function market_table_creater() {
    var tableStr = '<table id="traveltable" width="100%" cellspacing="1" cellpadding="0" border="0"> \
                    <tbody> \
                        <tr id="traveltable_header" bgcolor="#151515" style="font-family:Montserrat, sans-serif;letter-spacing:2px;"> \
                            <th><span style="color:#ffffff;"><strong>TYPE</strong></span></th> \
                            <th><span style="color:#ffffff;"><strong>ITEM</strong></span></th> \
                            <th><span style="color:#ffffff;"><strong>COST</strong></span></th> \
                            <th><span style="color:#ffffff;"><strong>STOCK</strong></span></th> \
                        </tr>  \
                     </tbody> \
                   </table>';
    var table = $(tableStr);

    var rows = "";
    $('.travel-agency-market > ul > li > span').each(function() {
      var elem = $(this);
      var type = elem.find('.type').text().replace(/type:/ig, "").trim();
      var name = elem.find('.name').text().replace(/^\s+x\d+(?:[.,]\d+)*/ig, "").trim();
      var cost = elem.find('.cost > .c-price').text().trim();
      var stock = elem.find('.stock > .stck-amount').text().trim();
      var row = '<tr style="font-family:Montserrat, sans-serif;">\
                    <td>' + type + '</td>\
                    <td>'+name + '</td>\
                    <td><center>'+cost + '</center></td>\
                    <td><center>' + stock + '</center></td>\
                 </tr>';
      rows += row;
    });

    table.append(rows);
    return table;
}


function forumPoster(post) {
  
  $.ajax({
		type : 'POST',
		url : 'http://www.torn.com/forums.php?rfcv=' + getCookie('rfc_v'),
		data : {
			'action' : 'reply',
			'belongs' : 0,
      'belongs_id' : 0,
      'forum_id'   : 10,
      'post' : post,
      'post_id' : 0,
      'remove_quote' : 0,
      'step' : 'reply',
      'submitpost' : 'REPLY',
      'subscribe' : 'false',
      'thread_id' : 15907728,
      'title'   : '',
		},
		success : function (data) {
            setTimeout(function() {
                $('#travelrun_msg').text("Table posted on the forum.");
            }, 1000);
			
		}
	});

}

function getCountry() {
    return $('.user-info').text().replace(/\r?\n|\r/g, "").replace(/.*you are in (.*?) and have \$.*/gi, '$1');
}

function sendForumPost() {
    var country = getCountry();
    var market_table = market_table_creater();
    var post = "<h1>" + country + "</h1><br /><br /><br />" + market_table[0].outerHTML;

    forumPoster(post);
}

/* Run script */
add_update_button();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址