Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link to Billing #73

Open
wants to merge 7 commits into
base: release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/content_scripts/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'web_resources/fuse.min.js', // Fuzzy Search Library
'web_resources/HangarXPLOR.js',
// 'web_resources/HangarXPLOR.Debug.js', // Uncomment to debug third party hangar HTML
'web_resources/HangarXPLOR.Billing.js',
'web_resources/HangarXPLOR.Download.js',
'web_resources/HangarXPLOR.Toggle.js',
'web_resources/HangarXPLOR.Templates.js',
Expand Down Expand Up @@ -79,7 +80,7 @@
case 'storage.local.clear.request': chrome.storage.local.clear(function() { window.postMessage({ type: "storage.clear.response", callbackIndex: event.data.callbackIndex }, "*") }); break;
}
});

var loadScript = function() {
if (scripts.length == 0) return;

Expand Down
1 change: 1 addition & 0 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"web_accessible_resources": [
"web_resources/shims.chrome.storage.js",
"web_resources/fuse.min.js",
"web_resources/HangarXPLOR.Billing.js",
"web_resources/HangarXPLOR.BulkUI.js",
"web_resources/HangarXPLOR.Button.js",
"web_resources/HangarXPLOR.Components.js",
Expand Down
141 changes: 141 additions & 0 deletions src/web_resources/HangarXPLOR.Billing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
var HangarXPLOR = window.HangarXPLOR || {};
HangarXPLOR.Billing = window.HangarXPLOR.Billing || {};

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

HangarXPLOR.Billing.LoadData = function() {
HangarXPLOR.Billing.parseBills(1);
};

HangarXPLOR.Billing.getBill = function(name, date, isUpgraded = false, upgradedName = null) {
date = HangarXPLOR.Billing.parseDate(date);
const bills = HangarXPLOR.Billing.Bills[date];

if(typeof bills === 'undefined') {
return [];
}

name = name.toLowerCase();

let findBill = function(bills, name) {
let bill = null;

for(let i = 0; i < bills.length; i++) {
if(bills[i]['name'].includes(name)) {
bill = bills[i];
}
}

if(bill === null) {
let fuse = new Fuse(bills, {keys: ['name']})
let results = fuse.search(name);

if(results.length > 0) {
bill = results[0];
}
}

return bill;
}

let bill = findBill(bills, name);

// --- if this item was upgraded then also try to find the bill for the upgrade
if(bill !== null && isUpgraded) {
upgradedName = upgradedName.toLowerCase();
let upgraded_bill = null;
for(let key in HangarXPLOR.Billing.UpgradeBills) {
if(key >= date) { // exploits lexicographic ordering - might be a bit hacky but avoids creating new Date objects
upgraded_bill = findBill(HangarXPLOR.Billing.UpgradeBills[key], upgradedName);
if(upgraded_bill !== null) {
let splitted = upgraded_bill.name.split(/( to )/gm);
if(splitted.length === 2 && splitted[1].indexOf(upgradedName)) {
break;
}
}
}
}

if(upgraded_bill !== null) {
return [bill, upgraded_bill];
}
}

if(bill !== null) {
return [bill];
}

return [];
}

HangarXPLOR.Billing.parseDate = function(date) {
date = new Date(date);
date.setHours(0);
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(HangarXPLOR.Billing.Keys.length === 0 || HangarXPLOR.Billing.Keys[HangarXPLOR.Billing.Keys.length - 1] !== order_date) {
HangarXPLOR.Billing.Keys.push(order_date);
}


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

let bill = {
'name': order_name.toLowerCase(),
'slug': order_slug,
'date': order_date,
};

if(bill.name.startsWith('upgrade') || bill.name.startsWith('ship upgrades ')) {
if(!(order_date in HangarXPLOR.Billing.UpgradeBills)) {
HangarXPLOR.Billing.UpgradeBills[order_date] = [];
}

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

HangarXPLOR.Billing.Bills[order_date].push(bill);
}


}
}

if(order_items.length == page_size) {
HangarXPLOR.Billing.parseBills(pageNo + 1);
} else {
HangarXPLOR.Billing.Keys.reverse();
HangarXPLOR.Billing.DoneLoading = true;
}
}
}
request.send();
}
11 changes: 9 additions & 2 deletions src/web_resources/HangarXPLOR.LoadPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ HangarXPLOR.LoadPage = function(pageNo)

var url = '/account/pledges?page=' + pageNo;

if (pageNo == 1 && document.location.search == '?page=1')
if(pageNo == 1) {
// TODO should this be place somewhere else? - it just needs to be done before we process the items
// so that we have the billing data already in order to link to it
HangarXPLOR.Billing.LoadData();
Comment on lines +14 to +16
Copy link
Member

@peter-dolkens peter-dolkens Feb 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're only linking to billing data - perhaps the lookup could be executed on-click rather than pre-loaded?

Might help with larger hangars/billing history

EDIT: I see in ProcessItem you may already be doing this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. The look up happens when you click the arrow button to open up each item. The look ups should also always be fairly fast unless the user has a lot of bills on one specific day.


if(document.location.search == '?page=1') {
return HangarXPLOR.ProcessPage(document.body, pageNo);

}
}

HangarXPLOR.Log('Loading', url);

var $page = $('<div>');
Expand Down
51 changes: 51 additions & 0 deletions src/web_resources/HangarXPLOR.ProcessItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,57 @@ HangarXPLOR.ProcessItem = function()
else
this.displayName = titleParts[0] + ' - ' + titleParts[1] + ' (' + this.pledgeId + ')';

// --- invoice button
// 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 existthen the button has already been added
if(element.hasOwnProperty('billsLoaded')) {
return;
}

let bills;
if(element.isUpgraded) {
bills = HangarXPLOR.Billing.getBill(
element.originalName.replace(/^(?:Standalone Ship|Package|Combo|Add-ons|Extras) - /, ''), element.getElementsByClassName('date-col')[0].lastChild.data.trim(),
element.isUpgraded, (element.shipName || titleParts[1])
);
} else {
bills = HangarXPLOR.Billing.getBill(titleParts[1], element.getElementsByClassName('date-col')[0].lastChild.data.trim())
}

element.billsLoaded = true;
if(bills.length > 0) {

$('.items', element)
.prepend(
'<a class="shadow-button trans-02s trans-color" data-order-slug="' + bills[0].slug + '"><span class="label js-label trans-02s">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>'
);

if(bills.length === 2) {
$('.items', element)
.prepend(
'<a class="shadow-button trans-02s trans-color" data-order-slug="' + bills[1].slug + '"><span class="label js-label trans-02s">Upgrade 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')
.focus();
});
}
};

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

if ($.cookie('noPrefix') == 'true')
Expand Down