Skip to content

Commit

Permalink
make XMLHttpRequest async
Browse files Browse the repository at this point in the history
minor fixes
  • Loading branch information
jan-krueger committed Feb 15, 2021
1 parent ced0841 commit 7f701a5
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 60 deletions.
95 changes: 47 additions & 48 deletions src/web_resources/HangarXPLOR.Billing.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,15 @@ var HangarXPLOR = window.HangarXPLOR || {};
HangarXPLOR.Billing = window.HangarXPLOR.Billing || {};

HangarXPLOR.Billing.Bills = [];
HangarXPLOR.Billing.DoneLoading = false;

HangarXPLOR.Billing.LoadData = function() {
let page = 1;
let page_size = 100; // 100 is max

let continue_parsing = true;
while(continue_parsing) {

// --- request page
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", 'https://robertsspaceindustries.com/account/billing?page=' + page + '&pagesize=' + page_size + '&storefront=2', false ); // false for synchronous request
xmlHttp.send( null );

// --- parse html & get the list of orders on that site
var doc = new DOMParser().parseFromString(xmlHttp.responseText, "text/html");
let order_items = doc.getElementsByClassName('orders-item')[0].children;

// --- go through each order and extract the name and the slug
for(let i = 0; i < order_items.length; i++) {

let order_name = order_items[i].getElementsByClassName('billing-summary')[0].children[0].children[1].children[0].firstChild.nodeValue;
let order_slug = order_items[i].querySelector('[data-order-slug]');
let order_date = HangarXPLOR.Billing.parseDate( new Date(order_items[i].getElementsByClassName('col date')[0].lastChild.data.trim()));

// --- if the order slug is null then no voice exists (e.g. cancelled, payment failed, etc.)
if(order_slug !== null) {
order_slug = order_slug.dataset.orderSlug;

if(!(order_date in HangarXPLOR.Billing.Bills)) {
HangarXPLOR.Billing.Bills[order_date] = [];
}

HangarXPLOR.Billing.Bills[order_date].push({
'name': order_name.toLowerCase(),
'slug': order_slug,
'date': order_date,
});
}

}

if(order_items.length == page_size) {
page++;
} else {
continue_parsing = false;
}
}

HangarXPLOR.Billing.parseBills(1);
};

HangarXPLOR.Billing.getBill = function(name, element) {
let date = element.getElementsByClassName('date-col')[0].lastChild.data.trim();
const bills = HangarXPLOR.Billing.Bills[this.parseDate(date)];
const bills = HangarXPLOR.Billing.Bills[HangarXPLOR.Billing.parseDate(date)];

if(typeof bills === 'undefined') {
return null;
Expand All @@ -79,4 +35,47 @@ HangarXPLOR.Billing.parseDate = function(date) {
date.setMinutes(0);

return date.toISOString().split('T')[0];
}
}

HangarXPLOR.Billing.parseBills = function(pageNo) {
let page_size = 100; // <-- 100 is probably max
var request = new XMLHttpRequest();
request.open('GET', 'https://robertsspaceindustries.com/account/billing?page=' + pageNo + '&pagesize=' + page_size + '&order_status=O,C ', true);
request.onload = function() {
if (request.readyState === 4 && request.status === 200) {
// --- parse html & get the list of orders on that site
var doc = new DOMParser().parseFromString(request.responseText, "text/html");
let order_items = doc.getElementsByClassName('orders-item')[0].children;

// --- go through each order and extract the name and the slug
for(let i = 0; i < order_items.length; i++) {

let order_name = order_items[i].getElementsByClassName('billing-summary')[0].children[0].children[1].children[0].firstChild.nodeValue;
let order_slug = order_items[i].querySelector('[data-order-slug]');
let order_date = HangarXPLOR.Billing.parseDate( new Date(order_items[i].getElementsByClassName('col date')[0].lastChild.data.trim()));

// --- if the order slug is null then no voice exists (e.g. cancelled, payment failed, etc.)
if(order_slug !== null) {
order_slug = order_slug.dataset.orderSlug;

if(!(order_date in HangarXPLOR.Billing.Bills)) {
HangarXPLOR.Billing.Bills[order_date] = [];
}

HangarXPLOR.Billing.Bills[order_date].push({
'name': order_name.toLowerCase(),
'slug': order_slug,
'date': order_date,
});
}
}

if(order_items.length == page_size) {
HangarXPLOR.Billing.parseBills(pageNo + 1);
} else {
HangarXPLOR.Billing.DoneLoading = true;
}
}
}
request.send();
}
43 changes: 31 additions & 12 deletions src/web_resources/HangarXPLOR.ProcessItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,37 @@ HangarXPLOR.ProcessItem = function()
this.displayName = titleParts[0] + ' - ' + titleParts[1] + ' (' + this.pledgeId + ')';

// --- invoice button
let slug = HangarXPLOR.Billing.getBill(titleParts[1], this);
if(slug !== null) {
$('.items', this)
.prepend(
'<a class="shadow-button trans-02s trans-color more__button-print js-print-invoice" data-order-slug="' + slug.slug + '"><span class="label js-label trans-02s">Retrieve invoice</span><span class="icon trans-02s"><span class="effect trans-opacity trans-03s"></span></span><span class="left-section"></span><span class="right-section"></span></a>'
);
$('[data-order-slug]', this)
.click(event => {
let slug = event.currentTarget.dataset.orderSlug;
window.open('https://robertsspaceindustries.com/account/billing/order/' + slug, 'Bill', 'scrollbars=yes,resizable=yes,top=0,left=0,width=640,height=360,toolbar=no');
});
}
// Note: We load and add the button when the user clicks the arrow to expand the item because we have to async
// load the billing data since async XMLHttpRequest are deprecated.
let arrow_element = $('a.arrow.js-expand-arrow', this)[0];
let element = this;

arrow_element.onclick = function() {
// --- if we haven't loaded all bills yet, we ignore this
if(!HangarXPLOR.Billing.DoneLoading) {
return;
}

// --- if this property exist then the button has already been added
if(element.hasOwnProperty('billSlug')) {
return;
}

let slug = HangarXPLOR.Billing.getBill(titleParts[1], element);
element.billSlug = slug;
if(slug !== null) {
$('.items', element)
.prepend(
'<a class="shadow-button trans-02s trans-color more__button-print js-print-invoice" data-order-slug="' + slug.slug + '"><span class="label js-label trans-02s">Retrieve invoice</span><span class="icon trans-02s"><span class="effect trans-opacity trans-03s"></span></span><span class="left-section"></span><span class="right-section"></span></a>'
);
$('[data-order-slug]', element)
.click(event => {
let slug = event.currentTarget.dataset.orderSlug;
window.open('https://robertsspaceindustries.com/account/billing/order/' + slug, 'Bill', 'scrollbars=yes,resizable=yes,top=0,left=0,width=640,height=360,toolbar=no');
});
}
};


this.sortName = this.displayName.replace(/^.*? - (.*)$/, '$1');

Expand Down

0 comments on commit 7f701a5

Please sign in to comment.