您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Add a download button at the top of every html table to download / export it as a CSV (comma seperated values) file
// ==UserScript== // @name Download Table as CSV // @namespace Violentmonkey Scripts // @match *://*/* // @grant none // @version 1.9 // @author igorlogius // @description Add a download button at the top of every html table to download / export it as a CSV (comma seperated values) file // ==/UserScript== (function(){ // simulate click event function simulateClick(elem) { var evt = new MouseEvent('click', { bubbles: true, cancelable: true, view: window }); var canceled = !elem.dispatchEvent(evt); } // get closest table parent function getTableParent(node){ while ( node = node.parentNode, node !== null && node.tagName !== 'TABLE' ); return node; } // assemble csv data function getTblData(tbl){ // csv store var csv = []; // get all rows inside the table tbl.querySelectorAll('tr').forEach(function(trRow) { // Only process direct tr children if( ! tbl.isEqualNode(getTableParent(trRow))){ return; } // assemble row content var row = []; trRow.querySelectorAll('td, th').forEach(function(col) { // remove multiple spaces and linebreaks (breaks csv) var data = col.innerText.replace(/(\r\n|\n|\r)/gm, '').replace(/(\s\s)/gm, ' ') // escape double-quote with double-double-quote data = data.replace(/"/g, '""'); row.push('"' + data + '"'); }); csv.push(row.join(',')); }); return csv.join('\n'); } // add button + click action function add_btn(tbl){ var btn = document.createElement('button'); btn.innerHTML = 'Download Table as CSV'; btn.setAttribute('type', 'button'); // Process Table on Click btn.onclick = function() { var csv_string = getTblData(tbl); csvlink.setAttribute('href', 'data:text/csv;charset=utf-8,' + encodeURIComponent(csv_string)); simulateClick(csvlink); }; // Insert before Table tbl.parentNode.insertBefore(btn,tbl); } /* * * M A I N * */ // add link var csvlink = document.createElement('a'); csvlink.style.display = 'none'; csvlink.setAttribute('target', '_blank'); csvlink.setAttribute('download', 'data.csv'); document.body.append(csvlink); // add buttons document.querySelectorAll('table').forEach(function(tbl){add_btn(tbl)}); }());
QingJ © 2025
镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址