[GC] UL Trophy ordering

Order trophies by gold/silver/bronze in every userlookup and also shows the CSS code used in the browser's console

目前为 2023-12-08 提交的版本。查看 最新版本

// ==UserScript==
// @name         [GC] UL Trophy ordering
// @version      1.0
// @description  Order trophies by gold/silver/bronze in every userlookup and also shows the CSS code used in the browser's console
// @author       Berna
// @match        https://www.grundos.cafe/userlookup/?user=*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=grundos.cafe
// @license      MIT
// @namespace https://greasyfork.org/users/1230396
// ==/UserScript==

function extractUrlEndAndApplyCSS() {
  // Select all elements with class "ul__gametrophy"
  var elements = document.querySelectorAll('.ul__gametrophy');

  // Initialize an object to track the CSS rules for each order
  var cssRulesByOrder = {};

  // Iterate through each element
  elements.forEach(function (element) {
    // Extract the last class
    var lastClass = element.classList[element.classList.length - 1];

    // Find the img element
    var imgElement = element.querySelector('img');

    // If img element is found
    if (imgElement) {
      // Extract the src attribute
      var srcAttribute = imgElement.getAttribute('src');

      // Extract the URL end
      var urlEnd = srcAttribute.substring(srcAttribute.lastIndexOf('/') + 1);

      // Extract the order from the URL end
      var orderMatch = urlEnd.match(/_(\d+)\.gif/);
      var order = orderMatch ? orderMatch[1] : '';

      // Initialize CSS rule array for the order if not already present
      if (!cssRulesByOrder[order]) {
        cssRulesByOrder[order] = [];
      }

      // Add the CSS rule to the array for the order
      cssRulesByOrder[order].push('.' + lastClass + ',');
    }
  });

  // Create a style element
  var styleElement = document.createElement('style');

  // Append the style element to the head of the document
  document.head.appendChild(styleElement);

  // Iterate through each order and append the CSS rules to the style element
  for (var order in cssRulesByOrder) {
    var cssRules = cssRulesByOrder[order].join(' ');
    var cssRuleText = cssRules.slice(0, -1) + ' { order: ' + order + '; }';

    // Log the CSS rule to the console
    console.log(cssRuleText);

    // Append the CSS rule to the style element
    styleElement.textContent += cssRuleText;
  }
}

// Call the function
extractUrlEndAndApplyCSS();