Facebook
From Rafsaf, 1 Week ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 22
  1. // ==UserScript==
  2. // @name     Zbiórka przegląd Obrona.
  3. // @version  3
  4. // @match    *://*.plemiona.pl/game.php*screen=ally*&mode=members_defense
  5. // ==/UserScript==
  6. // By Rafsaf
  7. var output = "";
  8. var players = [];
  9. // Adds players from current html to get array with players nicknames and ids
  10. function get_all_players_list(){
  11.   Array.from(document.querySelector('#ally_content .input-nicer').options).forEach(function(option_element) {
  12.     var option_text = option_element.text.trim();
  13.     var option_value = option_element.value;
  14.     if (option_text != 'Wybierz członka') {
  15.       players.push({
  16.         id: option_value,
  17.         nick: option_text
  18.       });
  19.     }
  20.   });
  21. }
  22. // Uses some methods to get all stuff from table with units from current html site
  23. function add_current_player_info_to_output(doc){
  24.   var trs = doc.querySelectorAll('.table-responsive .vis tr');
  25.   var attacks;
  26.   var coordinates;
  27.   for (var i = 1; i < trs.length; i++) {
  28.     output += "<br>";
  29.     var tds = trs[i].querySelectorAll('td');
  30.     if (i % 2 == 1){
  31.       for (var j = 0; j < tds.length; j++) {
  32.       var value = String(tds[j].innerHTML).trim();
  33.       if (j == 0) {
  34.         value = value.slice(-17,-10);
  35.         coordinates = value;
  36.       }
  37.       if (j == 12){
  38.         attacks = value;
  39.       }
  40.       output += value+",";
  41.       }
  42.     } else {
  43.       output += coordinates+",";
  44.       for (j = 0; j < tds.length; j++) {
  45.       value = String(tds[j].innerHTML).trim();
  46.       output += value + ",";
  47.       }
  48.       output += attacks+",";
  49.       }
  50.   }
  51. }
  52. // To add player_id to current path
  53. function getURL(id){
  54.   var params = new URLSearchParams(window.location.search);
  55.   params.set('player_id', id);
  56.   return "".concat(window.location.origin).concat(window.location.pathname, "?").concat(params.toString());
  57. }
  58. // Used to parse string from fetch func to html
  59. function convertToHTML(str) {
  60.   var parser = new DOMParser();
  61.   var doc = parser.parseFromString(str, 'text/html');
  62.   return doc.body;
  63. }
  64. // Most important async function, after confirmation waits 2s then uses get_all_players_list.
  65. // Then starts to fetch response from first player's page, then converts it.
  66. // Then uses 'add current player info to output' on it, and so on, in the end prints some dialog with results.
  67. async function renderPlayerTroops() {
  68.   var con = window.confirm("Czy chcesz zebrać wojska?(może to chwilkę potrwać ;) )");
  69.   if (con == false){
  70.     return;
  71.   }
  72.   var today = (new Date()).getTime();
  73.   var after_5_hours = today + 1800000;
  74.   var storage_date = localStorage.getItem('storage_date_obrona');
  75.   var now = (new Date()).getTime();
  76.   if (now < storage_date) {
  77.     output = localStorage.getItem('output_obrona');
  78.   } else {
  79.   get_all_players_list();
  80.   for (var i = 0; i < players.length; i++){
  81.     if (i == 0){
  82.       await new Promise(function (resolve) {
  83.         return setTimeout(resolve, 2000);
  84.       });
  85.   }
  86.     var id = players[i].id;
  87.     var response = await fetch(getURL(id));
  88.     var html = await response.text();
  89.     var doc = convertToHTML(html);
  90.     add_current_player_info_to_output(doc);
  91.     }
  92.     localStorage.setItem('storage_date_obrona', after_5_hours);
  93.     localStorage.setItem('output_obrona', output);
  94.   }
  95.   var div = document.createElement("div");
  96.   div.contentEditable = "true";
  97.   div.style.width = "600px";
  98.   div.style.height = "auto";
  99.   div.style.border = "2px solid black";
  100.   div.style.left = "25%";
  101.   div.style.top = "40%";
  102.   div.style.position = "absolute";
  103.   div.style.background = "red";
  104.   div.style.color = "white";
  105.   div.innerHTML = output;
  106.   document.body.appendChild(div);
  107. }
  108. function create_button(){
  109.   var td_place = document.querySelector('#menu_row2');
  110.   var td = document.createElement('td');
  111.   td.setAttribute('id', 'new_button');
  112.   td_place.appendChild(td);
  113.   var button_place = document.querySelector('#new_button');
  114.   var btn = document.createElement('btn');
  115.   btn.setAttribute('class', 'btn btn-default');
  116.   btn.innerHTML = 'Zbierz Obronę';
  117.   button_place.appendChild(btn);
  118.   btn.addEventListener ("click", function() {
  119.     renderPlayerTroops();
  120.   });
  121. }
  122. create_button();