// ==UserScript==
// @name 阿里巴巴国际站批量导出产品信息
// @namespace http://tampermonkey.net/
// @version 2.1.3
// @description 导出产品ID、链接、所属赛道
// @author Mike
// @icon http://is.alicdn.com/favicon.ico
// @match https://hz-productposting.alibaba.com/product/*
// @grant GM_addStyle
// ==/UserScript==
'use strict';
var globalAllresult = [];
var downloaded = false;
var repeated = false;
// 下面这段代码生成下载方式
(function(console){
console.save = function(data, filename){
if(!data) {
console.error('Console.save: No data found!')
return;
}
if(!filename) filename = 'console.json'
if(typeof data === "object"){
data = JSON.stringify(data, undefined, 4)
}
var blob = new Blob([data], {type: 'text/json'}),
e = document.createEvent('MouseEvents'),
a = document.createElement('a')
//var text = await blob.text()
a.download = filename
a.href = window.URL.createObjectURL(blob)
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':')
e = new MouseEvent ("click");
//e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null) !!!deprecated!!!
a.dispatchEvent(e)
}
})(console)
// 分配记录收集方法
function collectItems(){
var productIDs = document.getElementsByClassName('product-id');
var productSubjects = document.getElementsByClassName('product-subject');
var result=[];
for (var i = 0; i < productIDs.length; i++) {
console.log(productIDs[i].innerText,productSubjects[i].firstChild.href);
result.push({
'productID':productIDs[i].innerText,
'productURL':productSubjects[i].firstChild.href,
'productType':productIDs[i].parentNode.parentNode.parentNode.parentNode.childNodes[2].innerText,
'productLevel':productIDs[i].parentNode.parentNode.parentNode.parentNode.childNodes[6].innerText,
});
}
return result;
}
//与元数据块中的@grant值相对应,功能是生成一个style样式
GM_addStyle('#down_contacts_btn{color:#fb7d3c;}');
//下载按钮的html代码
var down_btn_html = '<span>';
down_btn_html += '<a href="javascript:void(0);" id="down_contacts_btn" class="S_txt2" title="一直点击,直到翻页到最后一页,自动下载,若无效请刷新重试">导出产品信息</a>';
down_btn_html += '</span>';
var inner = document.createElement('span');
inner.innerHTML = down_btn_html;
//将以上拼接的html代码插入到网页标签中
var ul_tag = document.getElementsByClassName('manage-app-market-status-selector')[0].parentNode;
console.log('ul_tag')
console.log(ul_tag)
if (ul_tag) {
ul_tag.append(inner);
}
var btn = document.getElementById('down_contacts_btn');
btn.onclick = function(){
if (repeated) {
confirm('请刷新页面后重试');
return;
}
if(downloaded) {
repeated = true;
return;//防抖,防止多次下载,刷新重启
}
var nextBtns = document.getElementsByClassName('next-pagination-item');
var n = nextBtns.length-1;
if (nextBtns[n].innerText != '下一页') {
n = n-1;
}
console.log(nextBtns[n]);
var nextBtn = nextBtns[n]
if(!nextBtn.disabled) {
console.log('next page');
let result = collectItems();
globalAllresult.push(result);
nextBtn.click();
}else{
console.log('page end');
let result = collectItems();
globalAllresult.push(result);
globalAllresult = [].concat(...globalAllresult);
console.save(globalAllresult,"ProductsInfo.json");
downloaded = true;
}
// 下载数据,第一个参数是数据对象,第二个参数是要保存成文件的名字。
}
版权属于:
admin
作品采用:
《
署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
》许可协议授权
评论 (0)