ManifestCSV

Convert Hermes manifest to CSV

  1. // ==UserScript==
  2. // @name ManifestCSV
  3. // @description Convert Hermes manifest to CSV
  4. // @include https://courierportal.hermescloud.co.uk/*
  5. // @grant none
  6. // @version 0.4
  7. // @namespace https://greasyfork.org/users/2391
  8. // ==/UserScript==
  9.  
  10. /* jshint esversion: 6 */
  11.  
  12. const AddCSV =
  13. {
  14. csvLines: [],
  15. scrollY: -1,
  16. scrollAttempts: 0,
  17. overlay: null,
  18. createOverlay: function()
  19. {
  20. AddCSV.overlay = document.createElement('div');
  21. AddCSV.overlay.id = 'AddCSV_Overlay';
  22.  
  23. AddCSV.overlay.style.position = "fixed";
  24. AddCSV.overlay.style.display = "block";
  25. AddCSV.overlay.style.width = "100%";
  26. AddCSV.overlay.style.height = "100%";
  27. AddCSV.overlay.style.top = 0;
  28. AddCSV.overlay.style.left = 0;
  29. AddCSV.overlay.style.right = 0;
  30. AddCSV.overlay.style.bottom = 0;
  31. AddCSV.overlay.style.backgroundColor = "rgba(0,0,0,0.5)";
  32. AddCSV.overlay.style.zIndex = 2;
  33. AddCSV.overlay.innerHTML = '<p style="color: yellow; font-size: 64px">Loading manifest, please wait...</p>';
  34. document.body.appendChild(AddCSV.overlay);
  35. },
  36. init: function()
  37. {
  38. if
  39. (
  40. (document.getElementById('manifest-summary-button') != null) &&
  41. (document.getElementsByName('round-id').length != 0) &&
  42. (document.getElementsByName('manifest').length != 0)
  43. )
  44. {
  45. var tBtn = document.createElement('i');
  46. tBtn.className = "btn btn-primary";
  47. tBtn.id = "saveAsCSV";
  48. tBtn.innerText = "Save as CSV";
  49. document.getElementsByClassName('card__body')[0].append(tBtn);
  50. tBtn.addEventListener("click", AddCSV.loadManifest);
  51. }
  52. else
  53. {
  54. window.setTimeout(AddCSV.init, 500);
  55. }
  56. },
  57. loadManifest: function()
  58. {
  59. AddCSV.createOverlay();
  60. AddCSV.scrollY = -1;
  61. AddCSV.scrollAttempts = 0;
  62. AddCSV.scrollToEnd();
  63. return false;
  64. },
  65. scrollToEnd: function()
  66. {
  67. // Reset the scroll attempts counter if we were able to scroll further down the page last time around,
  68. // OR if we're at the end but the "loading more data" wibbly dots are being displayed...
  69. if((window.scrollY != AddCSV.scrollY) || (document.getElementsByClassName('loading-dots').length != 0))
  70. {
  71. AddCSV.scrollY = window.scrollY;
  72. AddCSV.scrollAttempts = 0;
  73. }
  74. if(++AddCSV.scrollAttempts < 3)
  75. {
  76. window.scrollBy(0, 1000);
  77. window.setTimeout(AddCSV.scrollToEnd, 100);
  78. return;
  79. }
  80. window.scrollTo(0,0);
  81. AddCSV.process();
  82. },
  83. getFormattedType: function(rawType)
  84. {
  85. var retval = '';
  86. if(rawType == 'CL')
  87. {
  88. retval += 'COL, Collection';
  89. }
  90. else
  91. {
  92. retval += 'DEL, ';
  93. if(rawType == '01') retval += 'Packet/C2C Small';
  94. else if(rawType == '02') retval += 'Standard';
  95. else if(rawType == '03') retval += 'Heavy/Large';
  96. else if(rawType == '04') retval += 'Heavy';
  97. else if(rawType == '05') retval += 'Hanging';
  98. else if(rawType == '06') retval += 'Small';
  99. else if(rawType == '07') retval += 'Medium';
  100. else if(rawType == '08') retval += 'Postable';
  101. else retval += 'Unknown';
  102. }
  103. retval += ' ('+rawType+')';
  104. return retval;
  105. },
  106. saveToFile: function()
  107. {
  108. const a = document.createElement('a');
  109. var outData = '';
  110.  
  111. var currentRoundIdx = document.getElementsByName('round-id')[0].selectedIndex;
  112. var currentManifestIdx = document.getElementsByName('manifest')[0].selectedIndex;
  113.  
  114. var filename = 'manifest ';
  115. filename += document.getElementsByName('round-id')[0].options[currentRoundIdx].innerText.trim();
  116. filename += ' ';
  117. filename += document.getElementsByName('manifest')[0].options[currentManifestIdx].innerText.trim();
  118. filename += '.csv';
  119.  
  120. for(var i =0; i < AddCSV.csvLines.length; ++i)
  121. {
  122. outData += AddCSV.csvLines[i] + '\r\n';
  123. }
  124. const file = new Blob([outData], {type: 'text/plain'});
  125. a.href= URL.createObjectURL(file);
  126. a.download = filename;
  127. a.click();
  128. URL.revokeObjectURL(a.href);
  129. },
  130. process: function()
  131. {
  132. AddCSV.overlay.style.display = "none";
  133. var entries = document.getElementsByClassName('data-row').length;
  134. if(entries > 0)
  135. {
  136. AddCSV.csvLines = [];
  137. var tLine = '';
  138. for(var i = 0; i < entries; ++i)
  139. {
  140. var tRowObj = document.getElementsByClassName('data-row')[i];
  141. var tRowBits = tRowObj.getElementsByTagName('li');
  142.  
  143. tLine = '\t';
  144.  
  145. // fix for the new "remanifested" barcode entries
  146. var tBarcode = tRowBits[0].innerText;
  147. tBarcode = tBarcode.split('\n');
  148. tLine += tBarcode[1];
  149. if(tBarcode[0].indexOf('Remanifested') != -1)
  150. {
  151. tLine += ' (R)';
  152. }
  153. tLine += ',';
  154.  
  155. tLine += tRowBits[1].getElementsByClassName('data-row-item__value')[0].innerText + ',';
  156. tLine += tRowBits[2].getElementsByClassName('data-row-item__value')[0].innerText + ',';
  157. var tService = tRowBits[3].getElementsByClassName('data-row-item__value')[0].innerText;
  158. tService = tService.replace(',','/');
  159. tLine += tService + ',';
  160. var tType = tRowBits[4].getElementsByClassName('data-row-item__value')[0].innerText;
  161. tLine += tType; //AddCSV.getFormattedType(tType);
  162. AddCSV.csvLines.push(tLine);
  163. }
  164.  
  165. AddCSV.saveToFile();
  166. }
  167. }
  168. };
  169.  
  170. AddCSV.init();