Greasy Fork

Greasy Fork is available in English.

商品概览_货品结构分析

云图扩展工具

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

// ==UserScript==
// @name         商品概览_货品结构分析
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  云图扩展工具
// @author       siji-Xian
// @match        *://yuntu.oceanengine.com/yuntu_ng/product/productOverview/productStructure?*
// @icon         https://www.google.com/s2/favicons?domain=oceanengine.com
// @grant        none
// @license      MIT
// @require      https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/3.2.1/jquery.min.js
// @require      https://cdn.bootcss.com/moment.js/2.20.1/moment.min.js
// @require      http://greasyfork.icu/scripts/404478-jsonexportexcel-min/code/JsonExportExcelmin.js?version=811266
// @require      http://greasyfork.icu/scripts/455576-qmsg/code/Qmsg.js?version=1122361
// ==/UserScript==

(function () {
  "use strict";
  var new_element = document.createElement("link");
  new_element.setAttribute("rel", "stylesheet");
  new_element.setAttribute("href", "https://qmsg.refrain.xyz/message.min.css");
  document.body.appendChild(new_element);

  const button = document.createElement("div");
  button.textContent = "导出数据";
  Object.assign(button.style, {
    height: "34px",
    lineHeight: "var(--line-height, 34px)",
    alignItems: "center",
    color: "white",
    background: "linear-gradient(90deg, rgba(0, 239, 253), rgba(64, 166, 254))",
    borderRadius: "5px",
    marginLeft: "10px",
    fontSize: "13px",
    padding: "0 10px",
    cursor: "pointer",
    fontWeight: "500",
  });
  button.addEventListener("click", urlClick);

  function appendDoc() {
    const likeComment = document.querySelector(".ProductAnalysis__Selector-cOfEfK");
    if (likeComment) {
      likeComment.append(button);
      return;
    }
    setTimeout(appendDoc, 1000);
  }
  appendDoc();

  //message.js
  let loadingMsg = null;

  //目标数据
  let target_data = null;

  window.au_fetch = window.fetch;
  window.fetch = async (...args) => {
    let [resource, config] = args;
    const response = await window.au_fetch(resource, config);
    if (
      response.url.slice(0, 89) ==
      "https://yuntu.oceanengine.com/product_node/api/graphql/?op=productOverviewProductAnalysis"
    ) {
      target_data={ response, body: args[1].body }
    }
    return response;
  };

  async function getData(e) {
    let body = target_data.body;
    var xhr = new XMLHttpRequest();
    xhr.withCredentials = true;

    xhr.addEventListener("readystatechange", function () {
      if (this.readyState === 4) {
        let _this = JSON.parse(this.responseText)
        expExcel(_this.data)
      }
    });
    xhr.open("POST", target_data.response.url);
    xhr.setRequestHeader("authority", "yuntu.oceanengine.com");
    xhr.setRequestHeader("accept", "*/*");
    xhr.setRequestHeader("accept-language", "zh-CN,zh;q=0.9");
    xhr.setRequestHeader("content-type", "application/json");

    xhr.send(body);

  }


  function expExcel(e) {

    let fileName = `货品结构分析`

    let data = e.data?.list.map(v=>{
      return {...v,category:`${v.firstCategoryName}-${v.secondCategoryName}-${v.thirdCategoryName}`}
    })
    let contrast = {
      排名: "rank",
      商品信息: "itemName",
      商品ID:"itemId",
      图片链接:"imgUrl",
      商品品类: "category",
      销售额:"gmv",
      销售额占比:"gmvRatio",
      销售量:"salesVolumn",
      曝光人数:"showUidCnt",
      点击人数:"clickUidCnt",
      购买人数:"purchaseUidCnt",
      点击率:"showClickRatio",
      转化率:"clickPurchaseRatio",
      曝光_购买转化率:"showPurchaseRatio",
      GPM:"gpm",
      客单价:"perUidValue",
      A4拉新人数:"pullA4Cnt",
      拉新率:"pullA4Ratio",
      爆品指数:"productScore",
    };

    let option = {};
    option.fileName = fileName; //文件名
    option.datas = [
      {
        sheetName: "",
        sheetData: data,
        sheetHeader: Object.keys(contrast),
        sheetFilter: Object.values(contrast),
        columnWidths: [], // 列宽
      }
    ];
    var toExcel = new ExportJsonExcel(option);
    toExcel.saveExcel();
    loadingMsg.close();
  }

  function urlClick() {
    if (target_data) {
      loadingMsg = Qmsg.loading("正在导出,请勿重复点击!");
      getData(target_data);
    } else {
      loadingMsg = Qmsg.error("数据加载失败,请刷新页面!");
    }
  }
})();