Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
// ==UserScript==
// @name Japan Post Redelivery
// @description Fix usability issues on the Japan Post redelivery website
// @author mirka
// @include https://trackings.post.japanpost.jp/services/srv/search/direct*
// @include https://trackings.post.japanpost.jp/delivery/deli/*
// @namespace http://jaguchi.com
// @version 1.1
// ==/UserScript==
(function () {
var my_postcode = "1234567"; // 郵便番号
var my_directiontype_code = "1"; // 希望の配達先
if (window.location.pathname == "/delivery/deli/") {
redeliveryPage();
} else {
trackingPage();
}
// Run if this is a tracking page
function trackingPage() {
var history_table;
var btn;
if (/Absence|ご不在/.test(document.body.textContent) == false) {
return;
}
history_table = document.querySelector("table[summary='履歴情報']");
// Set up and insert button
btn = document.createElement("input");
btn.type = "button";
btn.onclick = goToRedelivery;
btn.value = "再配達を依頼";
with (btn.style) {
backgroundColor = "#dedede";
cursor = "pointer";
fontSize = "14px";
margin = "10px 0px 30px";
width = "100%";
}
history_table.parentNode.insertBefore(btn, history_table.nextSibling);
}
// Go to Redelivery page with tracking number as query
function goToRedelivery() {
var track_no = document.getElementsByName("trackingNo")[0].value;
window.location = "https://trackings.post.japanpost.jp/delivery/deli/" + "?number=" + track_no;
}
// Auto-fill Redelivery page
function redeliveryPage() {
var directiontype_radio = document.querySelector("input[name='directionType'][value='" + my_directiontype_code + "']");
var query = window.location.search.match(/number=(\d+)/);
var my_track_no = query ? query[1] : prompt("伝票番号を入力:");
var number_type;
if (my_track_no === null) {
my_track_no = "";
}
my_track_no = my_track_no.replace(/-/g, "");
switch (my_track_no.length) {
case 11:
case 12:
case 13:
// 追跡番号
number_type = "inputTrackNo";
document.querySelectorAll(".border_box.p_0")[2].style.display = "none";
document.querySelector(".form_layout.w_327.float_r").style.opacity = .3;
break;
case 6:
case 8:
// お知らせ番号
number_type = "inputNoticeNo";
document.querySelectorAll(".border_box.p_0")[1].style.display = "none";
document.querySelector(".form_layout.w_328.float_l").style.opacity = .3;
break;
case 0: // cancelled or no input
break;
default:
alert("番号の桁数が正しくありません");
my_track_no = "";
}
// Set form values
document.getElementsByName("postCode")[0].value = my_postcode;
document.getElementsByName(number_type)[0].value = my_track_no;
directiontype_radio.checked = true;
document.querySelector("h2.ttl_line").scrollIntoView();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment