// ==UserScript== // @name 亚马逊批量下载业务报告 // @namespace https://github.com/MaiXiaoMeng // @version 0.0.1 // @description 垃圾亚马逊,连批量下载业务报告的方法都没有,只好自己写一个了 // @author XiaoMeng Mai // @license GPLv3 // @match https://*.amazon.com/* // @match https://*.amazon.co.uk/* // @match https://*.amazon.co.de/* // @match https://*.amazon.fr/* // @match https://*.amazon.it/* // @match https://*.amazon.es/* // @match https://*.amazon.co.jp/* // @match https://*.amazon.com.au/* // @match https://*.amazon.sg/* // @grant GM_xmlhttpRequest // @grant GM_getResourceText // @grant GM_download // @grant GM_addStyle // @grant GM_openInTab // @grant GM_setValue // @grant GM_getValue // @grant GM_registerMenuCommand // @grant GM_unregisterMenuCommand // @grant unsafeWindow // @compatible firefox Tampermonkey // @compatible chrome Tampermonkey // @compatible edge Tampermonkey // @require https://unpkg.com/jquery // @require https://unpkg.com/sweetalert2 // @resource element-plus https://unpkg.com/element-plus/dist/index.css // @note 0.0.1 支持业务报告批量下载 // @downloadURL none // ==/UserScript== initializationScript() // 业务报告页面 waitForKeyElements(".css-1lafdix", getBusinessReport, ['business-reports/']) function waitForKeyElements(selectorTxt, actionFunction, activeURL, bWaitOnce, iframeSelector) { var active_url = false if (typeof activeURL == "object") { for (let index = 0; index < activeURL.length; index++) { if (window.location.href.indexOf(activeURL[index]) > -1) { active_url = true break } } } else if (typeof activeURL == "string") { if (window.location.href.indexOf(activeURL) > -1) active_url = true } if (active_url) { var targetNodes, btargetsFound; if (typeof iframeSelector == "undefined") { targetNodes = $(selectorTxt); } else { targetNodes = $(iframeSelector).contents().find(selectorTxt); } if (targetNodes && targetNodes.length > 0) { btargetsFound = true; targetNodes.each(function () { var jThis = $(this); var alreadyFound = jThis.data("alreadyFound") || false; if (!alreadyFound) { logPrint(`selectorTxt > ${selectorTxt} activeURL > ${activeURL} bWaitOnce > ${bWaitOnce} iframeSelector > ${iframeSelector}`) console.log(jThis); var cancelFound = false if (typeof actionFunction == "object") { actionFunction.forEach(element => { cancelFound = element(jThis, selectorTxt, activeURL); }) } else if (typeof actionFunction == "function") { cancelFound = actionFunction(jThis, selectorTxt, activeURL); } if (cancelFound) { btargetsFound = false } else { jThis.data("alreadyFound", true); } } }) } else { btargetsFound = false; } var controlObj = waitForKeyElements.controlObj || {}; var controlKey = selectorTxt.replace(/[^\w]/g, "_"); var timeControl = controlObj[controlKey]; if (btargetsFound && bWaitOnce && timeControl) { clearInterval(timeControl); delete controlObj[controlKey]; } else { if (!timeControl) { timeControl = setInterval(function () { waitForKeyElements(selectorTxt, actionFunction, activeURL, bWaitOnce, iframeSelector); }, 300); controlObj[controlKey] = timeControl; } } waitForKeyElements.controlObj = controlObj; } } function makeGetRequest(url, method = 'GET', data = null) { logPrint(`${method} -> ${url}`) return new Promise((resolve, reject) => { GM_xmlhttpRequest({ url: url, method: method, data: data, onload: function (response) { resolve(response); }, onerror: function (error) { reject(error); } }); }); } function logPrint(params) { var date_time = $.trim(new Date(new Date().setHours(new Date().getHours() + 8)).toISOString().replace("Z", " ").replace("T", " ")) var function_name = (new Error()).stack.split("\n")[2].trim().split(" ")[1] console.log(`[${date_time}][DEBUG] ${function_name} - ${params}`) } function sleep(interval) { return new Promise(resolve => { setTimeout(resolve, interval) }) } function getBusinessReport(jNode, selectorTxt, activeURL) { if (jNode.attr("class").indexOf("beibei") == -1) { jNode.after($(`
`)) $("#batch_download_of_the_daily_business_report").click(function (event) { var start_date = new Date($(".css-jfggi0")[0].value); var end_date = new Date($(".css-jfggi0")[1].value); var site_brand = $(".partner-dropdown-button > span").text().split(" | "); (async () => { while (start_date <= end_date) { var api_url = `https://${document.domain}/business-reports/api`; var data = JSON.stringify({ operationName: "reportDataDownloadQuery", variables: { input: { legacyReportId: "102:DetailSalesTrafficByParentItem", startDate: `${getFormatDate(start_date)}`, endDate: `${getFormatDate(start_date)}`, userSelectedRows: [], selectedColumns: [ "SC_MA_ParentASIN_25990", "SC_MA_ChildASIN_25991", "MYG_Growth_Opportunities", "sc_mat-ss_colDef_title", "SC_MA_MobileAppSessions", "SC_MA_BrowserSessions", "SC_MA_Sessions_Total", "SC_MA_MobileAppSessionPercentage", "SC_MA_BrowserSessionPercentage", "SC_MA_SessionPercentage_Total", "SC_MA_MobileAppPageViews", "SC_MA_BrowserPageViews", "SC_MA_PageViews_Total", "SC_MA_MobileAppPageViewsPercentage", "SC_MA_BrowserPageViewsPercentage", "SC_MA_PageViewsPercentage_Total", "SC_MA_BuyBoxPercentage_25956", "SC_MA_UnitsOrdered_40590", "SC_MA_UnitSessionPercentage_25957", "SC_MA_OrderedProductSales_40591", "SC_MA_TotalOrderItems_1" ], }, }, query: "query reportDataDownloadQuery($input: GetReportDataInput) {\n getReportDataDownload(input: $input) {\n url\n __typename\n }\n}\n", }) var response = await makeGetRequest(url = api_url, method = 'POST', data = data) var download_url = JSON.parse(response.responseText)["data"]["getReportDataDownload"]["url"] var download_file_name = `${site_brand[1]}_${site_brand[0]}_${getFormatDate(start_date)}.csv` ElementPlus.ElMessage(`正在下载: ${download_file_name}`) console.log(`正在下载: ${download_file_name} URL: ${download_url}`) GM_download(download_url, download_file_name) start_date = new Date((start_date / 1000 + 86400 * 1) * 1000) } })() }) } } function initializationScript() { var $ = window.$; var VueCDN = "https://lib.baomitu.com/vue/3.2.36/vue.global.prod.min.js"; var ElementPlusCDN = "https://lib.baomitu.com/element-plus/2.2.2/index.full.min.js"; // 过滤广告 GM_addStyle(GM_getResourceText("element-plus")) $.getScript(VueCDN, function () { console.log("[" + VueCDN + "] Vue 加载成功"); $.getScript(ElementPlusCDN, function () { console.log("[" + ElementPlusCDN + "] ElementPlus 加载成功") var ElementPlus = unsafeWindow.ElementPlus; var Vue = unsafeWindow.Vue; }) }) }