Google Search 结果导出

OutPut Data from Google Search Result

  1. // ==UserScript==
  2. // @name Google Search 结果导出
  3. // @namespace http://www.ixiqin.com/
  4. // @version 0.4
  5. // @description OutPut Data from Google Search Result
  6. // @author Bestony
  7. // @match https://www.google.com/search*
  8. // @require http://code.jquery.com/jquery-3.2.1.min.js
  9. // @license . GPLv2
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. /**
  14. * Action
  15. */
  16. importCss();
  17. scriptWithJquery();
  18.  
  19. /**
  20. * Get Result
  21. */
  22. var searchResult = $("h3.r");
  23. var contentArray = [];
  24. searchResult.each(function () {
  25. contentArray.push({
  26. "title": this.firstElementChild.text.toString().replace(new RegExp(',',"g"),"·"),
  27. "herf": this.firstElementChild.href.toString(),
  28. });
  29. });
  30.  
  31. /**
  32. * Import CSS For Button
  33. */
  34. function importCss() {
  35. var jqueryScriptBlock = document.createElement('style');
  36. jqueryScriptBlock.type = 'text/css';
  37. jqueryScriptBlock.innerHTML = "#gototop{position:fixed;bottom:60%;left:10px;border:1px solid gray;padding:3px;width:90px;font-size:12px;cursor:pointer;border-radius: 3px;text-shadow: 1px 1px 3px #676767;}";
  38. document.getElementsByTagName('head')[0].appendChild(jqueryScriptBlock);
  39. }
  40. /**
  41. * 页面元素添加
  42. */
  43. function scriptWithJquery() {
  44. $(document.body).append("<div id='gototop' title=''>下 载 搜 索 结 果</div>");
  45. $('#gototop').click(function () { downloadCSV({ filename: "data.csv" }); });
  46. }
  47. /**
  48. * 将 array 转换为 CSV 格式
  49. * @param args array
  50. */
  51. function convertArrayOfObjectsToCSV(args) {
  52. var result, ctr, keys, columnDelimiter, lineDelimiter, data;
  53.  
  54. data = args.data || null;
  55. if (data === null || !data.length) {
  56. return null;
  57. }
  58.  
  59. columnDelimiter = args.columnDelimiter || ',';
  60. lineDelimiter = args.lineDelimiter || '\n';
  61.  
  62. keys = Object.keys(data[0]);
  63.  
  64. result = '';
  65. result += keys.join(columnDelimiter);
  66. result += lineDelimiter;
  67.  
  68. data.forEach(function (item) {
  69. ctr = 0;
  70. keys.forEach(function (key) {
  71. if (ctr > 0) result += columnDelimiter;
  72.  
  73. result += item[key];
  74. ctr++;
  75. });
  76. result += lineDelimiter;
  77. });
  78.  
  79. return result;
  80. }
  81. /**
  82. * 下载 CSV 文件
  83. * @param args filename
  84. */
  85. function downloadCSV(args) {
  86. var data, filename, link;
  87. var csv = convertArrayOfObjectsToCSV({
  88. data: contentArray
  89. });
  90. if (csv === null) return;
  91.  
  92. filename = args.filename || 'export.csv';
  93.  
  94. if (!csv.match(/^data:text\/csv/i)) {
  95. csv = 'data:text/csv;charset=utf-8,' + csv;
  96. }
  97. data = encodeURI(csv);
  98.  
  99. link = document.createElement('a');
  100. link.setAttribute('href', data);
  101. link.setAttribute('download', filename);
  102. link.click();
  103. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址