// ==UserScript==
// @name 阿里巴巴国际站批量导出询盘国家或垃圾询盘
// @namespace http://tampermonkey.net/
// @version 0.0.3
// @description 在询盘页面,搜索按钮后面插入一个下载按钮,点击可以从头开始记录每个客户的分配记录,最终输出成JSON文件自动下载。
// @author Mike
// @icon http://is.alicdn.com/favicon.ico
// @match https://message.alibaba.com/message/*
// @grant GM_addStyle
// ==/UserScript==
'use strict';
var globalAllresult = [];
var downloaded = false;
var repeated = false;
var pages = 0;
// 下面这段代码生成下载方式
(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 inquiryIds = document.getElementsByClassName('spec-inquiry-id');
var inquiryFlags = document.getElementsByClassName('ui2-flag');
//var levels = document.getElementsByClassName('aui-icon-buyer-level');
var customers = document.getElementsByClassName('spec-icon-wrap');
var result=[];
for (var i = 0; i < inquiryIds.length; i++) {
console.log(inquiryIds[i].innerText,inquiryFlags[i].title);
//console.log(levels[i])
var customer = customers[i].childNodes[3].src;
console.log(customer)
result.push({
'inquiryId':inquiryIds[i].innerText,
'inquiryFlag':inquiryFlags[i].title,
'customer':customer
});
}
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;
var down_input_html = '<span>';
down_input_html += '<input type="text" id="down_input" placeholder="页数" class="ui2-pagination-goto"</input>';
down_input_html += '</span>';
var inner_input = document.createElement('span');
inner_input.innerHTML = down_input_html;
//将以上拼接的html代码插入到网页标签中
var ul_tag = document.getElementsByClassName('ui-dropdown-btn')[0].parentNode;
console.log('ul_tag');
//console.log(ul_tag);
if (ul_tag) {
ul_tag.append(inner_input);
ul_tag.append(inner);
}
var btn = document.getElementById('down_contacts_btn');
var type = document.getElementsByClassName('ui-dropdown-btn')[0].innerText
btn.onclick = function(){
if (repeated) {
confirm('请刷新页面后重试');
return;
}
if(downloaded) {
repeated = true;
return;//防抖,防止多次下载,刷新重启
}
var nextBtn = document.getElementsByClassName('next')[0];
console.log(nextBtn);
var ipt = document.getElementById('down_input').value;
console.log('ipt:',ipt);
if (undefined == nextBtn || (ipt-1) == pages){
console.log('没了');
let result = collectItems();
globalAllresult.push(result);
globalAllresult = [].concat(...globalAllresult);
if (type.indexOf('分配给') != -1) {
console.save(globalAllresult,"InquiryCountry.json");
}else{
console.save(globalAllresult,"InquirySpam.json");
}
downloaded = true;
}else{
console.log('next page');
pages += 1
console.log('pages:',pages);
let result = collectItems();
globalAllresult.push(result);
nextBtn.click();
}
}
版权属于:
admin
作品采用:
《
署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
》许可协议授权
评论 (0)