diff --git a/js/battlenet/battlenet.js b/js/battlenet/battlenet.js new file mode 100644 index 0000000..73eddf0 --- /dev/null +++ b/js/battlenet/battlenet.js @@ -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, + }); + }); +} diff --git a/js/helpers.js b/js/helpers.js index 75fb441..e7735a6 100644 --- a/js/helpers.js +++ b/js/helpers.js @@ -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++) { @@ -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; } @@ -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); @@ -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'); } /** diff --git a/js/index.js b/js/index.js index e5a224c..3fd93bf 100644 --- a/js/index.js +++ b/js/index.js @@ -48,5 +48,6 @@ function handleMutationsInit() { handlePSDealsMutations() handleXboxMutations(); handleNintendoARMutations() + handleBattleNetMutations(); }, 1000); } \ No newline at end of file diff --git a/manifest.json b/manifest.json index ae57322..a4c22ae 100644 --- a/manifest.json +++ b/manifest.json @@ -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" @@ -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" }