Cathay Award Search Fixer 2022

Un-Elevate Your Cathay Award Search 2022

As of 2022-08-23. See the latest version.

  1. // ==UserScript==
  2. // @name Cathay Award Search Fixer 2022
  3. // @name:zh-TW 國泰獎勵機票搜尋引擎修復神器 2022
  4. // @namespace jayliutw
  5. // @version 1.0
  6. // @description Un-Elevate Your Cathay Award Search 2022
  7. // @description:zh-TW 國泰航空 2022 里程獎勵機票搜尋引擎「反升級」套件
  8. // @author jayliutw
  9. // @match https://*.cathaypacific.com/cx/*/book-a-trip/redeem-flights/redeem-flight-awards.html*
  10. // @match https://book.cathaypacific.com/*
  11. // @grant none
  12. // @license GPL
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. function addCss(cssString) {
  19. var head = document.getElementsByTagName('head')[0];
  20. var newCss = document.createElement('style');
  21. newCss.type = "text/css";
  22. newCss.innerHTML = cssString;
  23. head.appendChild(newCss);
  24. }
  25.  
  26. var uef_from = localStorage.getItem("uef_from") || "HKG";
  27. var uef_to = localStorage.getItem("uef_to") || "TYO";
  28. var uef_date = localStorage.getItem("uef_date") || "20230801";
  29. var uef_adult = localStorage.getItem("uef_adult") || "1";
  30. var uef_child = localStorage.getItem("uef_child") || "0";
  31.  
  32. addCss (
  33. '.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%);} ' +
  34. '.unelevated_form.uef_collapsed { margin-left:-90%;} ' +
  35. '.unelevated_title {font-weight: 400; font-size: 17px; font-family: "GT Walsheim","Cathay Sans EN", CathaySans_Lt, sans-serif; color: #2d2d2d; margin: 5px;} '+
  36. '.unelevated_form label { display: inline-block; position: relative;} ' +
  37. '.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;} ' +
  38. '.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;} ' +
  39. '.unelevated_form input.uef_date { width:110px;} ' +
  40. '.unelevated_form button.uef_search { background-color: #2c4036; border: none; color: white; display: inline-block;vertical-align: top; margin: 5px; height: 45px; width: 110px;} ' +
  41. '.unelevated_sub a { font-family: Cathay Sans EN, CathaySans_Bd, sans-serif; font-size: 12px; margin: 5px; color: #0f748f; font-weight: bold;}' +
  42. 'a.uef_toggle, a.uef_toggle:hover { background: #367778; display: block; position: absolute; right: -1px; top: -5px; padding-top:5px; width: 30px; text-align: center; text-decoration: none; color: white !important; padding-bottom: 5px; }' +
  43. 'a.uef_toggle:after {content:\'«\'} .uef_collapsed a.uef_toggle:after {content : \'»\'} ' +
  44. 'body { padding-bottom: 220px !important; }'
  45. );
  46.  
  47. var lang = (navigator.language != "zh-TW") ? {
  48. "ec" : "HK",
  49. "el": "en",
  50. "search" : "Search",
  51. "coffee" : "Did this tool help you? Why not buy me a coffee!",
  52. "searching" : "Searching..."
  53. } : {
  54. "ec" : "TW",
  55. "el": "zh",
  56. "search" : "搜尋",
  57. "coffee" : "這工具有幫助到你嗎?歡迎請我喝杯咖啡呀!",
  58. "searching" : "請稍後..."
  59. };
  60.  
  61. var cx_json = {
  62. "awardType": "Standard",
  63. "brand": "CX",
  64. "cabinClass": "Y",
  65. "entryCountry": lang.ec,
  66. "entryLanguage": lang.el,
  67. "entryPoint": "https://www.cathaypacific.com/cx/" + lang.el + "_" + lang.ec + "/book-a-trip/redeem-flights/redeem-flight-awards.html",
  68. "errorUrl": "https://www.cathaypacific.com/cx/" + lang.el + "_" + lang.ec + "/book-a-trip/redeem-flights/redeem-flight-awards.html?recent_search=ow",
  69. "isFlexibleDate": false,
  70. "numAdult": 1,
  71. "numChild": 0,
  72. "promotionCode": "",
  73. "returnUrl": "https://www.cathaypacific.com/cx/" + lang.el + "_" + lang.ec + "/book-a-trip/redeem-flights/redeem-flight-awards.html?recent_search=ow",
  74. "segments": [
  75. {
  76. "departureDate": "20230801",
  77. "origin": "TPE",
  78. "destination": "TYO"
  79. }
  80. ]
  81. };
  82.  
  83. document.querySelector("body").insertAdjacentHTML("beforeend", "<div class='unelevated_form'>" +
  84. "<div class='unelevated_title'>Unelevated Award Search</div>" +
  85. "<label><span>From</span><input type='text' id='uef_from' name='uef_from' placeholder='TPE' value='" + uef_from + "'></label>" +
  86. "<label><span>To</span><input type='text' id='uef_to' name='uef_to' placeholder='TYO' value='" + uef_to + "'></label>" +
  87. "<label><span>Adults</span><input type='text' id='uef_adult' name='uef_adult' placeholder='Adults' value='" + uef_adult + "'></label>" +
  88. "<label><span>Children</span><input type='text' id='uef_child' name='uef_child' placeholder='Children' value='" + uef_child + "'></label>" +
  89. "<label><span>Date</span><input class='uef_date' id='uef_date' type='text' name='uef_date' placeholder='20221001' value='" + uef_date + "'></label>" +
  90. "<button class='uef_search'>" + lang.search + "</button>" +
  91. "<div class='unelevated_sub'><a href='https://jayliu.net/buymeacoffee' target='_blank'>" + lang.coffee + "</a></div>" +
  92. "<a href='javascript:void();' class='uef_toggle'></a>" +
  93. "</div>");
  94.  
  95. var cxhttp = new XMLHttpRequest();
  96. cxhttp.onload = function() {
  97. var response = JSON.parse(this.responseText);
  98. cxform(response.parameters);
  99. }
  100.  
  101. function cxsearch(string){
  102. cxhttp.withCredentials = true;
  103. cxhttp.open("POST", "https://api.cathaypacific.com/redibe/standardAward/create");
  104. cxhttp.setRequestHeader("Content-type", "application/json");
  105. cxhttp.send(string);
  106. }
  107.  
  108. function cxform(parameters) {
  109.  
  110. // Create a form dynamically
  111. var form = document.createElement("form");
  112. form.setAttribute("name", "cxform");
  113. form.setAttribute("method", "post");
  114. form.setAttribute("action", "https://book.cathaypacific.com/CathayPacificAwardV3/dyn/air/booking/availability");
  115.  
  116. for(var item in parameters) {
  117. var input = document.createElement("input");
  118. input.setAttribute("type", "hidden");
  119. input.setAttribute("name", item);
  120. input.setAttribute("value", parameters[item]);
  121. form.appendChild(input);
  122. }
  123.  
  124. document.getElementsByTagName("body")[0].appendChild(form);
  125. document.forms["cxform"].submit();
  126. }
  127.  
  128. document.querySelector(".uef_toggle").addEventListener("click",function(e){
  129. document.querySelector(".unelevated_form").classList.toggle("uef_collapsed")
  130.  
  131. });
  132.  
  133. document.querySelector(".uef_search").addEventListener("click",function(e){
  134. uef_from = document.querySelector("#uef_from").value;
  135. uef_to = document.querySelector("#uef_to").value;
  136. uef_date = document.querySelector("#uef_date").value;
  137. uef_adult = document.querySelector("#uef_adult").value;
  138. uef_child = document.querySelector("#uef_child").value;
  139. localStorage.setItem("uef_from",uef_from)
  140. localStorage.setItem("uef_to",uef_to)
  141. localStorage.setItem("uef_date",uef_date)
  142. localStorage.setItem("uef_adult",uef_adult)
  143. localStorage.setItem("uef_child",uef_child)
  144. cx_json.numAdult = uef_adult;
  145. cx_json.numChild = uef_child;
  146. cx_json.segments[0].departureDate = uef_date;
  147. cx_json.segments[0].origin = uef_from.toUpperCase();
  148. cx_json.segments[0].destination = uef_to.toUpperCase();
  149. var cx_string = JSON.stringify(cx_json);
  150. document.querySelector(".uef_search").innerText = lang.searching;
  151. cxsearch(cx_string);
  152. })
  153. })();