Frederick888 / PlayStation Store - I'm hella cheap

// ==UserScript==
// @name         PlayStation Store - I'm hella cheap
// @namespace    https://onee3.org
// @version      0.30
// @description  Add free items to your cart!
// @author       Frederick Zhang
// @include      *://store.playstation.com/*
// @grant        none
// @license      GPL-3.0-or-later
// ==/UserScript==

function cart_count() {
    return parseInt(document.querySelector('.icon-nav__count.icon-nav__count--cart').innerHTML);
}

(function() {
    'use strict';

    let add_magic_button = setInterval(() => {
        const magic_button_html = `
<style>
  .sub-nav-right .icon-nav:nth-of-type(2){
  display: block;
  margin-right: 1.563rem;
}
.sub-nav-right .icon-nav .icon-magic {
  background: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB3aWR0aD0iMTAwIiBoZWlnaHQ9IjEwMCIgdmlld0JveD0iMCAwIDEwMCAxMDAiPjxnPjxnIHN0eWxlPSJmaWxsOiMwMDAwMDAiPjxzdmcgZmlsbD0iIzAwMDAwMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgdmVyc2lvbj0iMS4xIiB4PSIwcHgiIHk9IjBweCIgdmlld0JveD0iMCAwIDUxMiA1MTIiIHN0eWxlPSJlbmFibGUtYmFja2dyb3VuZDpuZXcgMCAwIDUxMiA1MTI7IiB4bWw6c3BhY2U9InByZXNlcnZlIj48Zz48Zz48cG9seWdvbiBwb2ludHM9Ijc2Ljk2OSwzNTguNTgxIDEzNC40NjMsNDE2LjA1IDMwOS41OTEsMjQwLjkwOCAyNTIuMTI3LDE4My40NDYgICAiPjwvcG9seWdvbj48cGF0aCBkPSJNMzEzLjg2NCwxNDQuNzM4Yy0zLjA3LTMuMDctNy4xNjMtNC43NjEtMTEuNTIyLTQuNzYxYy00LjM1OCwwLT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I1LDAuMDM1QzQ2NS45MDIsMjM5LjE5Niw0NjUuODk5LDIzOS4xOTYsNDY1Ljg5NiwyMzkuMTk2eiI+PC9wYXRoPjxwYXRoIGQ9Ik0yOTUuNjQ3LDgyLjA2N2wtNS4zMjMsMTYuMzM2bDEzLjg2MS0xMC4xYzEuNDAzLTEuMDIzLDMuMzA4LTEuMDIzLDQuNzEyLDBsMTMuODQ1LDEwLjA4OGwtNS4zMzUtMTYuMzIgICAgYy0wLjU0MS0xLjY1NCwwLjA1LTMuNDY5LDEuNDYyLTQuNDg3bDEzLjgzMi05Ljk3OGwtMTcuMDkzLDAuMDM4Yy0wLjAwMywwLTAuMDA2LDAtMC4wMDksMGMtMS43MzYsMC0zLjI3My0xLjExOS0zLjgwNy0yLjc3MiAgICBsLTUuMjYtMTYuMzA1bC01LjI4LDE2LjMxYy0wLjUzNCwxLjY1LTIuMDcxLDIuNzY4LTMuODA2LDIuNzY4Yy0wLjAwMywwLTAuMDA2LDAtMC4wMDksMGwtMTcuMDY2LTAuMDM4bDEzLjgxNCw5Ljk3OSAgICBDMjk1LjU5Niw3OC42MDQsMjk2LjE4Niw4MC40MTUsMjk1LjY0Nyw4Mi4wNjd6Ij48L3BhdGg+PHBhdGggZD0iTTQ1NS40ODQsMTA2LjA1MWMwLTE1LjAzLTEyLjIyOS0yNy4yNTktMjcuMjYtMjcuMjU5cy0yNy4yNiwxMi4yMjktMjcuMjYsMjcuMjU5YzAsMTUuMDI4LDEyLjIyOSwyNy4yNTQsMjcuMjYsMjcuMjU0ICAgIFM0NTUuNDg0LDEyMS4wNzksNDU1LjQ4NCwxMDYuMDUxeiI+PC9wYXRoPjwvZz48L2c+PC9zdmc+PC9nPjwvZz48L3N2Zz4K") center no-repeat;
  background-size: auto;
  background-size: 100%;
  width: 20px;
  height: 22px;
  display: inline-block;
}
</style>
<a class="icon-nav" id="fill-cart">
  <span class="icon-magic"></span>
</a>
`;
        let icon_wishlist = document.querySelector('.sub-nav-right.right-nav-buttons > .icon-nav[data-ember-action]');
        if (icon_wishlist) {
            icon_wishlist.insertAdjacentHTML('beforebegin', magic_button_html);
            clearInterval(add_magic_button);

            let fill_cart_button = document.querySelector('#fill-cart');
            fill_cart_button.onclick = () => {
                let cart_buttons = Array.prototype.slice.call(document.querySelectorAll('button.grid-cell__add-to-cart-button')).filter(btn => {
                    let discounted_price_tag = btn.parentNode.querySelectorAll('.price-display__price--is-plus');
                    if (discounted_price_tag.length === 0) {
                        discounted_price_tag = btn.parentNode.querySelectorAll('.price-display__price');
                    }
                    let price = discounted_price_tag[0].innerHTML;
                    return price.indexOf('¥') == -1 && price.indexOf('$') == -1 && price.indexOf('€') == -1;
                }).sort((a, b) => {
                    const platform_ranks = {
                        'PS4®': 1,
                        'PS Vita': 2,
                        'PS3®': 3,
                    };
                    let platform_a = a.parentNode.parentNode.querySelector('.grid-cell__left-detail--detail-1').innerHTML;
                    let platform_b = b.parentNode.parentNode.querySelector('.grid-cell__left-detail--detail-1').innerHTML;
                    let rank_a = platform_ranks[platform_a] === undefined ? 99 : platform_ranks[platform_a];
                    let rank_b = platform_ranks[platform_b] === undefined ? 99 : platform_ranks[platform_b];
                    return rank_a - rank_b;
                });
                let to_process = 0;
                let to_add_cart = true;

                if (cart_buttons.length === 0) {
                    alert('Opps! It seems that there are no free items available in this page.');
                    return;
                }

                let main_loop = setInterval(() => {
                    if (to_add_cart) {
                        cart_buttons[to_process].click();
                        to_add_cart = false;
                    } else {
                        let continue_button = document.querySelectorAll('.added-to-cart__button-group > .modal-dialog__button--primary');
                        let error_button = document.querySelectorAll('.add-to-cart-error-dialog__button');
                        if (continue_button.length > 0) {
                            continue_button[0].click();
                            ++to_process;
                            to_add_cart = true;
                        } else if (error_button.length > 0) {
                            error_button[0].click();
                            ++to_process;
                            to_add_cart = true;
                        }
                    }
                    if (to_process === cart_buttons.length || cart_count() === 10) {
                        clearInterval(main_loop);
                        if (to_process === cart_buttons.length) {
                            alert('Hooray! All free items in this pages should have been added to your cart!');
                        } else {
                            alert('Cart is full, my dude! Go check out first :)');
                        }
                    }
                });
            };
        }
    }, 2000);
})();