Torn Rackets

description

目前為 2020-02-22 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Torn Rackets
// @namespace    namespace
// @version      0.4
// @description  description
// @author       tos
// @match        *.torn.com/city.php*
// @grant        GM_addStyle
// @grant        GM_setClipboard
// ==/UserScript==

GM_addStyle(`
#x_button_right {
  background-color: #c13c3c;
  color: #eaeaea;
  cursor: pointer;
  padding: 1em;
  position: fixed;
  right: 0;
}
#x_map_info {
  float: left;
  margin-left: 20px;
}
#x_map_info table {
  font-family: monospace;
}
#x_map_info thead {
  color: #000;
  font-weight: bold;
}
#x_map_info tbody: {
  color: #000;
}
#x_map_info td {
  padding: 0.5em;
  text-align: left;
}
#x_map_info td[data-tID] {
  cursor: pointer;
}
#x_map_info th {
  cursor: pointer;
  padding: 0.5em;
  text-align: left;
}

`)

//----------------------------------------------------------------------------------------------------------------------------------------
var TableSorter = {
    makeSortable: function(table){
        var _this = this; // Store context of this in the object
        var th = table.tHead, i;
        th && (th = th.rows[0]) && (th = th.cells);
        if (th){
            i = th.length;
        }else{
            return; // if no `<thead>` then do nothing
        }
        // Loop through every <th> inside the header
        while (--i >= 0) (function (i) {
            var dir = 1;
            // Append click listener to sort
            th[i].addEventListener('click', function () {
                _this._sort(table, i, (dir = 1 - dir));
            });
        }(i));
    },
    _sort: function (table, col, reverse) {
        var tb = table.tBodies[0], // use `<tbody>` to ignore `<thead>` and `<tfoot>` rows
        tr = Array.prototype.slice.call(tb.rows, 0), // put rows into array
        i;
        reverse = -((+reverse) || -1)
        console.log(reverse)
        // Sort rows
        if (col === 0 || col === 1 || col === 3 || col === 5) { //sort integer columns
          tr = tr.sort(function (a, b) {
            if (reverse === 1) return parseInt(b.cells[col].textContent.trim()) - parseInt(a.cells[col].textContent.trim())
            else return parseInt(a.cells[col].textContent.trim()) - parseInt(b.cells[col].textContent.trim())
          })
        }
        else {
          tr = tr.sort(function (a, b) { //sort text columns
            return reverse * (a.cells[col].textContent.trim().localeCompare(b.cells[col].textContent.trim()))
          })
        }
        for(i = 0; i < tr.length; ++i){
            tb.appendChild(tr[i])
        }
    }
};
//----------------------------------------------------------------------------------------------------------------------------------------

const territories_p = new Promise((resolve, reject) => {
  let territory_obj = {}
  window.unsafeWindow.territories_shapes.forEach((territory) => {
    territory_obj[territory[4]] = territory[9]
  })
  resolve(territory_obj)
})

const racket_info_HTML = `
<div id="x_map_info">
  <table>
    <thead>
      <tr>
        <th>#</th>
        <th>Type</th>
        <th>Type Name</th>
        <th>Level</th>
        <th>Description</th>
        <th>Territory ID</th>
      </tr>
    </thead>
    <tbody>
    </tbody>
  </table>
</div>
`

const mapdata_p = new Promise((resolve, reject) => {
  $( document ).ajaxComplete(function(event, jqXHR, ajaxObj) {
    if (ajaxObj.url && ajaxObj.url.includes('step=mapData')) {
      resolve(JSON.parse(jqXHR.responseText))
    }
  })
})

Promise.all([territories_p, mapdata_p]).then(([territories, mapdata]) => {
  console.log(territories, mapdata)
  document.querySelector('div.content').insertAdjacentHTML('beforebegin', `<div id="x_button_right">Copy Rackets (${mapdata.rackets.length})</div>`)
  document.querySelector('#mainContainer .content-wrapper').insertAdjacentHTML('afterend', racket_info_HTML)
  const table_body = document.querySelector('#x_map_info tbody')
  row_num = 0
  mapdata.rackets.forEach((r) => {
    row_num += 1
    table_body.insertAdjacentHTML('beforeend', `
      <tr>
        <td>${row_num}</td>
        <td>${r.type}</td>
        <td>${r.typeName}</td>
        <td>${r.level}</td>
        <td>${r.description}</td>
        <td><a href="https://www.torn.com/city.php#terrName=${territories[r.territoryID]}">${territories[r.territoryID]}</a></td>
      </tr>
    `)
  })
  TableSorter.makeSortable(document.querySelector('#x_map_info table'))
  document.querySelector('#x_button_right').addEventListener('click', (e) => {
    let csv = 'type\ttypeName\tlevel\tname\tdescription\tterritoryID\n'
    mapdata.rackets.forEach(r => csv += r.type+'\t'+r.typeName+'\t'+r.level+'\t'+r.name+'\t'+r.description+'\t'+r.territoryID+'\n')
    GM_setClipboard(csv)
  })
})