集合下载淘宝系订单

方便的下载淘宝订单信息

目前為 2020-04-29 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         集合下载淘宝系订单
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  方便的下载淘宝订单信息
// @author       You
// @grant        none
// @include      https://buyertrade.taobao.com/*
// @include      https://tradearchive.taobao.com/*
// @include      https://trade.tmall.com/*
// @include      https://trade.taobao.com/*
// @require      https://unpkg.com/[email protected]/dist/ajaxhook.min.js

// ==/UserScript==
window.parse_single_order = function(order) {
    var res = {}
    var pure_text = order.textContent;
    var pattern = /(\d{4}-\d{2}-\d{2})订单号: (\d{18})/;
    if (pattern.exec(pure_text)){
        var order_date = pure_text.match(pattern)[1];
        var order_id = pure_text.match(pattern)[2];
    } else{
        console.log(pure_text);
        console.log("date and ID not found");
        return 0;
    }
    var mini_id = 0;
    while(1){
        var count = ".0.7:$order-" + order_id + ".$" + order_id + ".0.1:1:0.$" + mini_id + ".$2.0.0";
        var product = ".0.7:$order-" + order_id + ".$" + order_id + ".0.1:1:0.$" + mini_id + ".$0.0.1.0.0.1";
        var price = ".0.7:$order-" + order_id + ".$" + order_id + ".0.1:1:0.$" + mini_id + ".$1.0.1.1";
        var count_query = order.querySelector("p[data-reactid='" + count + "']");
        var product_query = order.querySelector("span[data-reactid='" + product + "']");
        var price_query = order.querySelector("span[data-reactid='" + price + "']");
        mini_id += 1;
        if(count_query === null | product_query === null | price_query === null){
            break;
        }
        var uprice_str = price_query.textContent
        var count_str = count_query.textContent
        res[order_id + mini_id] = ["NO." + order_id, "D." + order_date, product_query.textContent, uprice_str, count_str, parseFloat(uprice_str) * parseFloat(count_str)]
        //{"id": order_id, "date": order_date, "product": product_query.textContent, "uprice": uprice_str, "count": count_str, "tprice": parseFloat(uprice_str) * parseFloat(count_str)}
    }
    return res
}

window.add_buttons = function(main_element, onclick_func={}, value="未指定按钮", width="60px", height="60px") {
    var button = document.createElement("input"); //创建一个input对象(提示框按钮)
    button.setAttribute("type", "button");
    button.setAttribute("value", value);
    button.style.width = width;
    button.style.height = height;
    button.style.align = "center";
    button.style.marginLeft = "250px";
    button.style.marginBottom = "10px";
    button.style.background = "#b46300";
    button.style.border = "1px solid #b46300";//52
    button.style.color = "white";
    button.onclick = function() {
        onclick_func()
    }
    main_element.appendChild(button);
    main_element.insertBefore(button, main_element.childNodes[0]);
}


window.add_items_to_list = function() {
    var items_list = document.getElementsByClassName("js-order-container");
    var item
    for (let item of items_list) {
        var single_res = window.parse_single_order(item)
        var index
        for (index in single_res){
            window.results[index] = single_res[index]
        }
    }
};

window.download_csv = function() {
    window.add_items_to_list();
    let csvContent = "data:text/csv;chabutton_downloadrset=utf-8,";
    var header_csv = ["订单号", "下单日期", "商品明细", "单价", "数量", "总价"]
    let row = header_csv.join(",");
    csvContent += row + "\r\n";

    for (let index in window.results) {
        let row = window.results[index].join(",");
        csvContent += row + "\r\n";
    }

    var encodedUri = encodeURI(csvContent);
    var link = document.createElement("a");
    link.setAttribute("href", encodedUri);
    link.setAttribute("download", "my_data.csv");
    document.body.appendChild(link); // Required for FF
    link.click();
}




window.add_tb_bought_buttons = function() {
    'use strict';
    var main_element = document.getElementById("J_bought_main");

    window.add_buttons(main_element, window.download_csv, "下载", "160px")
    window.add_buttons(main_element, window.add_items_to_list, "添加本页订单", "160px")

}


window.tb_generate_print_page = function() {
    console.log("wtf")
    document.body.innerHTML = document.getElementsByClassName('app-mod__tabs-container___iwi0I')[0].innerHTML;
    document.body.style.width = "950px"
    document.body.style.marginLeft = "auto"
    document.body.style.marginRight = "auto"

}

window.print_tb_detail_buttons = function(){
    'use strict';
    var main_element = document.getElementById("content");

    // 下载 CSV
    window.add_buttons(main_element, window.tb_generate_print_page, "生成打印页面", "160px")

}


window.tm_generate_print_page = function() {
    document.body.innerHTML=document.getElementById('appDetailPanel').innerHTML + "</br>" + document.getElementById('appOrders').innerHTML + "</br>" + document.getElementById('appAmount').innerHTML;
}

window.print_tm_detail_buttons = function() {
    'use strict';
    var main_element = document.getElementById("content");
    //main_element, onclick_func = {}, value="未指定按钮", width="60px", height="60px"
    window.add_buttons(main_element, window.tm_generate_print_page, "生成打印页面", "160px")

}

window.tb_bought_page = function(){
    window.results = {};
    window.add_tb_bought_buttons()
}

window.tb_detail_page = function() {
    window.print_tb_detail_buttons();
}

window.tm_detail_page = function(){
    window.print_tm_detail_buttons();
}

window.printme = function() {
    document.body.innerHTML=document.getElementById('appDetailPanel').innerHTML+'<br/>'+document.getElementById('appOrders').innerHTML + '<br/>'+document.getElementById('appAmount').innerHTML;
    setTimeout(window.print, 3000) ;
};

var re_tb_bought = /buyertrade\.taobao\.com\/trade/g;
var re_tb_tradearchive = /tradearchive\.taobao\.com/g
var re_tm_detail = /trade\.tmall\.com\/detail/g
var re_tb_detail = /trade\.taobao\.com\/detail/g

if (re_tb_bought.exec(document.URL)) {
    window.tb_bought_page()
    console.log("tb_bought_page")
}

if (re_tb_detail.exec(document.URL)){
    window.tb_detail_page()
    console.log("tb_detail_page")
}

if (re_tm_detail.exec(document.URL)){
    window.tm_detail_page()
    console.log("tm_detail_page")
}