mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 00:13:12 +09:00
Clean up web interface storage tab.
- Use diffing algorithm so you can select text and images.
- This uses textcontent, plugs a vulnerability where you could inject arbitrary javascript if you could upload a modded item with html in the name, eg, "iron-plate<img src=\"\" onerror=\"console.log('hi')\">".
- Fall back to transparent png if item image not found.
- This stops the flashing box when the image does not exist.
- (internal) Simplify `sortByKey(array, key)`.
This commit is contained in:
1 parent
d01914f7b2
commit
bdf4225dec
4 files changed
+31
-14
No files matched your search
@@ -11,6 +11,7 @@
|
|||||||
<h2>Storage</h2>
|
<h2>Storage</h2>
|
||||||
<input type="text" placeholder="search" id="search"></input>
|
<input type="text" placeholder="search" id="search"></input>
|
||||||
<div id="contents">
|
<div id="contents">
|
||||||
|
<table><tbody><!-- populated by js --></tbody></table>
|
||||||
</div>
|
</div>
|
||||||
<canvas id="contentGraph" width="200" height="200"></canvas>
|
<canvas id="contentGraph" width="200" height="200"></canvas>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
+1
-1
@@ -78,7 +78,7 @@ function getImageFromName(name){
|
|||||||
return "https://wiki.factorio.com/images/" + capitalizeFirstLetter(replaceAll(factorioLocale["fluid-name"][name], ' ', '_').toLowerCase()) + ".png";
|
return "https://wiki.factorio.com/images/" + capitalizeFirstLetter(replaceAll(factorioLocale["fluid-name"][name], ' ', '_').toLowerCase()) + ".png";
|
||||||
} else if(factorioLocale["equipment-name"] && factorioLocale["equipment-name"][name]){
|
} else if(factorioLocale["equipment-name"] && factorioLocale["equipment-name"][name]){
|
||||||
return "https://wiki.factorio.com/images/" + capitalizeFirstLetter(replaceAll(factorioLocale["equipment-name"][name], ' ', '_').toLowerCase()) + ".png";
|
return "https://wiki.factorio.com/images/" + capitalizeFirstLetter(replaceAll(factorioLocale["equipment-name"][name], ' ', '_').toLowerCase()) + ".png";
|
||||||
} else return "";
|
} else return "/pictures/unknown-item.png";
|
||||||
}
|
}
|
||||||
window.factorioLocale = {};
|
window.factorioLocale = {};
|
||||||
$.getJSON("/api/getFactorioLocale", (locale) => {
|
$.getJSON("/api/getFactorioLocale", (locale) => {
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 126 B |
+29
-13
@@ -1,16 +1,33 @@
|
|||||||
// function to draw data we recieve from ajax requests
|
// function to draw data we recieve from ajax requests
|
||||||
function drawcontents(data) {
|
function drawcontents(data) {
|
||||||
data = sortByKey(data, "count");
|
const table = document.querySelector("#contents tbody"); //tables have tbody inserted automatically
|
||||||
let result = "<table>";
|
const rows = table.children;
|
||||||
for(i = 0;i < data.length; i++) {
|
|
||||||
var img = getImageFromName(data[i].name);
|
sortByKey(data, "count");
|
||||||
var searchField = document.querySelector("#search");
|
data.forEach(function(item, i) {
|
||||||
if(!searchField.value || data[i].name.includes(searchField.value)) {
|
let row = rows[i];
|
||||||
result = result + "<tr><td><image src='" + img + "' onerror='hideThis(this);'></td><td>" + data[i].name + "</td><td>" + data[i].count + "</td></tr>";
|
if(!row) {
|
||||||
|
row = document.createElement('tr');
|
||||||
|
row.innerHTML = "<td><img width=32 height=32></td><td class=name></td><td class=count></td>";
|
||||||
|
table.appendChild(row);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
result = result + "</table>"
|
const img = row.querySelector('img');
|
||||||
document.getElementById("contents").innerHTML = result;
|
const imgName = getImageFromName(item.name);
|
||||||
|
if(img.getAttribute('src') !== imgName) {
|
||||||
|
img.setAttribute('src',imgName);
|
||||||
|
}
|
||||||
|
|
||||||
|
const name = row.querySelector('.name');
|
||||||
|
if(name.textContent !== item.name) {
|
||||||
|
name.textContent = item.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
const count = row.querySelector('.count');
|
||||||
|
if(count.textContent !== ''+item.count) {
|
||||||
|
count.textContent = item.count;
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// get cluster inventory from master
|
// get cluster inventory from master
|
||||||
@@ -33,8 +50,7 @@ if(JSON.parse(localStorage.settings)["Periodically update storage screen"]) {
|
|||||||
|
|
||||||
// function to sort arrays of objects after a keys value
|
// function to sort arrays of objects after a keys value
|
||||||
function sortByKey(array, key) {
|
function sortByKey(array, key) {
|
||||||
return array.sort(function(a, b) {
|
array.sort(function(a, b) {
|
||||||
var x = Number(a[key]); var y = Number(b[key]);
|
return b[key] - a[key];
|
||||||
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user