// ==UserScript==
// @name Cathay Award Search Fixer
// @name:zh-TW 國泰獎勵機票搜尋引擎修復神器
// @namespace jayliutw
// @version 0.3
// @description Un-Elevate Your Cathay Award Search
// @description:zh-TW 國泰航空里程獎勵機票搜尋引擎「反升級」套件
// @author jayliutw
// @match https://*.cathaypacific.com/cx/*/book-a-trip/redeem-flights/redeem-flight-awards.html*
// @match https://book.cathaypacific.com/*
// @grant none
// @license GPL
// ==/UserScript==
(function() {
'use strict';
function addCss(cssString) {
var head = document.getElementsByTagName('head')[0];
var newCss = document.createElement('style');
newCss.type = "text/css";
newCss.innerHTML = cssString;
head.appendChild(newCss);
}
var uef_from = localStorage.getItem("uef_from") || "HKG";
var uef_to = localStorage.getItem("uef_to") || "TYO";
var uef_date = localStorage.getItem("uef_date") || "20230801";
var uef_adult = localStorage.getItem("uef_adult") || "1";
var uef_child = localStorage.getItem("uef_child") || "0";
addCss (
'.unelevated_form { transition: margin-left 0.7s ease-out;z-index: 1000000; font-family: "GT Walsheim","Cathay Sans EN", CathaySans_Rg, sans-serif; position: fixed; bottom: 30px; left: 5%; width:90%; border: 1px solid #bcbec0; background: #f7f6f0; padding: 8px; border-top: 5px solid #367778; box-shadow: 0px 0px 7px rgb(0 0 0 / 20%);} ' +
'.unelevated_form.uef_collapsed { margin-left:-90%;} ' +
'.unelevated_title {font-weight: 400; font-size: 17px; font-family: "GT Walsheim","Cathay Sans EN", CathaySans_Lt, sans-serif; color: #2d2d2d; margin: 5px;} '+
'.unelevated_form label { display: inline-block; position: relative;} ' +
'.unelevated_form label span { position: absolute; top: 3px; left: 10px; color: #66686a; font-family: Cathay Sans EN, CathaySans_Rg, sans-serif; line-height: 30px; font-size: 10px;} ' +
'.unelevated_form input { font-family: Cathay Sans EN, CathaySans_Lt, sans-serif; padding: 19px 5px 5px 5px; border-radius: 0px; border: 1px solid #bcbec0; display: inline-block; margin: 5px; height: 45px; width: 50px;} ' +
'.unelevated_form input.uef_date { width:110px;} ' +
'.unelevated_form button.uef_search { background-color: #2c4036; border: none; color: white; display: inline-block;vertical-align: top; margin: 5px; height: 45px; width: 110px;} ' +
'.unelevated_sub a { font-family: Cathay Sans EN, CathaySans_Bd, sans-serif; font-size: 12px; margin: 5px; color: #0f748f; font-weight: bold;}' +
'a.uef_toggle, a.uef_toggle:hover { background: #367778; display: block; position: absolute; right: -1px; top: 0; width: 30px; text-align: center; text-decoration: none; color: white !important; padding-bottom: 5px; }' +
'a.uef_toggle:after {content:\'«\'} .uef_collapsed a.uef_toggle:after {content : \'»\'} ' +
'body { padding-bottom: 220px !important; }'
);
var cx_json = {
"awardType": "Standard",
"brand": "CX",
"cabinClass": "Y",
"entryCountry": "HK",
"entryLanguage": "en",
"entryPoint": "https://www.cathaypacific.com/cx/en_HK/book-a-trip/redeem-flights/redeem-flight-awards.html",
"errorUrl": "https://www.cathaypacific.com/cx/en_HK/book-a-trip/redeem-flights/redeem-flight-awards.html?recent_search=ow",
"isFlexibleDate": false,
"numAdult": 1,
"numChild": 0,
"promotionCode": "",
"returnUrl": "https://www.cathaypacific.com/cx/en_HK/book-a-trip/redeem-flights/redeem-flight-awards.html?recent_search=ow",
"segments": [
{
"departureDate": "20230801",
"origin": "TPE",
"destination": "TYO"
}
]
};
document.querySelector("body").insertAdjacentHTML("beforeend", "<div class='unelevated_form'>" +
"<div class='unelevated_title'>Unelevated Award Search</div>" +
"<label><span>From</span><input type='text' id='uef_from' name='uef_from' placeholder='TPE' value='" + uef_from + "'></label>" +
"<label><span>To</span><input type='text' id='uef_to' name='uef_to' placeholder='TYO' value='" + uef_to + "'></label>" +
"<label><span>Adults</span><input type='text' id='uef_adult' name='uef_adult' placeholder='Adults' value='" + uef_adult + "'></label>" +
"<label><span>Children</span><input type='text' id='uef_child' name='uef_child' placeholder='Children' value='" + uef_child + "'></label>" +
"<label><span>Date</span><input class='uef_date' id='uef_date' type='text' name='uef_date' placeholder='20221001' value='" + uef_date + "'></label>" +
"<button class='uef_search'>Search</button>" +
"<div class='unelevated_sub'><a href='https://jayliu.net/buymeacoffee' target='_blank'>Did this tool help you? Buy me a coffee!</a></div>" +
"<a href='javascript:void();' class='uef_toggle'></a>" +
"</div>");
var cxhttp = new XMLHttpRequest();
cxhttp.onload = function() {
var response = JSON.parse(this.responseText);
cxform(response.parameters);
}
function cxsearch(string){
cxhttp.withCredentials = true;
cxhttp.open("POST", "https://api.cathaypacific.com/redibe/standardAward/create");
cxhttp.setRequestHeader("Content-type", "application/json");
cxhttp.send(string);
}
function cxform(parameters) {
// Create a form dynamically
var form = document.createElement("form");
form.setAttribute("name", "cxform");
form.setAttribute("method", "post");
form.setAttribute("action", "https://book.cathaypacific.com/CathayPacificAwardV3/dyn/air/booking/availability");
for(var item in parameters) {
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", item);
input.setAttribute("value", parameters[item]);
form.appendChild(input);
}
document.getElementsByTagName("body")[0].appendChild(form);
document.forms["cxform"].submit();
}
document.querySelector(".uef_toggle").addEventListener("click",function(e){
document.querySelector(".unelevated_form").classList.toggle("uef_collapsed")
});
document.querySelector(".uef_search").addEventListener("click",function(e){
uef_from = document.querySelector("#uef_from").value;
uef_to = document.querySelector("#uef_to").value;
uef_date = document.querySelector("#uef_date").value;
uef_adult = document.querySelector("#uef_adult").value;
uef_child = document.querySelector("#uef_child").value;
localStorage.setItem("uef_from",uef_from)
localStorage.setItem("uef_to",uef_to)
localStorage.setItem("uef_date",uef_date)
localStorage.setItem("uef_adult",uef_adult)
localStorage.setItem("uef_child",uef_child)
cx_json.numAdult = uef_adult;
cx_json.numChild = uef_child;
cx_json.segments[0].departureDate = uef_date;
cx_json.segments[0].origin = uef_from;
cx_json.segments[0].destination = uef_to;
var cx_string = JSON.stringify(cx_json);
document.querySelector(".uef_search").innerText = "Searching...";
cxsearch(cx_string);
})
})();