Convert Hermes manifest to CSV
目前為
// ==UserScript==
// @name ManifestCSV
// @description Convert Hermes manifest to CSV
// @include https://courierportal.hermescloud.co.uk/*
// @grant none
// @version 0.2
// @namespace https://greasyfork.org/users/2391
// ==/UserScript==
/* jshint esversion: 6 */
const AddCSV =
{
csvLines: [],
scrollY: -1,
scrollAttempts: 0,
overlay: null,
createOverlay: function()
{
AddCSV.overlay = document.createElement('div');
AddCSV.overlay.id = 'AddCSV_Overlay';
AddCSV.overlay.style.position = "fixed";
AddCSV.overlay.style.display = "block";
AddCSV.overlay.style.width = "100%";
AddCSV.overlay.style.height = "100%";
AddCSV.overlay.style.top = 0;
AddCSV.overlay.style.left = 0;
AddCSV.overlay.style.right = 0;
AddCSV.overlay.style.bottom = 0;
AddCSV.overlay.style.backgroundColor = "rgba(0,0,0,0.5)";
AddCSV.overlay.style.zIndex = 2;
AddCSV.overlay.innerHTML = '<p style="color: yellow; font-size: 64px">Loading manifest, please wait...</p>';
document.body.appendChild(AddCSV.overlay);
},
init: function()
{
if
(
(document.getElementById('manifest-summary-button') != null) &&
(document.getElementsByName('round-id').length != 0) &&
(document.getElementsByName('manifest').length != 0)
)
{
tBtn = document.createElement('i');
tBtn.className = "btn btn-primary";
tBtn.id = "saveAsCSV";
tBtn.innerText = "Save as CSV";
document.getElementsByClassName('card__body')[0].append(tBtn);
tBtn.addEventListener("click", AddCSV.loadManifest);
}
else
{
window.setTimeout(AddCSV.init, 500);
}
},
loadManifest: function()
{
AddCSV.createOverlay();
AddCSV.scrollY = -1;
AddCSV.scrollAttempts = 0;
AddCSV.scrollToEnd();
return false;
},
scrollToEnd: function()
{
if(window.scrollY != AddCSV.scrollY)
{
AddCSV.scrollY = window.scrollY;
AddCSV.scrollAttempts = 0;
}
if(++AddCSV.scrollAttempts < 3)
{
window.scrollBy(0, 1000);
window.setTimeout(AddCSV.scrollToEnd, 1000);
return;
}
window.scrollTo(0,0);
AddCSV.process();
},
getFormattedType: function(rawType)
{
retval = '';
if(rawType == 'CL')
{
retval += 'COL, Collection';
}
else
{
retval += 'DEL, ';
if(rawType == '01') retval += 'Packet/C2C Small';
else if(rawType == '02') retval += 'Standard';
else if(rawType == '03') retval += 'Heavy/Large';
else if(rawType == '04') retval += 'Heavy';
else if(rawType == '05') retval += 'Hanging';
else if(rawType == '06') retval += 'Small';
else if(rawType == '07') retval += 'Medium';
else if(rawType == '08') retval += 'Postable';
else retval += 'Unknown';
}
retval += ' ('+tType+')';
return retval;
},
saveToFile: function()
{
const a = document.createElement('a');
var outData = '';
var currentRoundIdx = document.getElementsByName('round-id')[0].selectedIndex;
var currentManifestIdx = document.getElementsByName('manifest')[0].selectedIndex;
var filename = 'manifest ';
filename += document.getElementsByName('round-id')[0].options[currentRoundIdx].innerText.trim();
filename += ' ';
filename += document.getElementsByName('manifest')[0].options[currentManifestIdx].innerText.trim();
filename += '.csv';
for(var i =0; i < AddCSV.csvLines.length; ++i)
{
outData += AddCSV.csvLines[i] + '\r\n';
}
const file = new Blob([outData], {type: 'text/plain'});
a.href= URL.createObjectURL(file);
a.download = filename;
a.click();
URL.revokeObjectURL(a.href);
},
process: function()
{
AddCSV.overlay.style.display = "none";
var entries = document.getElementsByClassName('data-row').length;
if(entries > 0)
{
AddCSV.csvLines = [];
var tLine = '';
for(var i = 0; i < entries; ++i)
{
tRowObj = document.getElementsByClassName('data-row')[i];
tLine = '\t' + tRowObj.getElementsByClassName('barcode')[0].getElementsByClassName('data-row-item__value')[0].innerText + ',';
tLine += tRowObj.getElementsByClassName('address')[0].getElementsByClassName('data-row-item__value')[0].innerText + ',';
tLine += tRowObj.getElementsByClassName('client')[0].getElementsByClassName('data-row-item__value')[0].innerText + ',';
tService = tRowObj.getElementsByClassName('service-offers')[0].getElementsByClassName('data-row-item__value')[0].innerText;
tService = tService.replace(',','/');
tLine += tService + ',';
tType = tRowObj.getElementsByClassName('type')[0].getElementsByClassName('data-row-item__value')[0].innerText;
tLine += tType; //AddCSV.getFormattedType(tType);
AddCSV.csvLines.push(tLine);
}
AddCSV.saveToFile();
}
}
};
AddCSV.init();