Dune Csv Export

Downloading the queries csv export for free subscription users

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Dune Csv Export
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Downloading the queries csv export for free subscription users
// @author       Rv
// @match        https://dune.com/queries*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=dune.com
// @grant        none
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js
// @license MIT
// ==/UserScript==

function getElementByXpath(path){
  return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}

var intervalId;

function getHeaders(table){
    const headers = [];
    $.each($(table).find("thead").find("tr").find("th"), function (key, val2) {
        headers[key] = $(val2).find("div").text();
    });
    return headers;
}

function getValues(table){
    const values = []
    $.each($(table).find("tbody").find("tr"), function (key, val) {
        const row = [];
        $.each($(val).find("td"), function (tkey, tval) {
            row[tkey] = $(tval).find("div").first().text();
        });
        values[key] = row;
    });
    return values;
}

function collectLoop(last_values=[]){
    let nextButton = getElementByXpath('//*[@id="results"]/div/div[2]/div/div[2]/ul/li[6]/button');
    let table = getElementByXpath('//*[@id="results"]/div/div[2]/div/div[1]/table');
    const values = getValues(table);
    const newValues = last_values.concat(values);

    if (!$(nextButton).is(":disabled") && nextButton != null){
        $(nextButton).trigger("click");
        setTimeout(()=>{
            return collectLoop(newValues);
        }, 20)
    } else {
        const headers = getHeaders(table);
        download_csv(headers, newValues);
    }
}


function download_csv(headers, rows) {
    let csvHeaders = headers.join(",") + "\n";
    let csvRows = rows.map(row => row.join(",")).join("\n");

    var downloadBtn = document.createElement("a");
    downloadBtn.href = "data:text/csv;charset=utf-8,"+encodeURI(csvHeaders+csvRows);
    downloadBtn.target = "_blank";
    let url = window.location.href;
    downloadBtn.download = "query_"+url.split("queries/")[1].replace("/", "_")+".csv";
    downloadBtn.click();

}


function collectCsv() {

    let nextButton = getElementByXpath('//*[@id="results"]/div/div[2]/div/div[2]/ul/li[6]/button');
    if (nextButton == null){
        console.log("Butt not exists");
        collectLoop();
    } else {
        // go to first
        let firstPageBtn = getElementByXpath('//*[@id="results"]/div/div[2]/div/div[2]/ul/li[3]/button');
        if (firstPageBtn != null && !$(firstPageBtn).is(":disabled")){
            $(firstPageBtn).trigger("click");
            setTimeout(()=>{
                collectLoop();
            }, 20)
        } else {
            collectLoop();
        }
    }
}

function changeCsvButton(){
    let csvButton = getElementByXpath('//*[@id="results"]/div/div[1]/div[1]/div/div/button');
    if (csvButton != null && $(csvButton).is(":disabled")){
        csvButton.disabled = false;
        csvButton.onclick = collectCsv;
        clearInterval(intervalId);
    }
}


(function() {
    'use strict';


    $(document).ready ( function(){
        intervalId = setInterval(function(){
            changeCsvButton();
        }, 5000);
    });


})();