Vinted search followed brands

Creates search URLs for your followed brands (50 brands max per search)

当前为 2023-05-18 提交的版本,查看 最新版本

// ==UserScript==
// @name         Vinted search followed brands
// @namespace    https://greasyfork.org/en/users/230946-odinbrood
// @version      1.0
// @description  Creates search URLs for your followed brands (50 brands max per search)
// @include      http*://*vinted.*/member/personalization/brands/followed
// @grant        none
// @author       OdinBrood
// @license      GPLv3
// ==/UserScript==

(function() {
  'use strict';

  // Extract and return the contents of elements with the given CSS class
  function extractFollowNames() {
    // Find all <a> elements with the CSS class 'follow__name'
    const elements = document.querySelectorAll('.follow__name');
    const contents = [];

    elements.forEach(element => {
      contents.push(element.textContent.trim());
    });
    return contents;
  }

  // Call the api for the brand id
  async function getBrandIdByTitle(brandName) {
    const apiUrl = `https://www.vinted.nl/api/v2/brands?keyword=${encodeURIComponent(brandName).replace(/-/g, ' ')}`;

    try {
      const response = await fetch(apiUrl);
      const json = await response.json();

      const brands = json.brands;
      for (let i = 0; i < brands.length; i++) {
        if (brands[i].title === brandName) {
          console.log("✓ " + brandName + " = " + brands[i].id);
          return brands[i].id;
        }
      }
    } catch (error) {
      console.error('Error occurred while fetching the API:', error);
    }
    console.log("✗ " + brandName);
    return null;
  }

  // Find all IDs of brands
  async function getBrandIds(followNames) {
    const brandIds = [];

    for (const brandName of followNames) {
      const brandId = await getBrandIdByTitle(brandName);
      if (brandId !== null) {
        brandIds.push(brandId);
      }
    }
    brandIds.sort((a, b) => a - b);

    return brandIds;
  }

  // Construct search url from brand IDs
  function constructSearchURL(ids) {
    const baseURL = "/catalog?";
    const brandIds = ids.map(id => `brand_id[]=${id}`).join("&");

    return `${baseURL}${brandIds}`;
  }

  // Add button for search url
  function addButton(url, range) {
    const anchor = document.createElement('a');
    const br = document.createElement("br");

    anchor.textContent = range;
    anchor.href = url;
    anchor.target = "_blank";

    const bodyContent = document.querySelector('.body-content__sidebar');
    if (bodyContent) {
      bodyContent.appendChild(br);
      bodyContent.appendChild(br);
      bodyContent.appendChild(anchor);
    }
  }


  // Handle first button click event
  function handleButtonClick() {

    // Create the loading indicator element
    var loadingIndicator = document.createElement('div');
    loadingIndicator.textContent = 'Loading... (this can take a while if you follow many brands)';
    loadingIndicator.style.position = 'fixed';
    loadingIndicator.style.top = '50%';
    loadingIndicator.style.left = '50%';
    loadingIndicator.style.transform = 'translate(-50%, -50%)';
    loadingIndicator.style.backgroundColor = 'rgba(255, 255, 255)';
    loadingIndicator.style.padding = '10px';
    loadingIndicator.style.borderRadius = '5px';
    loadingIndicator.style.zIndex = '9999';
    document.body.appendChild(loadingIndicator);

    // Start logic
    const followNames = extractFollowNames();
    getBrandIds(followNames)
        .then(brandIds => {
          for (var i = 0; i < brandIds.length; i += 50) {
            const searchURL = constructSearchURL(brandIds.slice(i, i + 50));
            var range = (i + 1) + ' to ' + Math.min(i + 50, brandIds.length);
            addButton(searchURL, range);
          }
          loadingIndicator.remove();
        })
        .catch(error => {
          console.error('Error occurred:', error);
        });
  }

  // Create first button
  const button = document.createElement('button');
  button.textContent = 'Generate search url(s) for all brands you follow';
  button.addEventListener('click', handleButtonClick);

  // Append the button to the div with the class 'body-content__sidebar'
  window.addEventListener('load', function() {
    const bodyContent = document.querySelector('.body-content__sidebar');
    if (bodyContent) {
      bodyContent.appendChild(button);
    }
  });

})();