Dune Csv Export

Downloading the queries csv export for free subscription users

目前为 2023-10-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Dune Csv Export
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Downloading the queries csv export for free subscription users
  6. // @author Rv
  7. // @match https://dune.com/queries*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=dune.com
  9. // @grant none
  10. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. function getElementByXpath(path){
  15. return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  16. }
  17.  
  18. var intervalId;
  19.  
  20. function getHeaders(table){
  21. const headers = [];
  22. $.each($(table).find("thead").find("tr").find("th"), function (key, val2) {
  23. headers[key] = $(val2).find("div").text();
  24. });
  25. return headers;
  26. }
  27.  
  28. function getValues(table){
  29. const values = []
  30. $.each($(table).find("tbody").find("tr"), function (key, val) {
  31. const row = [];
  32. $.each($(val).find("td"), function (tkey, tval) {
  33. row[tkey] = $(tval).find("div").first().text();
  34. });
  35. values[key] = row;
  36. });
  37. return values;
  38. }
  39.  
  40. function collectLoop(last_values=[]){
  41. let nextButton = getElementByXpath('//*[@id="results"]/div/div[2]/div/div[2]/ul/li[6]/button');
  42. let table = getElementByXpath('//*[@id="results"]/div/div[2]/div/div[1]/table');
  43. const values = getValues(table);
  44. const newValues = last_values.concat(values);
  45.  
  46. if (!$(nextButton).is(":disabled") && nextButton != null){
  47. $(nextButton).trigger("click");
  48. setTimeout(()=>{
  49. return collectLoop(newValues);
  50. }, 20)
  51. } else {
  52. const headers = getHeaders(table);
  53. download_csv(headers, newValues);
  54. }
  55. }
  56.  
  57.  
  58. function download_csv(headers, rows) {
  59. let csvHeaders = headers.join(",") + "\n";
  60. let csvRows = rows.map(row => row.join(",")).join("\n");
  61.  
  62. var downloadBtn = document.createElement("a");
  63. downloadBtn.href = "data:text/csv;charset=utf-8,"+encodeURI(csvHeaders+csvRows);
  64. downloadBtn.target = "_blank";
  65. let url = window.location.href;
  66. downloadBtn.download = "query_"+url.split("queries/")[1].replace("/", "_")+".csv";
  67. downloadBtn.click();
  68.  
  69. }
  70.  
  71.  
  72. function collectCsv() {
  73.  
  74. let nextButton = getElementByXpath('//*[@id="results"]/div/div[2]/div/div[2]/ul/li[6]/button');
  75. if (nextButton == null){
  76. console.log("Butt not exists");
  77. collectLoop();
  78. } else {
  79. // go to first
  80. let firstPageBtn = getElementByXpath('//*[@id="results"]/div/div[2]/div/div[2]/ul/li[3]/button');
  81. if (firstPageBtn != null && !$(firstPageBtn).is(":disabled")){
  82. $(firstPageBtn).trigger("click");
  83. setTimeout(()=>{
  84. collectLoop();
  85. }, 20)
  86. } else {
  87. collectLoop();
  88. }
  89. }
  90. }
  91.  
  92. function changeCsvButton(){
  93. let csvButton = getElementByXpath('//*[@id="results"]/div/div[1]/div[1]/div/div/button');
  94. if (csvButton != null && $(csvButton).is(":disabled")){
  95. csvButton.disabled = false;
  96. csvButton.onclick = collectCsv;
  97. clearInterval(intervalId);
  98. }
  99. }
  100.  
  101.  
  102. (function() {
  103. 'use strict';
  104.  
  105.  
  106. $(document).ready ( function(){
  107. intervalId = setInterval(function(){
  108. changeCsvButton();
  109. }, 5000);
  110. });
  111.  
  112.  
  113. })();