Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Best Buy (Saved Items)
- // @namespace akito
- // @version 1.0.0
- // @description Auto-presses drops when button changes to Add
- // @author akito#9528 on Discord
- // @match https://www.bestbuy.com/site/customer/lists/manage/saveditems
- // @run-at document-start
- // @grant none
- // ==/UserScript==
- // Version Changelog
- // 1.0.0 - Initial Release for saved items script
- const title = "Best Buy (Saved Items) by akito#9528"
- const whitelist = ["3060", "3070", "3080", "3090", "6800", "6900", "5600x", "5800x", "5900x", "5950x"];
- // keyword whitelist, edit this if you want
- async function addObserver(cartButton, description, banner) {
- let flipped = cartButton.innerText.includes("Please"); // check whether ever included "Please"
- const MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
- let observer = new MutationObserver(function(mutations, observer) {
- if(cartButton.innerText.includes("Add") && flipped) {
- // Not sure whether I should open the cart or do something else lol
- cartButton.click(); // click button when please changes to add
- window.open("https://www.bestbuy.com/checkout/r/fast-track");
- // play sound for notification purposes
- const audio = new Audio("https://proxy.notificationsounds.com/notification-sounds/definite-555/download/file-sounds-1085-definite.mp3");
- audio.play();
- banner.innerHTML = `${title} | Successfully added [${description}] to cart!`;
- observer.disconnect();
- } else if(cartButton.innerText.includes("Please") && flipped === false) {
- flipped = true; // flip when please detected
- }
- });
- observer.observe(cartButton, {
- attributes: true,
- characterData: true,
- childList: true,
- subtree: true,
- attributeOldValue: true,
- characterDataOldValue: true
- });
- }
- // Filters products and sends to addObserver
- async function automate(banner) {
- const savedButtons = document.getElementsByClassName("add-to-cart-button");
- const savedDescs = document.getElementsByClassName("img-responsive");
- // Filter button descriptions to only those that are in the whitelist [O(n^2) sigh]
- // Length mismatch because of stray buttons being added but just ignore them.
- banner.innerHTML = `${title} | Filtering items: ${savedButtons.length} buttons and ${savedDescs.length} descriptions found`;
- await new Promise(r => setTimeout(r, 1000)); // wait so people can actually read this
- banner.innerHTML = `${title} | Adding individual mutation observers ...`;
- let numValid = 0; // number of products with mutation observers attached
- for(let productIndex = 0; productIndex < savedDescs.length; productIndex++) {
- // check whether product contains whitelist keyword
- let validProduct = false;
- const description = savedDescs[productIndex].getAttribute("alt");
- for(const keyword of whitelist) {
- if(description.includes(keyword)) {
- validProduct = true;
- numValid++;
- break;
- }
- } // then skip product if not whitelisted
- if(validProduct === false) {
- continue;
- }
- // actual attachment of mutation observers
- addObserver(savedButtons[productIndex], description, banner);
- }
- banner.innerHTML = `${title} | Finished, ${numValid} of ${savedDescs.length} products had observers attached.`;
- }
- // main function body
- (function() {
- 'use strict'; // necessary?
- // initialize bottom status banner
- let banner = document.createElement("div");
- banner.style.position = "fixed"; banner.style.bottom = "0px"; banner.style.zIndex = 100;
- banner.style.width = "100%"; banner.style.padding = "4px"; banner.style.textAlign = "center";
- banner.style.backgroundImage = "linear-gradient(to right, coral, crimson, coral, crimson)";
- banner.innerHTML = `${title} | <button id="begin">[ Press after scrolling and all products loaded ]</button>`;
- // Mutation Observer to detect HTML changes to the "Add to Cart" / "Please Wait" button
- // The script waits until the button switches from "Please Wait" back to "Add to Cart" (keep in mind you have to press the first Add button)
- // At that point, it clicks the button to add the item to the cart, plays a fancy notification sound, and opens a new window with the cart.
- // [ Note: untested on actual drops, only manually tested by manually changing the HTML ]
- document.addEventListener("DOMContentLoaded", function(event) {
- document.body.append(banner);
- document.getElementById("begin").onclick = function() { automate(banner); };
- }); // wait for document body to load
- }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement