表格下载工具

功能介绍:1、下载网页中出现的表格到本地

目前為 2021-09-06 提交的版本,檢視 最新版本

// ==UserScript==
// @name         表格下载工具
// @version      0.0.3
// @description  功能介绍:1、下载网页中出现的表格到本地
// @author       SvenJiA
// @match      *://zh.wikipedia.org/*
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/xlsx.min.js
// @license      MIT
// @antifeature  referral-link 此提示为GreasyFork代码规范要求含有查券功能的脚本必须添加,实际使用无任何强制跳转,代码可查,请知悉。
// @namespace https://greasyfork.org/users/812577
// ==/UserScript==

window.onload = function(){
  var btn  = document.createElement('button');
  btn.id = '_download_excel';
  btn.innerHTML = '下载表格'
  appendStyle();
  document.body.appendChild(btn);
    var tables = document.getElementsByTagName('table')
    var tableArr = Array.from(tables)
    tableArr = tableArr.map((item,index)=>{
      return {
        table:item,
        name:index
      }
    })
    var dowloadBtn = document.getElementById('_download_excel')
    dowloadBtn.onclick = function(){
      exportExcel(tableArr,'excel')
    }
}
const exportExcel = (exportArr, xlsxName) => {
  let wb = XLSX.utils.book_new();
  exportArr.map(val => {
    let ws = XLSX.utils.table_to_sheet(val.table);
    XLSX.utils.book_append_sheet(wb, ws, val.name);
  })
  XLSX.writeFile(wb, xlsxName + ".xlsx");
};

function appendStyle(){
  var style = document.createElement("style");
  style.type = "text/css";
  style.appendChild(document.createTextNode(`button {
  position:fixed;
  right:50px;
  bottom:50px;
  background-color: #f22;
  outline: none;
  border: none;
  border-radius: 50%;
  width: 50px;
  height: 50px;
  color: #fff;
  z-index:999999999;
}
button:hover{
  cursor: pointer;
  color: #ff2;
  background-color: #F00;
}`));



var head = document.getElementsByTagName("head")[0];

head.appendChild(style);
}