Place Browser Table Export

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

当前为 2019-10-04 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Place Browser Table Export
// @namespace    http://junyianl.net/
// @version      2019.10.04.02
// @description  Exports WME Place Browser's table output to CSV and download to local disk
// @author       junyianl
// @match        https://w-tools.org/*
// @grant        none
// ==/UserScript==

/*
Changelogs:
2019.10.04.02
- Changed PL column to actual permalink
- Using tab separator instead of comma, for easier copy-and-pasting into Google spreadsheet
*/

(function() {
    'use strict';

    // Your code here...

    function downloadCSV(csv, filename) {
        var csvFile;
        var downloadLink;

        // CSV file
        csvFile = new Blob([csv], {type: "text/csv"});

        // Download link
        downloadLink = document.createElement("a");

        // File name
        downloadLink.download = filename;

        // Create a link to the file
        downloadLink.href = window.URL.createObjectURL(csvFile);

        // Hide download link
//         downloadLink.style.display = "none";

        // Add the link to DOM
        document.body.appendChild(downloadLink);

        // Click download link
        downloadLink.click();
    }

    function exportTableToCSV(filename) {
        var csv = [];
        var table = document.querySelectorAll("table.tablesorter");

        // Populate column headers
        var row = [], cols = table[0].querySelectorAll("th");
        for (var i = 0; i < cols.length; i++) {
            row.push('"' + cols[i].innerText + '"');
        }
        csv.push(row.join(","));

        // Populate places
        var places = table[0].querySelectorAll("[class*=validate]");
        for (var j = 0; j < places.length; j++) {
            var placerow = [], placecol = places[j].querySelectorAll("td");
            for (var k = 0; k < placecol.length; k++) {
                if (k == 0) {
                    var pl = placecol[k].querySelector("a");
                    placerow.push('"' + pl.href + '"' );
                }
                else if (k == 2) {
                    var issue = [], issues = placecol[k].querySelectorAll("li");
                    for (var l = 0; l < issues.length; l++) {
                        issue.push(issues[l].innerText);
                    }
                    placerow.push('"' + issue.join("|") + '"');
                }
                else {
                    placerow.push('"' + placecol[k].innerText.trim() + '"');
                }
            }
            csv.push(placerow.join(","));
        }

        // Download CSV file
        downloadCSV(csv.join("\r\n"), filename);
    }

    var group = document.querySelector('[id=MainContent_ddlAreaGroup]').selectedOptions[0].value;
    var area = document.querySelector('[id=MainContent_DropDownList1]').selectedOptions[0].innerText;
    var date = new Date();
    exportTableToCSV(group + '_' + area + '_' + date.getFullYear() + '-' + (date.getMonth()+1) + '-' + date.getDate() + '.csv');

})();