Place Browser Table Export

Exports WME Place Browser's table output to CSV and download to local disk

目前為 2019-10-04 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Place Browser Table Export
  3. // @namespace http://junyianl.net/
  4. // @version 2019.10.04.04
  5. // @description Exports WME Place Browser's table output to CSV and download to local disk
  6. // @author junyianl
  7. // @match https://w-tools.org/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. /*
  12. Changelogs:
  13. 2019.10.04.04
  14. - Bugfix: Change separator for headers.
  15. 2019.10.04.03
  16. - Bugfix: Separators were not changed.
  17. 2019.10.04.02
  18. - Changed PL column to actual permalink.
  19. - Using tab separator instead of comma, for easier copy-and-pasting into Google spreadsheet.
  20. */
  21.  
  22. (function() {
  23. 'use strict';
  24.  
  25. // Your code here...
  26.  
  27. function downloadCSV(csv, filename) {
  28. var csvFile;
  29. var downloadLink;
  30.  
  31. // CSV file
  32. csvFile = new Blob([csv], {type: "text/csv"});
  33.  
  34. // Download link
  35. downloadLink = document.createElement("a");
  36.  
  37. // File name
  38. downloadLink.download = filename;
  39.  
  40. // Create a link to the file
  41. downloadLink.href = window.URL.createObjectURL(csvFile);
  42.  
  43. // Hide download link
  44. // downloadLink.style.display = "none";
  45.  
  46. // Add the link to DOM
  47. document.body.appendChild(downloadLink);
  48.  
  49. // Click download link
  50. downloadLink.click();
  51. }
  52.  
  53. function exportTableToCSV(filename) {
  54. var csv = [];
  55. var table = document.querySelectorAll("table.tablesorter");
  56.  
  57. // Populate column headers
  58. var row = [], cols = table[0].querySelectorAll("th");
  59. for (var i = 0; i < cols.length; i++) {
  60. row.push('"' + cols[i].innerText + '"');
  61. }
  62. csv.push(row.join("\t"));
  63.  
  64. // Populate places
  65. var places = table[0].querySelectorAll("[class*=validate]");
  66. for (var j = 0; j < places.length; j++) {
  67. var placerow = [], placecol = places[j].querySelectorAll("td");
  68. for (var k = 0; k < placecol.length; k++) {
  69. if (k === 0) {
  70. var pl = placecol[k].querySelector("a");
  71. placerow.push('"' + pl.href + '"' );
  72. }
  73. else if (k === 2) {
  74. var issue = [], issues = placecol[k].querySelectorAll("li");
  75. for (var l = 0; l < issues.length; l++) {
  76. issue.push(issues[l].innerText);
  77. }
  78. placerow.push('"' + issue.join("|") + '"');
  79. }
  80. else {
  81. placerow.push('"' + placecol[k].innerText.trim() + '"');
  82. }
  83. }
  84. csv.push(placerow.join("\t"));
  85. }
  86.  
  87. // Download CSV file
  88. downloadCSV(csv.join("\r\n"), filename);
  89. }
  90.  
  91. var group = document.querySelector('[id=MainContent_ddlAreaGroup]').selectedOptions[0].value;
  92. var area = document.querySelector('[id=MainContent_DropDownList1]').selectedOptions[0].innerText;
  93. var date = new Date();
  94. exportTableToCSV(group + '_' + area + '_' + date.getFullYear() + '-' + (date.getMonth()+1) + '-' + date.getDate() + '.csv');
  95.  
  96. })();