Skip to content

Instantly share code, notes, and snippets.

@dfang
Last active July 18, 2019 07:11
Show Gist options
  • Save dfang/21cb975506155e43f8eadf013636245d to your computer and use it in GitHub Desktop.
Save dfang/21cb975506155e43f8eadf013636245d to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name JDWL(京东大家电----订单一键导出)
// @namespace Violentmonkey Scripts
// @version 0.0.4
// @homepage https://github.com/dfang/userscript
// @homepageURL https://gist.github.com/dfang/21cb975506155e43f8eadf013636245d
// @description try to make partner.dhc.jd.com more user friendly!
// @author fang duan
// @include *partner.dhc.jd.com*
// @grant GM_notification
// @grant GM_log
// @grant GM_addStyle
// @grant GM_setValue
// @grant GM_getValue
// @run-at document-end
// @updateURL https://gist.githubusercontent.com/dfang/21cb975506155e43f8eadf013636245d/raw/jdwl.user.js
// @downloadURL https://gist.githubusercontent.com/dfang/21cb975506155e43f8eadf013636245d/raw/jdwl.user.js
// @require https://gist.githubusercontent.com/dfang/21cb975506155e43f8eadf013636245d/raw/reservation.js
// @require https://gist.githubusercontent.com/dfang/21cb975506155e43f8eadf013636245d/raw/orderReports.js
// @require https://raw.githubusercontent.com/eligrey/FileSaver.js/master/dist/FileSaver.js
// ==/UserScript==
var uuid = "15d99242e9f7235d220d";
(function () {
"use strict";
GM_setValue("uuid", uuid);
GM_log("start hacking ......");
GM_log("uuid: " + GM_getValue("uuid"))
InitForReservationsPage()
})();
function initForOrderReportsPage() {
// 报表管理/ 配送员监控/ 运单明细页面 底部加的下载按钮
$("#export").parent().append(
$("#export")
.clone()
.attr("id", "downloadOrdersIds")
.attr("name", "downloadOrdersIds")
.attr("onclick", "")
.text("下载本页所有的订单编号")
)
$(document).on("click", "#downloadOrdersIds", downloadOrderIds);
}
function downloadOrderIds() {
var orders = []
$("table tr td:nth-child(7)").each(function (i, e) {
console.log($(e).text())
orders.push($(e).text())
})
var filename = formatDate($("table tr td:nth-child(6)").first().text()) + ".txt"
var blob = new Blob([orders.join("\n")], { type: "text/plain;charset=utf-8" });
saveAs(blob, filename);
}
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('');
}
// ==UserScript==
// @name JDWL(京东大家电----订单一键导出)
// @namespace Violentmonkey Scripts
// @version 0.0.4
// @homepage https://github.com/dfang/userscript
// @homepageURL https://gist.github.com/dfang/21cb975506155e43f8eadf013636245d
// @description try to make partner.dhc.jd.com more user friendly!
// @author fang duan
// @include *partner.dhc.jd.com*
// @include *ext.mads.jd.com*
// @grant GM_notification
// @grant GM_log
// @grant GM_addStyle
// @grant GM_setValue
// @grant GM_getValue
// @run-at document-end
// @updateURL https://gist.githubusercontent.com/dfang/21cb975506155e43f8eadf013636245d/raw/jdwl.user.js
// @downloadURL https://gist.githubusercontent.com/dfang/21cb975506155e43f8eadf013636245d/raw/jdwl.user.js
// @require http://code.jquery.com/jquery-1.12.4.min.js
// @require https://gist.githubusercontent.com/dfang/21cb975506155e43f8eadf013636245d/raw/reservation.js
// @require https://gist.githubusercontent.com/dfang/21cb975506155e43f8eadf013636245d/raw/orderReports.js
// @require https://raw.githubusercontent.com/eligrey/FileSaver.js/master/dist/FileSaver.js
// ==/UserScript==
window.addEventListener('message', function (event) {
console.log(event)
// IMPORTANT: Check the origin of the data!
if (~event.origin.indexOf('http://partner.dhc.jd.com')) {
// The data has been sent from your site
// The data sent with postMessage is stored in event.data
console.log(event.data);
} else {
// The data hasn't been sent from your site!
// Be careful! Do not use it.
return;
}
});
console.log("reservations page");
function InitForReservationsPage() {
GM_log("uuid: " + GM_getValue("uuid"))
// var $ = window.jQuery;
init1();
init2();
// select default warehouses and default carrier
allTasks();
// add a fake button to modal popup for export data and then confirm reservation
$("#reserve .modal-content").append(
$("#confirm")
.clone()
.attr("id", "export")
.text("导出数据")
);
$("#reserve #confirm").hide();
$("#reserve .modal-content")
.find(".btn-primary")
.css("margin-bottom", "30px")
.css("margin-left", "140px");
$(document).on("click", "#reserve #export", function (e) {
e.preventDefault();
$("#cyq7").val(
"您好,您的订单已经预约成功,如遇客户催单及投诉,可以联系站点电话18070421816或0792-7688336,空调安装电话17370085337,或咚咚联系/李娟/邓彬"
);
$(e.target).attr("disabled", true);
var reservation = null;
var orderDetails = [];
var payment = null;
reservation = new Object({
order_no: $("#cyq1").text().trim(),
customer_address: $("#cyq6").val().trim(),
customer_name: $("#cyq2").val().trim(),
customer_phone: $("#cyq3").val().trim(),
reserverd_delivery_time: $("#cyq4").val().trim(),
reserverd_setup_time: $("#cyq5").val().trim(),
is_delivery_and_setup: $("#cyq22").val().trim(),
created_at: new Date().toISOString(),
updated_at: new Date().toISOString()
});
var $paymentTR = $("#reserve .modal-content table:eq(0) tbody").find(
"tr:eq(0)"
);
payment = new Object({
total: $paymentTR.find("td:eq(0)").text().trim(),
paymentMethod: $paymentTR.find("td:eq(1)").text().trim()
});
var $orderDetailsTRS = $("#reserve .modal-content table:eq(1) tbody").find(
"tr"
);
$orderDetailsTRS.map(function (i, e) {
var orderLine = new Object({
order_no: reservation.order_no,
product_no: $(e)
.find("td:eq(1)")
.text().trim(),
product_name: $(e)
.find("td:eq(2)")
.text().trim(),
quantity: $(e)
.find("td:eq(3)")
.text().trim(),
install: $(e)
.find("td:eq(7)")
.text().trim(),
created_at: new Date().toISOString(),
updated_at: new Date().toISOString()
});
orderDetails.push(orderLine);
});
reservation.receivables = payment.total;
reservation.order_details = orderDetails;
console.log(reservation);
var order = new Object({
order: reservation,
uuid: GM_getValue("uuid")
})
GM_log("reservation data : " + reservation);
GM_log("Make an ajax request to send reservation to hook")
// make a ajax request to save reservation data
$.ajax({
// url: 'https://jinshuju.net/api/v1/forms/VaVmnO',
// url: "https://xsjd.df1228.now.sh/import",
// url: "https://requestbin.df1228.now.sh/post",
// url: "https://hook.io/df1228/import_jd_orders",
url: "https://en52v9dw7pw0iy3.m.pipedream.net",
type: "POST",
dataType: "json",
tryCount: 0,
retryLimit: 5,
data: JSON.stringify(reservation),
beforeSend: function (xhr) {
// xhr.setRequestHeader("Authorization", "Basic ".concat(btoa("ktAF6IyhZPZoRuyKuuaYRw:uhsvWAdrWcx-svHL-d2LtA")));
xhr.setRequestHeader("Content-Type", "application/json;charset=utf-8");
},
success: function (data) {
GM_log("Post success");
// notify user
var notificationDetails = {
text: "客户" + reservation.customer_name + "的数据导出成功!!!",
title: "导出成功!",
timeout: 5000,
highlight: true,
onclick: function () {
window.focus();
}
};
GM_notification(notificationDetails);
$(e.target).hide();
$("#reserve #confirm").show();
},
error: function (xhr, textStatus, errorThrown) {
if (textStatus == "timeout") {
this.tryCount++;
if (this.tryCount <= this.retryLimit) {
//try again
$.ajax(this);
return;
}
return;
}
if (xhr.status == 500) {
//handle error
} else {
//handle error
}
}
});
$(document).on("click", ".reserveUrl", function (e) {
console.log(e.target);
$("#reserve #confirm").hide();
$("#reserve #export").attr("disabled", false);
$("#reserve #export").show();
});
});
}
function init1() {
GM_log("window.location.host = partner.dhc.jd.com");
// expand menu
$("dt:eq(4) a").click();
// open in new tab
$("a[href='http://ext.mads.jd.com/outweb/orderReserve/index']").attr(
"target",
"_blank"
);
}
function init2() {
addStyles()
// auto select first item when combobox loaded
$.fn.combobox.defaults.onLoadSuccess = function (items) {
if (items.length) {
var opts = $(this).combobox('options');
$(this).combobox('select', items[0][opts.valueField]);
}
}
// auto select carrier and click query button after change warehouse
$(document).on("change", "#warehouses", function () {
setTimeout(function () {
$("#carriers").combobox("setText", "京东帮修水服务店D");
// $('#carriers').combobox("setValue","569972");
}, 500);
setTimeout(function () {
$("#query").click();
}, 1000);
});
// 选择预约状态 自动查询
$(document).on("change", "#states", function () {
setTimeout(function () {
$("#query").click();
}, 500);
});
// 选择预约方式 自动查询
$(document).on("change", "#reserveType", function () {
setTimeout(function () {
$("#query").click();
}, 500);
});
}
function addStyles() {
var styleSheet =
"" +
"#table1.flexigrid_report {" +
"font-size: 14px;" +
"}" +
".form-group {" +
"margin-top: 5px;" +
"}" +
"form button {" +
"margin-top: 8px;" +
"}" +
"";
GM_addStyle(styleSheet);
}
async function allTasks() {
await task1();
await task2();
await task3();
await task4();
await task5();
}
function task1() {
return new Promise(resolve => {
setTimeout(() => {
GM_log("1", "第一个任务: 自动选择机构");
$("#organizations")
.val($("#organizations option:eq(1)").val())
.trigger("change");
resolve("done");
}, 3000);
});
}
function task2() {
return new Promise(resolve => {
setTimeout(() => {
GM_log("2", "第二个任务: 自动选择配送中心");
$("#distributionCentres")
.val($("#distributionCentres option:eq(1)").val())
.trigger("change");
resolve("done");
}, 1000);
});
}
function task3() {
return new Promise((resolve, reject) => {
setTimeout(() => {
GM_log("3", "第三个任务: 自动选择仓库");
$("#warehouses")
.val($("#warehouses option:eq(1)").val())
.trigger("change");
resolve("done");
}, 1000);
});
}
function task4() {
return new Promise(resolve => {
// setTimeout(() => {
// console.log("4", "第四个任务");
// $("#carriers").combobox("setText", "京东帮修水服务店A");
// // $('#carriers').combobox("setValue","569972");
// resolve("done");
// }, 1000);
GM_log("4", "第四个任务: 自动选择承运商");
$.fn.combobox.defaults.onLoadSuccess = function (items) {
if (items.length) {
var opts = $(this).combobox('options');
$(this).combobox('select', items[0][opts.valueField]);
}
}
resolve("done")
});
}
function task5() {
return new Promise(resolve => {
setTimeout(() => {
GM_log("5", "第五个任务: 自动点击查询按钮");
$("#query").click();
resolve("done");
}, 1000);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment