Facebook
From Rafsaf, 3 Years ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 93
  1.   // ==UserScript==
  2.   // @name     Zbiórka przegląd Wojska.
  3.   // @version  3
  4.   // @match    *://*.plemiona.pl/game.php*screen=ally*&mode=members_troops
  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.     for (var i = 1; i < trs.length; i++) {
  26.       output += "<br>";
  27.       var tds = trs[i].querySelectorAll('td');
  28.       for (var j = 0; j < tds.length; j++) {
  29.         var value = String(tds[j].innerHTML).trim();
  30.         if (j == 0) {
  31.         value = value.slice(-17,-10);
  32.         }
  33.         output += value+",";
  34.       }
  35.     }
  36.   }
  37.   // To add player_id to current path
  38.   function getURL(id){
  39.     var params = new URLSearchParams(window.location.search);
  40.     params.set('player_id', id);
  41.     return "".concat(window.location.origin).concat(window.location.pathname, "?").concat(params.toString());
  42.   }
  43.   // Used to parse string from fetch func to html
  44.   function convertToHTML(str) {
  45.     var parser = new DOMParser();
  46.     var doc = parser.parseFromString(str, 'text/html');
  47.     return doc.body;
  48.   }
  49.   // Most important async function, after confirmation waits 2s then uses get_all_players_list.
  50.   // Then starts to fetch response from first player's page, then converts it.
  51.   // Then uses 'add current player info to output' on it, and so on, in the end prints some dialog with results.
  52.   async function renderPlayerTroops() {
  53.     var con = window.confirm("Czy chcesz zebrać wojska?(może to chwilkę potrwać ;) )");
  54.     if (con == false){
  55.       return;
  56.     }
  57.     // added today + 5h and output to local storage, in this term function uses 'ouput' from local storage
  58.     var today = (new Date()).getTime();
  59.     var after_5_hours = today + 1800000;
  60.     var storage_date = localStorage.getItem('storage_date');
  61.     var now = (new Date()).getTime();
  62.     if (now < storage_date) {
  63.       output = localStorage.getItem('output');
  64.     } else {
  65.       get_all_players_list();
  66.       for (var i = 0; i < players.length; i++){
  67.         if (i == 0){
  68.           await new Promise(function (resolve) {
  69.             return setTimeout(resolve, 2000);
  70.           });
  71.         }
  72.         var id = players[i].id;
  73.         var response = await fetch(getURL(id));
  74.         var html = await response.text();
  75.         var doc = convertToHTML(html);
  76.         add_current_player_info_to_output(doc);
  77.       }
  78.       localStorage.setItem('storage_date', after_5_hours);
  79.       localStorage.setItem('output', output);
  80.     }
  81.     var div = document.createElement("div");
  82.     div.contentEditable = "true";
  83.     div.style.width = "600px";
  84.     div.style.height = "auto";
  85.     div.style.border = "2px solid black";
  86.     div.style.left = "25%";
  87.     div.style.top = "40%";
  88.     div.style.position = "absolute";
  89.     div.style.background = "red";
  90.     div.style.color = "white";
  91.     div.innerHTML = output;
  92.     document.body.appendChild(div);
  93.   }
  94.   // creates button
  95.   function create_button(){
  96.     var td_place = document.querySelector('#menu_row2');
  97.     var td = document.createElement('td');
  98.     td.setAttribute('id', 'new_button');
  99.     td_place.appendChild(td);
  100.     var button_place = document.querySelector('#new_button');
  101.     var btn = document.createElement('btn');
  102.     btn.setAttribute('class', 'btn btn-default');
  103.     btn.innerHTML = 'Zbierz wojska';
  104.     button_place.appendChild(btn);
  105.     btn.addEventListener ("click", function() {
  106.       renderPlayerTroops();
  107.     });
  108.   }
  109.   create_button();
  110.