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