Advertisement
dhillonsh

Rogue Stock Notifier

May 28th, 2020
2,392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Rogue Fitness Stock Notifier
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.1
  5. // @description  Notifier for Rogue Fitness Stock
  6. // @author       dhillonsh
  7. // @match        https://www.roguefitness.com/*
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12.     'use strict';
  13.  
  14.     var browserAlert = function () {
  15.         var oldTitle = document.title;
  16.         var msg = "New!";
  17.         var timeoutId;
  18.         var blink = function() { document.title = document.title == msg ? ' ' : msg; };
  19.         var clear = function() {
  20.             clearInterval(timeoutId);
  21.             document.title = oldTitle;
  22.             window.onmousemove = null;
  23.             timeoutId = null;
  24.         };
  25.         return function () {
  26.             if (!timeoutId) {
  27.                 timeoutId = setInterval(blink, 1000);
  28.                 window.onmousemove = clear;
  29.             }
  30.         };
  31.     };
  32.  
  33.     var productNotification = function () {
  34.         console.log("Product available.");
  35.         browserAlert();
  36.  
  37.         var player = document.createElement('audio');
  38.         player.src = 'https://notificationsounds.com/soundfiles/a86c450b76fb8c371afead6410d55534/file-sounds-1108-slow-spring-board.mp3';
  39.         player.preload = 'auto';
  40.         player.play()
  41.  
  42.         var xhttp = new XMLHttpRequest();
  43.         xhttp.open("GET", "http://localhost:8080/?title=" + document.title, true);
  44.         xhttp.send();
  45.     };
  46.  
  47.     var refresh = function() {
  48.         var seconds = 30;
  49.         setTimeout(function() {
  50.             var params = new URLSearchParams(window.location.search);
  51.             var new_url = window.location.href.split('?')[0];
  52.             if(params.get("iteration") === null) {
  53.                 new_url = new_url + "?iteration=1";
  54.             } else {
  55.                 new_url = new_url + "?iteration=" + (parseInt(params.get("iteration")) + 1);
  56.             }
  57.             window.location.replace(new_url);
  58.         }, seconds * 1000);
  59.     };
  60.  
  61.     // IMPORTANT!
  62.     // This is the list of product IDs that you want to monitor, you get this by using inspect element on a product page.
  63.     // In this case, we are monitoring for the ohio powerlift bar and the matador dip bar
  64.     // Note: You must have the webpages open on your browser where these two products are listed
  65.     // Note: The only thing you need to add below is the product ID, you do not need the comments and the URL that i added (that's just to make it clear to me what the product id is for)
  66.     var productIDs = [
  67.         "60093", // ohio powerlift bar https://www.roguefitness.com/rogue-45lb-ohio-powerlift-bar-cerakote
  68.         "3527", // matador dip bar https://www.roguefitness.com/rogue-infinity-matador
  69.         "85743", // 15 lb fleck = https://www.roguefitness.com/rogue-fleck-plates
  70.         "85745", // 25 lb fleck = https://www.roguefitness.com/rogue-fleck-plates
  71.         "85747", // 35 lb fleck = https://www.roguefitness.com/rogue-fleck-plates
  72.         "85749" // 45 lb fleck = https://www.roguefitness.com/rogue-fleck-plates
  73.     ];
  74.  
  75.     productIDs.forEach((productID) => {
  76.         var product = document.getElementsByClassName("product-purchase-wrapper-" + productID);
  77.  
  78.         if(product.length === 0) return;
  79.  
  80.         console.log("Found product on page: " + productID);
  81.  
  82.         product = product[0]
  83.  
  84.         if(product.getElementsByClassName("color-swatch-wrapper").length === 1) {
  85.             // Product has multiple color choices
  86.             var swatch_container = product.getElementsByClassName("color-swatch-container")[0];
  87.             var swatches = swatch_container.querySelectorAll('div[id^="swatch-main-' + productID + '"]');
  88.  
  89.             for(var swatch_index=0; swatch_index < swatches.length; swatch_index++) {
  90.                 var swatch = swatches[swatch_index];
  91.                 if(!swatch.classList.contains("swatch-oos")) {
  92.                     productNotification();
  93.                     break;
  94.                 }
  95.             };
  96.  
  97.             refresh();
  98.  
  99.        } else if(product.getElementsByClassName("bin-stock-availability").length === 1) {
  100.            console.log("Product not available.");
  101.  
  102.            refresh();
  103.        } else {
  104.             productNotification();
  105.        }
  106.     });
  107.  
  108. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement