Skip to content

Instantly share code, notes, and snippets.

@carlo-colombo
Created October 21, 2024 20:37
Show Gist options
  • Select an option

  • Save carlo-colombo/58e12f0b1ef3f11454a85c3c9609d03a to your computer and use it in GitHub Desktop.

Select an option

Save carlo-colombo/58e12f0b1ef3f11454a85c3c9609d03a to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Add LEGO Hyperlink to MOC Kit Header
// @namespace http://tampermonkey.net/
// @version 1.4
// @description Adds a LEGO hyperlink in smaller text on the next line after "MOC MXXXXX Parts Kit" within the specified header, linking to the corresponding LEGO product page.
// @author Emmet
// @match *://www.marstoy.net/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to reverse a string
function reverseString(str) {
return str.split('').reverse().join('');
}
// Target the specific <h1> element
let header = document.querySelector('h1.product-info__header_title.dj_skin_product_title');
if (header) {
let regex = /MOC M(\d{5}) Parts Kit/;
let match = header.innerHTML.match(regex);
if (match) {
let mocNumber = match[1];
let reversedNumber = reverseString(mocNumber);
let legoUrl = `https://www.lego.com/en-us/product/${reversedNumber}`;
// Create a new anchor (link) element
let link = document.createElement('a');
link.href = legoUrl;
link.innerHTML = 'Open product on Lego.com';
link.style.display = 'block'; // Ensures the link is on the next line
link.style.fontSize = 'smaller'; // Make the text smaller
link.style.marginTop = '5px'; // Add some spacing
link.target = '_blank'; // Open link in a new tab
// Append the link after the matched text
header.innerHTML = `MOC M${mocNumber} Parts Kit`;
header.appendChild(link);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment