Infinity 导出 Excel

Infinity Pro 网站版,将您保存的网站导出为一个 Excel 文件。导出前需要登录账号,如果导出的网站为空,请先刷新界面。

  1. // ==UserScript==
  2. // @name Infinity 导出 Excel
  3. // @version 1.0.5
  4. // @icon https://inftab.com/icon/favicon.ico
  5. // @description Infinity Pro 网站版,将您保存的网站导出为一个 Excel 文件。导出前需要登录账号,如果导出的网站为空,请先刷新界面。
  6. // @author 曦源 <pinkones@qq.com>
  7. // @match *://inftab.com/*
  8. // @match *://www.inftab.com/*
  9. // @grant none
  10. // @run-at document-end
  11. // @namespace 项目地址github
  12. // @supportURL https://github.com/SunBrook/infinity_tool_scripts
  13. // @homepageURL https://github.com/SunBrook/infinity_tool_scripts
  14. // ==/UserScript==
  15.  
  16.  
  17. 'use strict';
  18.  
  19. function init(){
  20. // 目标位置
  21. var backup_btn_box = document.querySelector("side-profile")
  22. .shadowRoot.querySelector("side-user")
  23. .shadowRoot.querySelector("infinito-card.backup-recovery .backup-btn-box");
  24.  
  25. // 创建 Excel 导出页面元素
  26. var export_excel_node = document.createElement("div");
  27. export_excel_node.className = "backup-btn-card";
  28. export_excel_node.innerHTML = ''+
  29. '<div class="backup-btn-content" id="ExportExcel">'+
  30. '<div class="backup-btn">'+
  31. '<div class="backup-btn-title">'+
  32. '<span class="backup-btn-title-text"><!---->导出 Excel 文件<!----></span>'+
  33. '<i-svg></i-svg>'+
  34. '</div>'+
  35. '<span class="backup-btn-desc"><!---->从当前本地数据导出<!----></span>'+
  36. '</div>'+
  37. '</div>';
  38.  
  39. // 创建完成后,然后在设置图标
  40. var createFirst = new Promise(function (resolve, reject){
  41. backup_btn_box.append(export_excel_node);
  42. resolve("finish");
  43. });
  44.  
  45. createFirst.then(function(message){
  46. // 设置导出图标样式
  47. var ico_svg = backup_btn_box.querySelector(".backup-btn-card:nth-child(4) .backup-btn-title i-svg")
  48. .shadowRoot.querySelector(".svg");
  49. ico_svg.style.webkitMaskImage = "url(/images/arrow-right.d51ffb7.svg)";
  50. });
  51.  
  52. backup_btn_box.querySelector(".backup-btn-card:nth-child(4) div#ExportExcel").onclick = function(){
  53. GetWebInfoV2();
  54. }
  55. }
  56.  
  57. setTimeout(init, 3000);
  58.  
  59.  
  60. //网站数量
  61. var tb_count = 0;
  62.  
  63. //获取用户的网站信息
  64. function GetWebInfoV2() {
  65. //表头
  66. var tableHtml = '<div id="z_wrapper" style="z-index: 999999;position: absolute;background-color: white;width: 1000px;height: 500px;margin-left: 150px;margin-top:50px;overflow-y: scroll;padding:8px;filter:alpha(Opacity=80);-moz-opacity:0.5;opacity: 0.8;border-radius: 15px;">' +
  67. '<table id="z_table" style="border-collapse: collapse;text-align:center;">' +
  68. '<tr>' +
  69. '<th style="border: 1px solid #ccc;padding:5px;width:500px;height:40px;">名称</th>' +
  70. '<th style="border: 1px solid #ccc;padding:5px;width:500px;height:40px;">网址</th>' +
  71. '<th style="border: 1px solid #ccc;padding:5px;width:100px;height:40px;">分类</th>' +
  72. '</tr>';
  73.  
  74. //获取 window.localStorage
  75. var storage = window.localStorage;
  76. if (!storage) {
  77. alert("浏览器不支持LocalStorage");
  78. return;
  79. }
  80.  
  81. //用户收藏的网站json字符串
  82. var store_site = storage["store-site"];
  83. if (!store_site) {
  84. alert("导出失败,请刷新页面重试");
  85. return;
  86. }
  87.  
  88. //字符串 转 ToJson对象
  89. var u_list = JSON.parse(store_site).sites;
  90.  
  91. //遍历 - 每页
  92. for (let i = 0; i < u_list.length; i++) {
  93. var page_list = u_list[i]; //某一页
  94. for (let j = 0; j < page_list.length; j++) {
  95. var unit_list = page_list[j]; //某个单元格
  96. var image_type = unit_list.bgType;
  97. //判断是 单个网站 还是文件夹 image / folder
  98. if (image_type == "image" || image_type == "color") {
  99. //单个网站 纯色
  100. //网站
  101. var url = unit_list.target;
  102. //过滤 infinity:// 开头的官方应用
  103. var fdStart = url.indexOf("infinity://");
  104. if (fdStart == 0) continue;
  105. //名称
  106. var name = unit_list.name;
  107. tableHtml += '<tr>' +
  108. '<td style="border: 1px solid #ccc;padding:5px;height:25px;" align="center">' + name + '</td>' +
  109. '<td style="border: 1px solid #ccc;padding:5px;height:25px;"><a href="' + url + '" target="_blank">' + url + '</a></td>' +
  110. '<td style="border: 1px solid #ccc;padding:5px;height:25px;" align="center"></td>' +
  111. '</tr>';
  112. tb_count++;
  113. } else if (image_type == undefined) {
  114. //文件夹
  115. //文件夹名称
  116. var folder_name = unit_list.name;
  117. //文件夹子项集合
  118. var folder_list = unit_list.children;
  119. //只要用户自定义的网站集合
  120. var item_user_disposed = new Array();
  121. for (let k = 0; k < folder_list.length; k++) {
  122. var url = folder_list[k].target;
  123. var fdStart = url.indexOf("infinity://");
  124. if (fdStart == 0) continue;
  125. item_user_disposed.push({ url: url, name: folder_list[k].name });
  126. }
  127. //组成table
  128. if (!item_user_disposed.length) continue;
  129. for (let m = 0; m < item_user_disposed.length; m++) {
  130. if (m == 0) {
  131. tableHtml += '<tr>' +
  132. '<td style="border: 1px solid #ccc;padding:5px;height:25px;" align="center">' + item_user_disposed[m].name + '</td>' +
  133. '<td style="border: 1px solid #ccc;padding:5px;height:25px;"><a href="' + item_user_disposed[m].url + '" target="_blank">' + item_user_disposed[m].url + '</a></td>' +
  134. '<td style="border: 1px solid #ccc;padding:5px;height:25px;" align="center" rowspan="' + item_user_disposed.length + '">' + folder_name + '</td>' +
  135. '</tr>';
  136. } else {
  137. tableHtml += '<tr>' +
  138. '<td style="border: 1px solid #ccc;padding:5px;height:25px;" align="center">' + item_user_disposed[m].name + '</td>' +
  139. '<td style="border: 1px solid #ccc;padding:5px;height:25px;"><a href="' + item_user_disposed[m].url + '" target="_blank">' + item_user_disposed[m].url + '</a></td>' +
  140. '</tr>';
  141. }
  142. }
  143. tb_count += item_user_disposed.length;
  144. }
  145. }
  146. }
  147.  
  148. tableHtml += '</table></div><a id="dlink" style="display:none;"></a><input type="button" onclick="tableToExcel(\'tablename\', \'name\', \'myfile.xlsx\')" value="Export to Excel">';
  149. var div_table = document.createElement("div");
  150. div_table.id = 'temp_div';
  151. div_table.innerHTML = tableHtml;
  152. document.body.appendChild(div_table);
  153.  
  154. console.log("网站个数:", tb_count);
  155.  
  156. // tableToExcel('z_table', " 累计 " + tb_count + " 个网站"); //直接下载
  157. var userInfo = storage["store-user"];
  158. if (!userInfo) {
  159. tableToExcel('z_table', "累计 " + tb_count + " 个网站", "InfinityPro " + getCurrentDate(2));
  160. } else {
  161. var user_info = JSON.parse(userInfo).userInfo;
  162. if(user_info.name){
  163. var space = " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  164. tableToExcel('z_table', "用户:" + user_info.name + space + "共收藏 " + tb_count + " 个网站", "InfinityPro " + getCurrentDate(2));
  165. }
  166. else{
  167. tableToExcel('z_table', "累计 " + tb_count + " 个网站", "InfinityPro " + getCurrentDate(2));
  168. }
  169. }
  170.  
  171. //销毁table
  172. document.getElementById("temp_div").remove();
  173. }
  174.  
  175. //时间类
  176. function getCurrentDate(format) {
  177. var now = new Date();
  178. var year = now.getFullYear(); //年份
  179. var month = now.getMonth();//月份
  180. var date = now.getDate();//日期
  181. var day = now.getDay();//周几
  182. var hour = now.getHours();//小时
  183. var minu = now.getMinutes();//分钟
  184. var sec = now.getSeconds();//秒
  185. month = month + 1;
  186. if (month < 10) month = "0" + month;
  187. if (date < 10) date = "0" + date;
  188. if (hour < 10) hour = "0" + hour;
  189. if (minu < 10) minu = "0" + minu;
  190. if (sec < 10) sec = "0" + sec;
  191. var time = "";
  192. //精确到天
  193. if (format == 1) {
  194. time = year + "-" + month + "-" + date;
  195. }
  196. //精确到分
  197. else if (format == 2) {
  198. time = year + "/" + month + "/" + date + " " + hour + ":" + minu + ":" + sec;
  199. }
  200. return time;
  201. }
  202.  
  203.  
  204. //导出excel
  205. var tableToExcel = (function () {
  206. var uri = 'data:application/vnd.ms-excel;base64,',
  207. template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><?xml version="1.0" encoding="UTF-8" standalone="yes"?><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
  208. base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) },
  209. format = function (s, c) {
  210. return s.replace(/{(\w+)}/g,
  211. function (m, p) { return c[p]; })
  212. }
  213. // 直接下载
  214. // return function (table, name) {
  215. // if (!table.nodeType) table = document.getElementById(table)
  216. // var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
  217. // window.location.href = uri + base64(format(template, ctx))
  218. // }
  219.  
  220. // 重命名下载文件
  221. return function (table, name, filename) {
  222. if (!table.nodeType) table = document.getElementById(table)
  223. var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
  224.  
  225. document.getElementById("dlink").href = uri + base64(format(template, ctx));
  226. document.getElementById("dlink").download = filename;//这里是关键所在,当点击之后,设置a标签的属性,这样就可以更改标签的标题了
  227. document.getElementById("dlink").click();
  228.  
  229. }
  230. })()