Dune Csv Export

Downloading the queries csv export for free subscription users

目前为 2024-02-18 提交的版本,查看 最新版本

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