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