ElBort / eRepublik AutoShouter

// ==UserScript==
// @name         eRepublik AutoShouter
// @namespace    https://www.erepublik.com/en/citizen/profile/2524994
// @version      2.1
// @description  Post in your feed automatically
// @author       El Bort
// @require      https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @include      http://*.erepublik.com/*
// @include      https://*.erepublik.com/*
// @updateURL    https://openuserjs.org/meta/ElBort/eRepublik_AutoShouter.meta.js
// @copyright    2017, ElBort (https://openuserjs.org/users/ElBort)
// @license      MIT
// ==/UserScript==

if (!localStorage.getItem("autoShout_setup")) {
  localStorage.setItem("autoShout_minTime", 5);
  localStorage.setItem("autoShout_maxTime", 6);
  localStorage.setItem("autoShout_amount", 0);
  localStorage.setItem("autoShout_medalSpam", "none");
  localStorage.setItem("autoShout_setup", true);
}

loadModules();

$("#feedTabs").append("<li id=\"autoShout_setup\"><a href=\"javascript:\" name=\"autoShout_setup\"><img src=\"https://dl.dropboxusercontent.com/s/j8qar3olx6gmwzp/h2_bg.png\" width=\"16\" height=\"15\" > Setup</a></li>");
$("#show_facebook_feed").remove();

$("#autoShout_setup").click(function () {
  $("#feedTabs>li").removeClass("active");
  $("#autoShout_setup").addClass("active");
  $("#wall_master").toggleClass("countryFeed");
  $("#citizen_feed").attr("class", "duo countryFeed");
  $("#shoutBox").addClass("disabled");
  $("#wallError").attr("style", "display: none;");
  $("#shout").prop('disabled', true).attr("placeholder", "Configuration menu of eRepublik autoShouter");
  $('#wall_post > span.controls > label.post_to_wall_as').hide();
  $('#wall_post select.post_to_wall_as').hide().prop('disabled', true);
  $('#citizen_feed:not(.alwaysShowDailyOrder)').removeClass('showDailyOrder');
  $('#wall_post > span.controls > label.post_to_wall_as, #wall_post select.post_to_wall_as').hide().prop('disabled', true);
  setTimeout(function () {
    $("#citizen_older_feeds").attr("style", "display: none;");
  }, 500);

  $("#wall_post_list>ul").html("<li class=\"wall_post firstPost\"><a class=\"user_pic\"><span></span><img src=\"https://dl.dropboxusercontent.com/s/j8qar3olx6gmwzp/h2_bg.png\" title=\"\"></a><div class=\"post_content\"><h6><a>eRepublik autoShouter</a><em>by El Bort</em></h6><p>autoShouter is a tool made to post shout automatically. El Bort is not responsible for the misuse of it. Be cool with your friends!</p></div></li>" +
    "<li class=\"wall_post\"><a class=\"user_pic\"><span></span><img src=\"https://dl.dropboxusercontent.com/s/j8qar3olx6gmwzp/h2_bg.png\" title=\"\"></a><div class=\"post_content\"><h6><a>autoShout setup</a><em>make it long</em></h6><p>Post between <select id=\"minTime_autoshout\">" + optionLoop(5, 30) + "</select> and <select id=\"maxTime_autoshout\">" + optionLoop(5, 30) + "</select> minutes<br><br>Post <select id=\"amount_autoshout\">" + optionLoop(0, 15) + "</select> shouts</p></div></li>" +
    "<li class=\"wall_post\"><a class=\"user_pic\"><span></span><img src=\"https://dl.dropboxusercontent.com/s/j8qar3olx6gmwzp/h2_bg.png\" title=\"\"></a><div class=\"post_content\"><h6><a>Anti-spam medals</a><em>hide medal shouts spam</em></h6><p>Level <select id=\"medal_spam\"><option value=\"high\">High</option><option value=\"medium\">Medium</option><option value=\"low\">Low</option><option value=\"none\">None</option></select><br><strong>None:</strong> do nothing.<br><strong>Low:</strong> remove actions.<br><strong>Medium:</strong> + low opacity.<br><strong>High:</strong> disable medal shouts</p></div></li>" +
    "<li class=\"wall_post\"><a class=\"user_pic\"><span></span><img src=\"https://dl.dropboxusercontent.com/s/j8qar3olx6gmwzp/h2_bg.png\" title=\"\"></a><div class=\"post_content\"><h6><a>reShout - quote</a><em>no more copy and paste</em></h6><p><select><option value=\"enable\">Enable</option><option value=\"disable\">Disable</option></select></p></div></li>" +
    //"<li class=\"wall_post\"><a class=\"user_pic\"><span></span><img src=\"https://dl.dropboxusercontent.com/s/j8qar3olx6gmwzp/h2_bg.png\" title=\"\"></a><div class=\"post_content\"><h6><a>Ignore module</a><em>they are scum</em></h6><p><a style=\"color: white; text-decoration: none;\" class=\"std_global_btn redColor tinySize\" id=\"ignoreList_button\" original-title=\"\">Ignore list</a></p></div></li>"+
    "<li class=\"wall_post\"><a class=\"user_pic\"><span></span><img src=\"https://dl.dropboxusercontent.com/s/j8qar3olx6gmwzp/h2_bg.png\" title=\"\"></a><div class=\"post_content\"><h6><a>Contact me</a><em>help me to keep working</em></h6><p><a href=\"https://www.erepublik.com/en/economy/donate-money/2524994\" style=\"color: white; text-decoration: none;\" class=\"std_global_btn greenColor tinySize\" id=\"donate_button\" original-title=\"\">Donate</a>" +
    "<br><br><a href=\"https://www.erepublik.com/en/main/messages-compose/2524994\" style=\"color: white; text-decoration: none;\" class=\"std_global_btn redColor tinySize\" id=\"bugReport_button\" original-title=\"\">Bug Report</a></p></div></li>");

  $("#minTime_autoshout").find("option[value=" + localStorage.getItem("autoShout_minTime") + "]").attr("selected", "selected");
  $("#maxTime_autoshout").find("option[value=" + localStorage.getItem("autoShout_maxTime") + "]").attr("selected", "selected");
  $("#amount_autoshout").find("option[value=" + localStorage.getItem("autoShout_amount") + "]").attr("selected", "selected");
  $("#amount_autoshout").find("option[value=0]").text("Infinite");
  $("#medal_spam").find("option[value=" + localStorage.getItem("autoShout_medalSpam") + "]").attr("selected", "selected");

  var previous;
  $("#minTime_autoshout").on('focus', function () {
    previous = Number($(this).attr("value"));
  }).change(function () {
    if (Number($(this).attr("value")) >= Number($("#maxTime_autoshout").attr("value"))) {
      $("#wallError").attr("style", "display: block;").text("Minimum time should be lower than maximum");
      $(this).val(previous);
    }
    else {
      previous = Number($(this).attr("value"));
      localStorage.setItem("autoShout_minTime", $(this).attr("value"));
      $("#wallError").attr("style", "display: block;border: 1px solid #A9F5BC; color: #298A08; background: #BEF781;").text("Minimum time changed to " + Number($(this).attr("value")) + " minutes");
    }
  });
  $("#maxTime_autoshout").on('focus', function () {
    previous = Number($(this).attr("value"));
  }).change(function () {
    if (Number($(this).attr("value")) <= Number($("#minTime_autoshout").attr("value"))) {
      $("#wallError").attr("style", "display: block;").text("Maximum time should be higher than minimum");
      $(this).val(previous);
    }
    else {
      previous = Number($(this).attr("value"));
      localStorage.setItem("autoShout_maxTime", $(this).attr("value"));
      $("#wallError").attr("style", "display: block;border: 1px solid #A9F5BC; color: #298A08; background: #BEF781;").text("Maximum time changed to " + Number($(this).attr("value")) + " minutes");
    }
  });
  $("#amount_autoshout").change(function () {
    localStorage.setItem("autoShout_amount", $(this).attr("value"));
    $("#wallError").attr("style", "display: block;border: 1px solid #A9F5BC; color: #298A08; background: #BEF781;").text("AutoShout will stop after " + $(this).find("option[value=" + Number($(this).attr("value")) + "]").text() + " posts");
  });
  $("#medal_spam").change(function () {
    localStorage.setItem("autoShout_medalSpam", $(this).attr("value"));
    $("#wallError").attr("style", "display: block;border: 1px solid #A9F5BC; color: #298A08; background: #BEF781;").text("You changed the anti-spam medals level to " + $(this).attr("value"));
  });

  $("#feedTabs > li").click(function () {
    $("#wallError").attr("style", "display: none;");
    $("#citizen_older_feeds").attr("style", "display: block;");
  });
});

$("#newPost").attr("name", "newPost");
$("#newPost").after("<a id=\"newPost\" name=\"autoShout\" href=\"javascript:\" class=\"std_global_btn greenColor tinySize noPadding floatRight newFeedPost\" trigger=\"new_autoshout\" original-title=\"\"><span trigger=\"new_autoshout\"><b trigger=\"new_autoshout\">AutoShout</b></span></a>");

if (window.location.href.indexOf("?AutoShouter") >= 0) {
  localStorage.setItem("autoshout_token", getToken());
  startAutoShouter();
}

$("a[name=autoShout]").click(function () {
  var post_message = $("textarea[id=shout]").attr("value");

  if (post_message.length === 0) {
    $("#wallError").css("display", "block");
    $("#wallError").text("Your post was either empty or invalid");
  }
  else {
    var feed = getFeedData();
    localStorage.setItem("autoShout_count", 0);
    localStorage.setItem("autoShout_message", post_message);
    localStorage.setItem("autoShout_feedUrl", feed.url);
    localStorage.setItem("autoShout_feedActive", feed.name);

    var url = window.location.href + "?AutoShouter";
    var newTab = window.open(url, "_blank", "toolbar=0,resizable=no,menubar=0,width=490,height=475,scrollbars=0,resizable=0,top=200,left=500");
    newTab.focus();

    setTimeout(function () {
      updateFeed(feed.url);
    }, 2500);
  }
});

function startAutoShouter() {
  var maxTime = localStorage.getItem("autoShout_maxTime");
  var minTime = localStorage.getItem("autoShout_minTime");
  var amount = localStorage.getItem("autoShout_amount");
  var autoShout_message = localStorage.getItem("autoShout_message");
  var feedActive = localStorage.getItem("autoShout_feedActive");
  var feedUrl = localStorage.getItem("autoShout_feedUrl");
  var time = randTime(Number(minTime), Number(maxTime));
  var token = getToken();

  var tab_autoshout = "<div class=\"facebook_friends\" id=\"invite_step1\" style=\"left: 50%; margin-left: -260px; z-index: 25004; background-image: none ; box-shadow: none  ; position: absolute; top: 0px; margin-top: 0px; display: block;\">" +
    "<h2 class=\"autoshout_header\"><span>AutoShouter</span><span style=\"font-size:70%\"> by <a href=\"https://www.erepublik.com/en/citizen/profile/2524994\" target=\"_blank\">El Bort</a></span></h2>" +
    "<h4>AutoShouter in progress, do NOT close this tab.</h4>" +
    "<div class=\"listing\">" +
    "Next shout will be post in... <strong id=\"timer\"></strong>" + "</br><span><a>Active feed: " + feedActive + "</a></span>" +
    "</br><p><strong>Shout message: </strong></br>" + autoShout_message.replace(/\n/g, "<br />") + "</p></div>" +
    "<div class=\"listing\"><strong>Timer setup:</strong></br>Shout between <select id=\"minTime_autoshout\">" + optionLoop(5, 30) + "</select> and <select id=\"maxTime_autoshout\">" + optionLoop(5, 30) + "</select> minutes." +
    "<br><strong>Amount setup:</strong></br>Post <select id=\"amount_autoshout\">" + optionLoop(0, 15) + "</select> shouts<br><br>" +
    "<a class=\"std_global_btn blueColor tinySize\" id=\"updateTime_autoshout\" original-title=\"\">Save</a> <a target=\"_blank\" href=\"https://www.erepublik.com/en/main/messages-compose/2524994\" class=\"std_global_btn redColor tinySize floatRight\" original-title=\"\">Bug Report</a>" +
    "<p id=\"timer_updated\"></p>" +
    "</div>" +
    "<a class=\"std_global_btn redColor mediumSize\" id=\"stop_autoshout\" original-title=\"\">Stop AutoShouter</a>" +
    "<a href=\"https://www.erepublik.com/en/economy/donate-money/2524994\" target=\"_blank\" class=\"std_global_btn greenColor mediumSize floatRight\" id=\"donate_autoshout\" original-title=\"\">Donate</a>" +
    "</div>" +
    "<div class=\"lb_overlay js_lb_overlay\" style=\"height: 2150px; position: absolute; width: 100%; top: 0px; left: 0px; right: 0px; bottom: 0px; z-index: 9000; background: #F1F1F1;\"></div>";

  document.title = 'eRepublik autoShouter';
  $("body").append(tab_autoshout).css({
    "overflow-x": "hidden",
    "overflow-y": "hidden"
  });
  $(".autoshout_header").css("background-image", "url(https://dl.dropboxusercontent.com/s/j8qar3olx6gmwzp/h2_bg.png)");
  $(".facebook_friends").css({
    'background-image': 'none',
    'box-shadow': 'none'
  });
  $("#minTime_autoshout").find("option[value=" + minTime + "]").attr("selected", "selected");
  $("#maxTime_autoshout").find("option[value=" + maxTime + "]").attr("selected", "selected");
  $("#amount_autoshout").find("option[value=" + amount + "]").attr("selected", "selected");
  $("#amount_autoshout").find("option[value=0]").text("Infinite");

  var myShoutData = {
    _token: token,
    post_message: autoShout_message
  };
  var dataToBeSent = $.param(myShoutData, true);
  shout(dataToBeSent, feedUrl);

  var timer = time / 1000,
    display = document.querySelector('#timer');
  startTimer(timer, display);

  setInterval(function () {
    window.location.reload();
  }, time);

  var autoShout_count = Number(localStorage.getItem("autoShout_count")) + 1;
  localStorage.setItem("autoShout_count", autoShout_count);
  if (Number(localStorage.getItem("autoShout_amount")) !== 0) {
    setTimeout(function () {
      if (Number(localStorage.getItem("autoShout_count")) >= Number(localStorage.getItem("autoShout_amount"))) {
        window.close();
      }
    }, 5000);
  }
  $("#updateTime_autoshout").click(function () {
    if (Number($("#minTime_autoshout").attr("value")) >= Number($("#maxTime_autoshout").attr("value"))) {
      document.getElementById("timer_updated").innerHTML = "*<strong>Error:</strong> first number must be lower than second one.";
    }
    else {
      localStorage.setItem("autoShout_maxTime", Number($("#maxTime_autoshout").attr("value")));
      localStorage.setItem("autoShout_minTime", Number($("#minTime_autoshout").attr("value")));
      localStorage.setItem("autoShout_amount", Number($("#amount_autoshout").attr("value")));
      document.getElementById("timer_updated").innerHTML = "*<strong>Updated timer:</strong> next shout will be updated.";
    }
  });

  $("#stop_autoshout").click(function () {
    window.close();
  });
  $(".close").click(function () {
    window.close();
  });
}

function reshoutLink() {
  $('li[id^=post_]').each(function () {
    if (!$(this).hasClass("automatic")) {
      if (!$(this).hasClass("reshout")) {
        var shout_id = $(this).attr("id").split("_").pop();
        $(this).find(".post_actions").append("<span>ยท</span> <a href=\"javascript:\" class=\"reShout\" title=\"reShout\" rel=\"reshout\" id=\"" + $(this).attr("id") + "\" trigger=\"post_reshout\">reShout</a> " +
          "<span>ยท</span> <a href=\"javascript:\" class=\"quote\" title=\"quote\" rel=\"quote\" id=\"" + $(this).attr("id") + "\" trigger=\"post_quote\">Quote</a>");
        $(this).addClass("reshout");
      }
    }
  });

  $(".post_actions").on("click", ".reShout", function () {
    var id = $(this).attr("id");
    reShout(id);
  });

  $(".post_actions").on("click", ".quote", function () {
    var id = $(this).attr("id");
    reShout(id, true);
  });
}

function reShout(id, quote = false) {
  var token = getToken();
  var shoutData = getShoutData(id);
  var message = shoutData.message;

  if (quote === true) {
    message = shoutData.author.name + " " + shoutData.time + "\n" + shoutData.message;
  }

  var myShoutData = {
    _token: token,
    post_message: message
  };

  var dataToBeSent = $.param(myShoutData, true);
  shout(dataToBeSent, shoutData.feedUrl);

  setTimeout(function () {
    updateFeed(shoutData.feedUrl);
  }, 1000);
}

function medals(level) {
  var medals = level;
  if (level == "high") {
    console.log($("#citizen_older_feeds").attr("style"));
    $(".wall_post_list").find(".automatic").remove();
    if ($(".wall_post").length < 7 && !$("#autoShout_setup").hasClass("active") && $("#citizen_older_feeds").attr("style") == "display: block;") {
      document.querySelector("#citizen_older_feeds a").click();
      setTimeout(function () {
        loadModules();
      }, 1500);
    }
  }

  if (level == "low" || level == "medium") {
    if (level == "medium") {
      $(".automatic").css("opacity", 0.5);
    }
    $(".wall_post_list").find(".automatic .post_actions,.automatic .post_content .second_actions, .auto_img").remove();
    $("#citizen_feed li.wall_post.condensed > a.user_pic").css({
      "width": "16px",
      "height": "16px"
    });
    $("#citizen_feed li.wall_post.condensed > a.user_pic").children().css({
      "width": "16px",
      "height": "16px"
    });
  }
}

emojiButton();

function emojiButton() {
  $("a[name=newPost]").before("<a id=\"emojiButton\" href=\"javascript:\" class=\"\" original-title=\"\"><span><q class=\"emoji emoji_1f600\" draggable=\"true\">๐Ÿ˜€</q></span></a>");
  $("#emojiButton").after("<div id=\"emojiList\" style=\"display: none;\"></div>");
  $("#emojiList").append("<a><q class=\"emoji emoji_1f600\" code=\":grinning:\" id=\"emoji_1f600\" draggable=\"true\">๐Ÿ˜€</q><a><q class=\"emoji emoji_1f601\" code=\":grin:\" id=\"emoji_1f601\" draggable=\"true\">๐Ÿ˜</q><a><q class=\"emoji emoji_1f602\" code=\":joy:\" id=\"emoji_1f602\" draggable=\"true\">๐Ÿ˜‚</q><a><q class=\"emoji emoji_1f923\" code=\":rofl:\" id=\"emoji_1f923\" draggable=\"true\">๐Ÿคฃ</q><a><q class=\"emoji emoji_1f603\" code=\":smiling_face:\" id=\"emoji_1f603\" draggable=\"true\">๐Ÿ˜ƒ</q><a><q class=\"emoji emoji_1f604\" code=\":smile:\" id=\"emoji_1f604\" draggable=\"true\">๐Ÿ˜„</q><a><q class=\"emoji emoji_1f605\" code=\":sweat_smile:\" id=\"emoji_1f605\" draggable=\"true\">๐Ÿ˜…</q><a><q class=\"emoji emoji_1f606\" code=\":laughing:\" id=\"emoji_1f606\" draggable=\"true\">๐Ÿ˜†</q><a><q class=\"emoji emoji_1f609\" code=\":wink:\" id=\"emoji_1f609\" draggable=\"true\">๐Ÿ˜‰</q><a><q class=\"emoji emoji_1f60a\" code=\":blush:\" id=\"emoji_1f60a\" draggable=\"true\">๐Ÿ˜Š</q><a><q class=\"emoji emoji_1f60b\" code=\":yum:\" id=\"emoji_1f60b\" draggable=\"true\">๐Ÿ˜‹</q><a><q class=\"emoji emoji_1f60e\" code=\":sunglasses:\" id=\"emoji_1f60e\" draggable=\"true\">๐Ÿ˜Ž</q><a><q class=\"emoji emoji_1f60d\" code=\":heart_eyes:\" id=\"emoji_1f60d\" draggable=\"true\">๐Ÿ˜</q><a><q class=\"emoji emoji_1f618\" code=\":kissing_heart:\" id=\"emoji_1f618\" draggable=\"true\">๐Ÿ˜˜</q><a><q class=\"emoji emoji_1f617\" code=\":kissing:\" id=\"emoji_1f617\" draggable=\"true\">๐Ÿ˜—</q><a><q class=\"emoji emoji_1f619\" code=\":kissing_smiling_eyes:\" id=\"emoji_1f619\" draggable=\"true\">๐Ÿ˜™</q><a><q class=\"emoji emoji_1f61a\" code=\":kissing_closed_eyes:\" id=\"emoji_1f61a\" draggable=\"true\">๐Ÿ˜š</q><a><q class=\"emoji emoji_263a\" code=\":relaxed:\" id=\"emoji_263a\" draggable=\"true\">โ˜บ</q><a><q class=\"emoji emoji_1f642\" code=\":simple_smile:\" id=\"emoji_1f642\" draggable=\"true\">๐Ÿ™‚</q><a><q class=\"emoji emoji_1f917\" code=\":hugging:\" id=\"emoji_1f917\" draggable=\"true\">๐Ÿค—</q><a><q class=\"emoji emoji_1f914\" code=\":thinking:\" id=\"emoji_1f914\" draggable=\"true\">๐Ÿค”</q><a><q class=\"emoji emoji_1f610\" code=\":neutral_face:\" id=\"emoji_1f610\" draggable=\"true\">๐Ÿ˜</q><a><q class=\"emoji emoji_1f611\" code=\":expressionless:\" id=\"emoji_1f611\" draggable=\"true\">๐Ÿ˜‘</q><a><q class=\"emoji emoji_1f636\" code=\":no_mouth:\" id=\"emoji_1f636\" draggable=\"true\">๐Ÿ˜ถ</q><a><q class=\"emoji emoji_1f644\" code=\":rolling_eyes:\" id=\"emoji_1f644\" draggable=\"true\">๐Ÿ™„</q><a><q class=\"emoji emoji_1f60f\" code=\":smirk:\" id=\"emoji_1f60f\" draggable=\"true\">๐Ÿ˜</q><a><q class=\"emoji emoji_1f623\" code=\":persevere:\" id=\"emoji_1f623\" draggable=\"true\">๐Ÿ˜ฃ</q><a><q class=\"emoji emoji_1f625\" code=\":disappointed_relieved:\" id=\"emoji_1f625\" draggable=\"true\">๐Ÿ˜ฅ</q><a><q class=\"emoji emoji_1f62e\" code=\":open_mouth:\" id=\"emoji_1f62e\" draggable=\"true\">๐Ÿ˜ฎ</q><a><q class=\"emoji emoji_1f910\" code=\":zipper_mouth:\" id=\"emoji_1f910\" draggable=\"true\">๐Ÿค</q><a><q class=\"emoji emoji_1f62f\" code=\":hushed:\" id=\"emoji_1f62f\" draggable=\"true\">๐Ÿ˜ฏ</q><a><q class=\"emoji emoji_1f62a\" code=\":sleepy:\" id=\"emoji_1f62a\" draggable=\"true\">๐Ÿ˜ช</q><a><q class=\"emoji emoji_1f62b\" code=\":tired_face:\" id=\"emoji_1f62b\" draggable=\"true\">๐Ÿ˜ซ</q><a><q class=\"emoji emoji_1f634\" code=\":sleeping:\" id=\"emoji_1f634\" draggable=\"true\">๐Ÿ˜ด</q><a><q class=\"emoji emoji_1f60c\" code=\":relieved:\" id=\"emoji_1f60c\" draggable=\"true\">๐Ÿ˜Œ</q><a><q class=\"emoji emoji_1f913\" code=\":nerd:\" id=\"emoji_1f913\" draggable=\"true\">๐Ÿค“</q><a><q class=\"emoji emoji_1f61b\" code=\":stuck_out_tongue:\" id=\"emoji_1f61b\" draggable=\"true\">๐Ÿ˜›</q><a><q class=\"emoji emoji_1f61c\" code=\":stuck_out_tongue_winking_eye:\" id=\"emoji_1f61c\" draggable=\"true\">๐Ÿ˜œ</q><a><q class=\"emoji emoji_1f61d\" code=\":stuck_out_tongue_closed_eyes:\" id=\"emoji_1f61d\" draggable=\"true\">๐Ÿ˜</q><a><q class=\"emoji emoji_1f924\" code=\":drooling:\" id=\"emoji_1f924\" draggable=\"true\">๐Ÿคค</q><a><q class=\"emoji emoji_1f612\" code=\":unamused:\" id=\"emoji_1f612\" draggable=\"true\">๐Ÿ˜’</q><a><q class=\"emoji emoji_1f613\" code=\":sweat:\" id=\"emoji_1f613\" draggable=\"true\">๐Ÿ˜“</q><a><q class=\"emoji emoji_1f614\" code=\":pensive:\" id=\"emoji_1f614\" draggable=\"true\">๐Ÿ˜”</q><a><q class=\"emoji emoji_1f615\" code=\":confused:\" id=\"emoji_1f615\" draggable=\"true\">๐Ÿ˜•</q><a><q class=\"emoji emoji_1f643\" code=\":upside_down:\" id=\"emoji_1f643\" draggable=\"true\">๐Ÿ™ƒ</q><a><q class=\"emoji emoji_1f911\" code=\":money_mouth:\" id=\"emoji_1f911\" draggable=\"true\">๐Ÿค‘</q><a><q class=\"emoji emoji_1f632\" code=\":astonished:\" id=\"emoji_1f632\" draggable=\"true\">๐Ÿ˜ฒ</q><a><q class=\"emoji emoji_2639\" code=\":frowning_face:\" id=\"emoji_2639\" draggable=\"true\">โ˜น</q><a><q class=\"emoji emoji_1f641\" code=\":slight_frown:\" id=\"emoji_1f641\" draggable=\"true\">๐Ÿ™</q><a><q class=\"emoji emoji_1f616\" code=\":confounded:\" id=\"emoji_1f616\" draggable=\"true\">๐Ÿ˜–</q><a><q class=\"emoji emoji_1f61e\" code=\":disappointed:\" id=\"emoji_1f61e\" draggable=\"true\">๐Ÿ˜ž</q><a><q class=\"emoji emoji_1f61f\" code=\":worried:\" id=\"emoji_1f61f\" draggable=\"true\">๐Ÿ˜Ÿ</q><a><q class=\"emoji emoji_1f624\" code=\":triumph:\" id=\"emoji_1f624\" draggable=\"true\">๐Ÿ˜ค</q><a><q class=\"emoji emoji_1f622\" code=\":cry:\" id=\"emoji_1f622\" draggable=\"true\">๐Ÿ˜ข</q><a><q class=\"emoji emoji_1f62d\" code=\":sob:\" id=\"emoji_1f62d\" draggable=\"true\">๐Ÿ˜ญ</q><a><q class=\"emoji emoji_1f626\" code=\":frowning:\" id=\"emoji_1f626\" draggable=\"true\">๐Ÿ˜ฆ</q><a><q class=\"emoji emoji_1f627\" code=\":anguished:\" id=\"emoji_1f627\" draggable=\"true\">๐Ÿ˜ง</q><a><q class=\"emoji emoji_1f628\" code=\":fearful:\" id=\"emoji_1f628\" draggable=\"true\">๐Ÿ˜จ</q><a><q class=\"emoji emoji_1f629\" code=\":weary:\" id=\"emoji_1f629\" draggable=\"true\">๐Ÿ˜ฉ</q><a><q class=\"emoji emoji_1f62c\" code=\":grimacing:\" id=\"emoji_1f62c\" draggable=\"true\">๐Ÿ˜ฌ</q><a><q class=\"emoji emoji_1f630\" code=\":cold_sweat:\" id=\"emoji_1f630\" draggable=\"true\">๐Ÿ˜ฐ</q><a><q class=\"emoji emoji_1f631\" code=\":scream:\" id=\"emoji_1f631\" draggable=\"true\">๐Ÿ˜ฑ</q><a><q class=\"emoji emoji_1f633\" code=\":flushed:\" id=\"emoji_1f633\" draggable=\"true\">๐Ÿ˜ณ</q><a><q class=\"emoji emoji_1f635\" code=\":dizzy_face:\" id=\"emoji_1f635\" draggable=\"true\">๐Ÿ˜ต</q><a><q class=\"emoji emoji_1f621\" code=\":rage:\" id=\"emoji_1f621\" draggable=\"true\">๐Ÿ˜ก</q><a><q class=\"emoji emoji_1f620\" code=\":angry:\" id=\"emoji_1f620\" draggable=\"true\">๐Ÿ˜ </q><a><q class=\"emoji emoji_1f607\" code=\":innocent:\" id=\"emoji_1f607\" draggable=\"true\">๐Ÿ˜‡</q><a><q class=\"emoji emoji_1f920\" code=\":cowboy:\" id=\"emoji_1f920\" draggable=\"true\">๐Ÿค </q><a><q class=\"emoji emoji_1f921\" code=\":clown:\" id=\"emoji_1f921\" draggable=\"true\">๐Ÿคก</q><a><q class=\"emoji emoji_1f925\" code=\":pinocchio:\" id=\"emoji_1f925\" draggable=\"true\">๐Ÿคฅ</q><a><q class=\"emoji emoji_1f637\" code=\":mask:\" id=\"emoji_1f637\" draggable=\"true\">๐Ÿ˜ท</q><a><q class=\"emoji emoji_1f912\" code=\":sick:\" id=\"emoji_1f912\" draggable=\"true\">๐Ÿค’</q><a><q class=\"emoji emoji_1f915\" code=\":head_bandage:\" id=\"emoji_1f915\" draggable=\"true\">๐Ÿค•</q><a><q class=\"emoji emoji_1f922\" code=\":nauseated:\" id=\"emoji_1f922\" draggable=\"true\">๐Ÿคข</q><a><q class=\"emoji emoji_1f927\" code=\":sneezing:\" id=\"emoji_1f927\" draggable=\"true\">๐Ÿคง</q><a><q class=\"emoji emoji_1f608\" code=\":smiling_imp:\" id=\"emoji_1f608\" draggable=\"true\">๐Ÿ˜ˆ</q><a><q class=\"emoji emoji_1f47f\" code=\":imp:\" id=\"emoji_1f47f\" draggable=\"true\">๐Ÿ‘ฟ</q><a><q class=\"emoji emoji_1f480\" code=\":skull:\" id=\"emoji_1f480\" draggable=\"true\">๐Ÿ’€</q><a><q class=\"emoji emoji_1f47b\" code=\":ghost:\" id=\"emoji_1f47b\" draggable=\"true\">๐Ÿ‘ป</q><a><q class=\"emoji emoji_1f47d\" code=\":alien:\" id=\"emoji_1f47d\" draggable=\"true\">๐Ÿ‘ฝ</q><a><q class=\"emoji emoji_1f916\" code=\":robot:\" id=\"emoji_1f916\" draggable=\"true\">๐Ÿค–</q><a><q class=\"emoji emoji_1f4a9\" code=\":poop:\" id=\"emoji_1f4a9\" draggable=\"true\">๐Ÿ’ฉ</q><a><q class=\"emoji emoji_1f648\" code=\":see_no_evil:\" id=\"emoji_1f648\" draggable=\"true\">๐Ÿ™ˆ</q><a><q class=\"emoji emoji_1f649\" code=\":hear_no_evil:\" id=\"emoji_1f649\" draggable=\"true\">๐Ÿ™‰</q><a><q class=\"emoji emoji_1f64a\" code=\":speak_no_evil:\" id=\"emoji_1f64a\" draggable=\"true\">๐Ÿ™Š</q><a><q class=\"emoji emoji_1f385\" code=\":santa:\" id=\"emoji_1f385\" draggable=\"true\">๐ŸŽ…</q><a><q class=\"emoji emoji_1f478\" code=\":princess:\" id=\"emoji_1f478\" draggable=\"true\">๐Ÿ‘ธ</q><a><q class=\"emoji emoji_1f934\" code=\":prince:\" id=\"emoji_1f934\" draggable=\"true\">๐Ÿคด</q><a><q class=\"emoji emoji_1f470\" code=\":bride:\" id=\"emoji_1f470\" draggable=\"true\">๐Ÿ‘ฐ</q><a><q class=\"emoji emoji_1f935\" code=\":tuxedo:\" id=\"emoji_1f935\" draggable=\"true\">๐Ÿคต</q><a><q class=\"emoji emoji_1f487\" code=\":haircut:\" id=\"emoji_1f487\" draggable=\"true\">๐Ÿ’‡</q><a><q class=\"emoji emoji_1f48f\" code=\":couplekiss:\" id=\"emoji_1f48f\" draggable=\"true\">๐Ÿ’</q><a><q class=\"emoji emoji_1f491\" code=\":couple_with_heart:\" id=\"emoji_1f491\" draggable=\"true\">๐Ÿ’‘</q><a><q class=\"emoji emoji_1f4aa\" code=\":flex:\" id=\"emoji_1f4aa\" draggable=\"true\">๐Ÿ’ช</q><a><q class=\"emoji emoji_1f448\" code=\":point_left:\" id=\"emoji_1f448\" draggable=\"true\">๐Ÿ‘ˆ</q><a><q class=\"emoji emoji_1f449\" code=\":point_right:\" id=\"emoji_1f449\" draggable=\"true\">๐Ÿ‘‰</q><a><q class=\"emoji emoji_261d\" code=\":point_up:\" id=\"emoji_261d\" draggable=\"true\">โ˜</q><a><q class=\"emoji emoji_1f447\" code=\":point_down:\" id=\"emoji_1f447\" draggable=\"true\">๐Ÿ‘‡</q><a><q class=\"emoji emoji_270c\" code=\":victory:\" id=\"emoji_270c\" draggable=\"true\">โœŒ</q><a><q class=\"emoji emoji_1f918\" code=\":metal:\" id=\"emoji_1f918\" draggable=\"true\">๐Ÿค˜</q><a><q class=\"emoji emoji_1f44c\" code=\":ok_hand:\" id=\"emoji_1f44c\" draggable=\"true\">๐Ÿ‘Œ</q><a><q class=\"emoji emoji_1f44d\" code=\":thumbsup:\" id=\"emoji_1f44d\" draggable=\"true\">๐Ÿ‘</q><a><q class=\"emoji emoji_1f44e\" code=\":thumbsdown:\" id=\"emoji_1f44e\" draggable=\"true\">๐Ÿ‘Ž</q><a><q class=\"emoji emoji_270a\" code=\":fist:\" id=\"emoji_270a\" draggable=\"true\">โœŠ</q><a><q class=\"emoji emoji_1f44a\" code=\":punch:\" id=\"emoji_1f44a\" draggable=\"true\">๐Ÿ‘Š</q><a><q class=\"emoji emoji_1f44f\" code=\":clap:\" id=\"emoji_1f44f\" draggable=\"true\">๐Ÿ‘</q><a><q class=\"emoji emoji_1f485\" code=\":nail_care:\" id=\"emoji_1f485\" draggable=\"true\">๐Ÿ’…</q><a><q class=\"emoji emoji_1f440\" code=\":eyes:\" id=\"emoji_1f440\" draggable=\"true\">๐Ÿ‘€</q><a><q class=\"emoji emoji_1f48b\" code=\":kiss:\" id=\"emoji_1f48b\" draggable=\"true\">๐Ÿ’‹</q><a><q class=\"emoji emoji_2764\" code=\":heart:\" id=\"emoji_2764\" draggable=\"true\">โค</q><a><q class=\"emoji emoji_1f494\" code=\":broken_heart:\" id=\"emoji_1f494\" draggable=\"true\">๐Ÿ’”</q><a><q class=\"emoji emoji_1f4a3\" code=\":bomb:\" id=\"emoji_1f4a3\" draggable=\"true\">๐Ÿ’ฃ</q><a><q class=\"emoji emoji_1f4a5\" code=\":boom:\" id=\"emoji_1f4a5\" draggable=\"true\">๐Ÿ’ฅ</q><a><q class=\"emoji emoji_1f48d\" code=\":ring:\" id=\"emoji_1f48d\" draggable=\"true\">๐Ÿ’</q><a><q class=\"emoji emoji_1f48e\" code=\":diamond:\" id=\"emoji_1f48e\" draggable=\"true\">๐Ÿ’Ž</q><a><q class=\"emoji emoji_1f34c\" code=\":banana:\" id=\"emoji_1f34c\" draggable=\"true\">๐ŸŒ</q><a><q class=\"emoji emoji_1f351\" code=\":peach:\" id=\"emoji_1f351\" draggable=\"true\">๐Ÿ‘</q><a><q class=\"emoji emoji_1f340\" code=\":clover:\" id=\"emoji_1f340\" draggable=\"true\">๐Ÿ€</q><a><q class=\"emoji emoji_1f354\" code=\":hamburger:\" id=\"emoji_1f354\" draggable=\"true\">๐Ÿ”</q><a><q class=\"emoji emoji_1f355\" code=\":pizza:\" id=\"emoji_1f355\" draggable=\"true\">๐Ÿ•</q><a><q class=\"emoji emoji_1f32d\" code=\":hotdog:\" id=\"emoji_1f32d\" draggable=\"true\">๐ŸŒญ</q><a><q class=\"emoji emoji_1f37f\" code=\":popcorn:\" id=\"emoji_1f37f\" draggable=\"true\">๐Ÿฟ</q><a><q class=\"emoji emoji_1f382\" code=\":birthday_cake:\" id=\"emoji_1f382\" draggable=\"true\">๐ŸŽ‚</q><a><q class=\"emoji emoji_1f370\" code=\":cake:\" id=\"emoji_1f370\" draggable=\"true\">๐Ÿฐ</q><a><q class=\"emoji emoji_1f52a\" code=\":knife:\" id=\"emoji_1f52a\" draggable=\"true\">๐Ÿ”ช</q><a><q class=\"emoji emoji_2614\" code=\":umbrella:\" id=\"emoji_2614\" draggable=\"true\">โ˜”</q><a><q class=\"emoji emoji_1f525\" code=\":fire:\" id=\"emoji_1f525\" draggable=\"true\">๐Ÿ”ฅ</q><a><q class=\"emoji emoji_1f383\" code=\":jack_o_lantern:\" id=\"emoji_1f383\" draggable=\"true\">๐ŸŽƒ</q><a><q class=\"emoji emoji_1f384\" code=\":christmas_tree:\" id=\"emoji_1f384\" draggable=\"true\">๐ŸŽ„</q><a><q class=\"emoji emoji_1f386\" code=\":fireworks:\" id=\"emoji_1f386\" draggable=\"true\">๐ŸŽ†</q><a><q class=\"emoji emoji_1f387\" code=\":sparkle:\" id=\"emoji_1f387\" draggable=\"true\">๐ŸŽ‡</q><a><q class=\"emoji emoji_1f381\" code=\":gift:\" id=\"emoji_1f381\" draggable=\"true\">๐ŸŽ</q><a><q class=\"emoji emoji_1f4b0\" code=\":moneybag:\" id=\"emoji_1f4b0\" draggable=\"true\">๐Ÿ’ฐ</q><a><q class=\"emoji emoji_1f4b8\" code=\":money_with_wings:\" id=\"emoji_1f4b8\" draggable=\"true\">๐Ÿ’ธ</q><a><q class=\"emoji emoji_1f4b3\" code=\":credit_card:\" id=\"emoji_1f4b3\" draggable=\"true\">๐Ÿ’ณ</q><a><q class=\"emoji emoji_1f52b\" code=\":gun:\" id=\"emoji_1f52b\" draggable=\"true\">๐Ÿ”ซ</q><a><q class=\"emoji emoji_1f48a\" code=\":pill:\" id=\"emoji_1f48a\" draggable=\"true\">๐Ÿ’Š</q><a><q class=\"emoji emoji_2708\" code=\":airplane:\" id=\"emoji_2708\" draggable=\"true\">โœˆ</q><a><q class=\"emoji emoji_1f6eb\" code=\":airstrike:\" id=\"emoji_1f6eb\" draggable=\"true\">๐Ÿ›ซ</q><a><q class=\"emoji emoji_1f6ec\" code=\":failstrike:\" id=\"emoji_1f6ec\" draggable=\"true\">๐Ÿ›ฌ</q><a><q class=\"emoji emoji_1f989\" code=\":owl:\" id=\"emoji_1f989\" draggable=\"true\">๐Ÿฆ‰</q><a><q class=\"emoji emoji_1f63a\" code=\":smiley_cat:\" id=\"emoji_1f63a\" draggable=\"true\">๐Ÿ˜บ</q><a><q class=\"emoji emoji_1f638\" code=\":smile_cat:\" id=\"emoji_1f638\" draggable=\"true\">๐Ÿ˜ธ</q><a><q class=\"emoji emoji_1f639\" code=\":joy_cat:\" id=\"emoji_1f639\" draggable=\"true\">๐Ÿ˜น</q><a><q class=\"emoji emoji_1f63b\" code=\":heart_eyes_cat:\" id=\"emoji_1f63b\" draggable=\"true\">๐Ÿ˜ป</q><a><q class=\"emoji emoji_1f63c\" code=\":smirk_cat:\" id=\"emoji_1f63c\" draggable=\"true\">๐Ÿ˜ผ</q><a><q class=\"emoji emoji_1f63d\" code=\":kissing_cat:\" id=\"emoji_1f63d\" draggable=\"true\">๐Ÿ˜ฝ</q><a><q class=\"emoji emoji_1f640\" code=\":scream_cat:\" id=\"emoji_1f640\" draggable=\"true\">๐Ÿ™€</q><a><q class=\"emoji emoji_1f63f\" code=\":crying_cat_face:\" id=\"emoji_1f63f\" draggable=\"true\">๐Ÿ˜ฟ</q><a><q class=\"emoji emoji_1f63e\" code=\":pouting_cat:\" id=\"emoji_1f63e\" draggable=\"true\">๐Ÿ˜พ</q><a><q class=\"emoji emoji_1f43c\" code=\":panda_face:\" id=\"emoji_1f43c\" draggable=\"true\">๐Ÿผ</q><a><q class=\"emoji emoji_1f414\" code=\":chicken:\" id=\"emoji_1f414\" draggable=\"true\">๐Ÿ”</q>");

  $(".controls").on("click", "#emojiButton", function () {
    if ($("#emojiList").is(":visible")) {
      $("#emojiList").attr("style", "display: none;");
    }
    else {
      $("#emojiList").attr("style", "display: block;");
      var check = setInterval(function () {
        if (!$("#shoutBox").hasClass("focused")) {
          $("#emojiList").attr("style", "display: none;");
          clearInterval(check);
        }
      }, 1000);
    }
  });

  $("#emojiList").on("click", ".emoji", function () {
    var position = $("#shout").getCursorPosition();
    var content = $("#shout").val();
    var newContent = content.substr(0, position) + $(this).text() + content.substr(position);
    $('#shout').val(newContent);
    //$("#shout").val(content+$(this).text());
  });
}

function shout(dataToBeSent, feedUrl) {
  $.post("https://www.erepublik.com/es/main/" + feedUrl + "-post/create/", dataToBeSent, function (data, textStatus) {
    if (textStatus == "error") {
      alert("AutoShout: error. Try again");
    }
    else if (data.message == 1) {
      setTimeout(function () {
        updateFeed(feedUrl);
      }, 2500);
    }
    else if (data.message === 0) {
      $("#wallError").css("display", "block");
      $("#wallError").css("display", "block").text(data.error_message);
    }
    else {
      alert("AutoShout: error. Refresh please");
    }
  }, "json");
}

$("#feedTabs > li").click(function () {
  setTimeout(function () {
    loadModules();
  }, 1000);
});
$(".previous_posts").on("click", ".blue_arrow_down_medium", function () {
  setTimeout(function () {
    loadModules();
  }, 1000);
});

function loadModules() {
  reshoutLink();
  medals(localStorage.getItem("autoShout_medalSpam"));
}

function updateFeed(feedUrl) {
  var token = getToken();
  var feedData = {
    _token: token,
    page: 1
  };
  var dataFeed = $.param(feedData, true);
  $.post("https://www.erepublik.com/en/main/" + feedUrl + "-post/retrieve/", dataFeed, function (data, textStatus) {
      if (textStatus == "error") {
        alert("Error. Try again");
      }
      else {
        $("#wall_post_list").html("<ul>" + data + "</ul>");
        $("#shout").val("");
        $("#shoutBox").removeClass("focused");
        $(".controls").css("display", "none");
      }
    }, "html").fail(function (xhr, status, error) {
      alert("Error, try again");
    })
    .done(function () {
      loadModules();
    });
}

function getToken() {
  var token = $("#award_token").attr("value");
  return token;
}

function getShoutData(shoutId) {
  var shoutTime = $("#" + shoutId + "> .post_content > h6 > em").text();
  var shoutMessage = $("#" + shoutId + "> .post_content > p").text();
  var shoutFeed = getFeedData();
  var citizenId = Number($("#" + shoutId + "> .user_pic").attr("href").split("/").pop());
  var citizenName = $("#" + shoutId + "> .user_pic > img").attr("alt");
  var citizenAvatar = $("#" + shoutId + "> .user_pic > img").attr("src");

  var data = {
    "id": Number(shoutId.split("_").pop()),
    "message": shoutMessage,
    "time": shoutTime,
    "feedName": shoutFeed.name,
    "feedUrl": shoutFeed.url,
    "author": {
      "id": citizenId,
      "name": citizenName,
      "avatar": citizenAvatar
    }
  };
  return data;
}

function getFeedData() {
  var feedData = [];
  $("#feedTabs > li").each(function () {
    if ($(this).hasClass("active")) {
      switch ($(this).attr("id")) {
        case "show_party_feed":
          feedData.url = "party";
          feedData.name = $("#show_party_feed").find('a').attr("title");
          break;
        case "show_friends_feed":
          feedData.url = "wall";
          feedData.name = "Friends";
          break;
        case "show_regiment_feed":
          feedData.url = "group";
          feedData.name = $(this).find('a').attr("title");
          break;
        case "show_country_feed":
          feedData.url = "country";
          feedData.name = $(this).find('a').attr("title");
      }
    }
  });
  return feedData;
}

function optionLoop(min, max) {
  var list = "";
  for (var i = min; i <= max; i++) {
    list += "<option value=\"" + i + "\">" + i + "</option>";
  }
  return list;
}

function randTime(minTime, maxTime) {
  var randTime = Math.floor(Math.random() * (maxTime * 60 * 1000 - minTime * 60 * 1000 + 1)) + minTime * 60 * 1000;
  return randTime;
}

function startTimer(duration, display) {
  var start = Date.now(),
    diff,
    minutes,
    seconds;

  function timer() {
    diff = duration - (((Date.now() - start) / 1000) | 0);
    minutes = (diff / 60) | 0;
    seconds = (diff % 60) | 0;

    minutes = minutes < 10 ? "0" + minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;

    display.textContent = minutes + ":" + seconds;

    if (diff <= 0) {
      start = Date.now() + 1000;
    }
  }
  timer();
  setInterval(timer, 1000);
}

(function ($, undefined) {
  $.fn.getCursorPosition = function () {
    var el = $(this).get(0);
    var pos = 0;
    if ('selectionStart' in el) {
      pos = el.selectionStart;
    }
    else if ('selection' in document) {
      el.focus();
      var Sel = document.selection.createRange();
      var SelLength = document.selection.createRange().text.length;
      Sel.moveStart('character', -el.value.length);
      pos = Sel.text.length - SelLength;
    }
    return pos;
  };
})(jQuery);