188 lines
6.3 KiB
JavaScript
188 lines
6.3 KiB
JavaScript
// ==UserScript==
|
|
// @name 批量下载市场洞察品牌数据
|
|
// @namespace https://bbs.tampermonkey.net.cn/
|
|
// @version 0.1.0
|
|
// @description 下载抖音云图市场洞察品牌数据
|
|
// @author wan喜
|
|
// @match https://yuntu.oceanengine.com/yuntu_brand/ecom/product/segmentedMarketDetail/marketProduct?*
|
|
// @icon https://www.google.com/s2/favicons?domain=oceanengine.com
|
|
// @require https://cdn.staticfile.net/sweetalert2/11.10.0/sweetalert2.all.min.js
|
|
// ==/UserScript==
|
|
|
|
(function () {
|
|
"use strict"
|
|
|
|
// 创建下载按钮
|
|
var newButton = document.createElement("button")
|
|
newButton.innerHTML = "下载品牌数据"
|
|
newButton.type = "button"
|
|
newButton.style.height = "34px"
|
|
newButton.style.lineHeight = "34px"
|
|
newButton.style.align = "center" // 文本居中
|
|
newButton.style.color = "white" // 按钮文字颜色
|
|
newButton.style.background = "#1f4bd9" // 按钮底色
|
|
newButton.style.border = "1px solid #1f4bd9" // 边框属性
|
|
newButton.style.borderRadius = "3px" // 按钮四个角弧度
|
|
newButton.style.marginLeft = '10px'
|
|
newButton.style.fontSize = '12px'
|
|
newButton.style.padding = '0 10px'
|
|
newButton.className = "byted-btn byted-btn-size-md byted-btn-type-primary byted-btn-shape-angle byted-can-input-grouped"
|
|
newButton.addEventListener("click", handleButtonClick) // 监听按钮点击事件
|
|
|
|
// 添加按钮到页面
|
|
function appendButton() {
|
|
setTimeout(() => {
|
|
// 这里需要根据实际页面结构找到合适的位置添加按钮
|
|
var targetElement = document.querySelector(".操作区域的选择器") // 需要根据实际页面修改
|
|
if (targetElement) {
|
|
targetElement.append(newButton)
|
|
console.log("按钮已添加")
|
|
} else {
|
|
console.log("未找到目标元素,继续尝试")
|
|
appendButton()
|
|
}
|
|
}, 1000)
|
|
}
|
|
appendButton()
|
|
|
|
// 从URL获取参数
|
|
function getParamsFromUrl() {
|
|
const url = window.location.href
|
|
const aadvid = url.split('aadvid=')[1]?.split('&')[0]
|
|
const reportId = url.split('reportId=')[1]?.split('&')[0]
|
|
|
|
return {
|
|
aadvid,
|
|
reportId
|
|
}
|
|
}
|
|
|
|
// 获取请求体参数
|
|
function getRequestBody() {
|
|
// 这里需要根据实际情况获取或设置参数
|
|
// 可以从页面元素中提取,或者让用户输入
|
|
return {
|
|
reportId: "", // 需要获取
|
|
categoryId: "0",
|
|
contentType: "ALL",
|
|
startDate: "0",
|
|
endDate: "2025-03-31",
|
|
dateType: "MONTH",
|
|
analysisType: "DRILL_DOWN",
|
|
itemType: "PRODUCT_BRAND",
|
|
sortKey: "salesAmount",
|
|
page: 1,
|
|
pageSize: 100,
|
|
order: "ascend"
|
|
}
|
|
}
|
|
|
|
// 发送请求获取数据
|
|
async function fetchBrandData(aadvid, body) {
|
|
const url = `https://yuntu.oceanengine.com/product_node/v2/api/industry/insightBrandStats?aadvid=${aadvid}`
|
|
|
|
try {
|
|
const response = await fetch(url, {
|
|
method: "POST",
|
|
headers: {
|
|
"accept": "application/json, text/plain, */*",
|
|
"content-type": "application/json"
|
|
},
|
|
body: JSON.stringify(body),
|
|
credentials: "include"
|
|
})
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP error! status: ${response.status}`)
|
|
}
|
|
|
|
const data = await response.json()
|
|
return data
|
|
} catch (error) {
|
|
console.error("获取数据失败:", error)
|
|
throw error
|
|
}
|
|
}
|
|
|
|
// 处理数据并下载CSV
|
|
function processAndDownloadData(data) {
|
|
// 假设data.data.list是品牌数据列表
|
|
if (!data || !data.data || !data.data.list) {
|
|
throw new Error("数据格式不正确")
|
|
}
|
|
|
|
const brandList = data.data.list
|
|
|
|
// 创建CSV内容
|
|
let csv = ''
|
|
|
|
// 添加表头
|
|
const headers = Object.keys(brandList[0])
|
|
csv += headers.join(',') + '\n'
|
|
|
|
// 添加数据行
|
|
brandList.forEach(brand => {
|
|
const values = headers.map(header => {
|
|
let value = brand[header]
|
|
// 处理包含逗号的字符串
|
|
if (typeof value === 'string' && value.includes(',')) {
|
|
return `"${value}"`
|
|
}
|
|
return value
|
|
})
|
|
csv += values.join(',') + '\n'
|
|
})
|
|
|
|
// 下载CSV文件
|
|
const hiddenElement = document.createElement('a')
|
|
hiddenElement.href = 'data:text/csv;charset=utf-8,%EF%BB%BF' + encodeURI(csv)
|
|
hiddenElement.target = '_blank'
|
|
hiddenElement.download = `品牌数据_${new Date().toISOString().slice(0, 10)}.csv`
|
|
hiddenElement.click()
|
|
}
|
|
|
|
// 按钮点击处理函数
|
|
async function handleButtonClick() {
|
|
try {
|
|
// 显示加载提示
|
|
const Toast = Swal.mixin({
|
|
toast: true,
|
|
position: "top-end",
|
|
showConfirmButton: false,
|
|
timer: 3000,
|
|
})
|
|
Toast.fire({
|
|
icon: "info",
|
|
title: "正在获取数据..."
|
|
})
|
|
|
|
// 获取参数
|
|
const { aadvid, reportId } = getParamsFromUrl()
|
|
|
|
// 获取请求体
|
|
const requestBody = getRequestBody()
|
|
requestBody.reportId = reportId
|
|
|
|
// 获取数据
|
|
const brandData = await fetchBrandData(aadvid, requestBody)
|
|
|
|
// 处理并下载数据
|
|
processAndDownloadData(brandData)
|
|
|
|
// 显示成功提示
|
|
Toast.fire({
|
|
icon: "success",
|
|
title: "数据下载成功"
|
|
})
|
|
} catch (error) {
|
|
console.error(error)
|
|
|
|
// 显示错误提示
|
|
Swal.fire({
|
|
icon: "error",
|
|
title: "下载失败",
|
|
text: error.message
|
|
})
|
|
}
|
|
}
|
|
})() |