SteamGifts Region Helper

12/13/2024, 9:41:45 PM

// ==UserScript==
// @name        SteamGifts Region Helper
// @namespace   Violentmonkey Scripts
// @match       https://www.steamgifts.com/giveaway/*/region-restrictions*
// @match       https://www.steamgifts.com/giveaways/new
// @grant       none
// @version     1.3.1
// @author      Lex
// @description 12/13/2024, 9:41:45 PM
// @license MIT
// ==/UserScript==


function regionRestrictionsPage() {
  const countryCodes = $("p.table__column__heading")
    .map((_, el) => $(el).text())
    .map((_, el) => el.match(/\((\w{2})\)/)?.[1])
    .get()
    .filter(code => code !== null)
    .join(" ");
  const textBox = $("<input>", {
    type: "text", value: countryCodes
  });
  $(".page__heading").first().after(textBox);
}

const inputTextBox = $("<input>", {
  type: "text", style: "margin-bottom: 0.5em",
  placeholder: "Enter regions here like US, GB, CA"
});

function getCountryCodesInput() {
  const uppercaseCodes = inputTextBox.val().toUpperCase();
  inputTextBox.val(uppercaseCodes);
  const arr = uppercaseCodes.split(/[\s,]+/).filter(Boolean);
  const sgCodes = getSGCountryCodes();

  const difference = (new Set(arr)).difference(new Set(sgCodes.keys()));
  if (difference.size) {
    alert("Some of your input country codes were not found in Steam Gifts!\n"
          + "Please fix this and try again.\nInvalid country codes: "
          + [...difference].join(", "));
    return [];
  }
  return arr;
}

var _sgCountryCodes;
function getSGCountryCodes() {
  if (_sgCountryCodes)
    return _sgCountryCodes
  const validCodes = new Map();
  $("div[data-input='country_item_string'] > div").each(function(){
    const name = $(this).data("name");
    const code = name.substr(name.length-2);
    validCodes.set(code, $(this));
  })
  _sgCountryCodes = validCodes;
  return validCodes;
}

function addRegions() {
  const sgCodes = getSGCountryCodes();
  getCountryCodesInput().forEach(code => {
    const regionElement = sgCodes.get(code);
    if (!regionElement.hasClass('is-selected'))
      regionElement.click();
  })
}

function removeRegions() {
  const sgCodes = getSGCountryCodes();
  getCountryCodesInput().forEach(code => {
    const regionElement = sgCodes.get(code);
    if (regionElement.hasClass('is-selected'))
      regionElement.click();
  })
}

function newGiveawayPage() {
  const container = $("<div>", {
    style: "display: block; width: 100%; margin-top: 0.5em;",
    class: "is-hidden"
  });
  container.append(inputTextBox);
  const applyButton = $("<button>", {
    type: "button",
    text: "Add Regions",
    style: "border: 1px solid black; padding: 1px 3px; border-radius: 3px"
  }).on("click", addRegions);
  const removeButton = $("<button>", {
    type: "button",
    text: "Remove Regions",
    style: "margin-left: 1.5em; border: 1px solid black; padding: 1px 3px; border-radius: 3px"
  }).on("click", removeRegions);
  container.append(applyButton);
  container.append(removeButton);

  const parentElement = $(".form_list[data-input='country_item_string']").first();
  parentElement.after(container);

  // add onclick handlers to the Yes/No buttons
  const checkboxContainer = $("div:has(> input[name=region_restricted])").first();
  const yesBox = checkboxContainer.find("div[data-checkbox-value=1]");
  yesBox.on("click", function() {
    $(this).closest(".form__row").find(".is-hidden").removeClass("is-hidden").addClass("is-shown");
  })
  const noBox = checkboxContainer.find("div[data-checkbox-value=0]");
  noBox.on("click", function() {
    $(this).closest(".form__row").find(".is-shown").removeClass("is-shown").addClass("is-hidden");
  })
}


if (window.location.href.includes("region-restrictions")) {
  regionRestrictionsPage();
} else {
  newGiveawayPage();
}