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

add BattleNet Support #8

Open
wants to merge 4 commits into
base: develop
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
108 changes: 108 additions & 0 deletions js/battlenet/battlenet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* Battle.net Web Store
*/

// us.shop.battle.net/es-mx/

function handleBattleNetMutations() {

if (someURL(['us.shop.battle.net'], hostname) && someURL(['/es-mx'], pathname)) {

if (someURL(['/product/'], pathname)) {
handleBattleNetMultiProductArg();
} else {
handleBattleNetAllGamesArg()
}
}
}






/**
* Tested on:
* https://us.shop.battle.net/es-mx/product/diablo_ii_resurrected?p=74831
* https://us.shop.battle.net/es-mx/product/overwatch?p=37693
*/
function handleBattleNetMultiProductArg() {
handleMutations("meka-price-label", "battlenet--hero", (game) => {
// https://stackoverflow.com/questions/62897456/access-a-shadow-root-element-using-document-queryselector


//console.log("DEMO11", game, "price");

var appRoot = game.shadowRoot;
var appRootPrice = appRoot.querySelector(
".meka-price-label--details__standard-price"
);
//console.log("DEMO111", "appRoot", appRoot, "appRootPrice", appRootPrice);


// Price
scrapper({
priceElement: appRootPrice,
eventElement: appRootPrice,
currency: "ARS",
showEmoji: true,
});
});
}

/**
* Tested on:
* https://us.shop.battle.net/es-mx
*/
function handleBattleNetAllGamesArg() {
// browsing-card-group__layout -> list of cards
// browsing-card-group__layout--card browsing-card ng-star-inserted -> one card
handleMutations(
".browsing-card-group__layout--card.browsing-card.ng-star-inserted",
"battlenet--hero",
(game) => {

var insidecard1 = game.querySelector("meka-browsing-card");
var insidecard1Super2 = insidecard1.querySelector("meka-price-label").shadowRoot
var insidecard1Super3 = insidecard1Super2.querySelector("span.meka-price-label--details__standard-price");
// Price
scrapper({
priceElement: insidecard1Super3,
eventElement: insidecard1Super3,
currency: "ARS",
showEmoji: true,
});
/*
*/
}
);
}






// replaced by handleBattleNetMultiProductArg() //
/**
* Tested on:
* https://us.shop.battle.net/es-mx/product/diablo-ii
*/
function handleBattleNetProductArg() {
handleMutations(".price-container", "epic--hero", (game) => {
// https://stackoverflow.com/questions/62897456/access-a-shadow-root-element-using-document-queryselector
var appRoot = document.querySelector("meka-price-label").shadowRoot;
var appRootPrice = appRoot.querySelector(
".meka-price-label--details__standard-price"
);

//console.log("DEMO4", game, "price", appRootPrice);
// Price
scrapper({
priceElement: appRootPrice,
eventElement: appRootPrice,
currency: "ARS",
showEmoji: true,
});
});
}
13 changes: 10 additions & 3 deletions js/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
function handleMutations(gamesSelector, className, callback) {
const games = document.querySelectorAll(gamesSelector);
//console.log("DEMO3",gamesSelector,games.length,games, className, callback)

if (games && games.length > 0) {
for (let i = 0; i < games.length; i++) {
Expand All @@ -26,15 +27,16 @@ function handleMutations(gamesSelector, className, callback) {
* @returns {number}
*/
function getNewPrice(originalPrice, taxes, currency = 'ARS') {
const exceptions = ['Free', 'FREE', 'Gratuito', 'Gratis', 'Gratis+', 'No disponible', '--'];
const exceptions = ['Free', 'FREE', 'Gratuito', 'Gratis', 'Gratis+', 'No disponible', '--','Juégalo gratis','Más información'];
const priceTextNaN = exceptions.some(exception => exception.toLowerCase() === originalPrice.toLowerCase());
const priceWithTaxes = (p) => (p + p * (taxes.ganancias + taxes.pais)).toFixed(2)
//console.log("DEMO5",originalPrice, taxes, currency,priceTextNaN,priceWithTaxes)

if (priceTextNaN) {
return 0;
}

const priceNumber = sanitizePricePunctuation(sanitizePriceSigns(originalPrice));
//console.log("DEMO",originalPrice,sanitizePriceSigns(originalPrice),priceNumber)
if (priceNumber === 0) {
return 0;
}
Expand Down Expand Up @@ -114,6 +116,8 @@ function replacePrice(priceElement, eventElement = priceElement, originalPrice,
*/
function scrapper({ priceElement, eventElement, currency, showEmoji }) {
if (priceElement) {

//console.log("DEMO2",priceElement, eventElement, currency, showEmoji)
const originalPrice = priceElement.textContent;
const newPrice = getNewPrice(originalPrice, tax, currency);
newPrice && replacePrice(priceElement, eventElement, originalPrice, newPrice, showEmoji);
Expand Down Expand Up @@ -171,8 +175,11 @@ function sanitizePriceSigns(price) {
* $ 1,222.43 +
* ARS$ 1,222.43 +
* ARS$1,222.43 +
* ARS790.00 -> ^\s*[a-zA-z]*?\$?\s?(\d+\W?\d+\W?\d+)\s?\+?
* Desde ARS790.00 -> ^\s*[a-zA-z\s]*?\$?\s?(\d+\W?\d+\W?\d+)\s?\+?
* Desde ARS566.67/mes -> ^\s*[a-zA-z\s]*?\$?\s?(\d+\W?\d+\W?\d+)\s?\+?[/a-zA-z\s]*
*/
return price.replace(/^\s*[a-zA-z]*?\$\s?(\d+\W?\d+\W?\d+)\s?\+?/gi, '$1');
return price.replace(/^\s*[a-zA-z\s]*?\$?\s?(\d+\W?\d+\W?\d+)\s?\+?[/a-zA-z\s]*/gi, '$1');
}

/**
Expand Down
1 change: 1 addition & 0 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ function handleMutationsInit() {
handlePSDealsMutations()
handleXboxMutations();
handleNintendoARMutations()
handleBattleNetMutations();
}, 1000);
}
7 changes: 5 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"*://*.nintendo.com.ar/*",
"*://*.epicgames.com/store/*",
"*://*.xbdeals.net/ar-store*",
"*://*.psdeals.net/ar-store*"
"*://*.psdeals.net/ar-store*",
"*://*.shop.battle.net/es-mx/*",
"*://*.shop.battle.net/es-mx*"
],
"css": [
"css/styles.css"
Expand All @@ -31,7 +33,8 @@
"js/nintendo/nintendo.js",
"js/epic/epic.js",
"js/deals/xbdeals.js",
"js/deals/psdeals.js"
"js/deals/psdeals.js",
"js/battlenet/battlenet.js"
],
"run_at": "document_idle"
}
Expand Down