BoardGameGeek Link Tabletop Simulator Steam Workshop

Adds a link to BoardGameGeek to search for the game on the Steam Tabletop Simulator workshop

// ==UserScript==
// @name        BoardGameGeek Link Tabletop Simulator Steam Workshop
// @match       https://boardgamegeek.com/boardgame/*
// @grant       none
// @version     0.0.1
// @icon        https://icons.duckduckgo.com/ip2/boardgamegeek.com.ico
// @description Adds a link to BoardGameGeek to search for the game on the Steam Tabletop Simulator workshop
// @license     MIT
// @namespace https://greasyfork.org/users/1257939
// ==/UserScript==

(async () => {
	'use strict';

	// Select the toolbar actions element
	const toolbarActions = document.querySelector(".toolbar-actions");

	// Get the game title from the header
	const gameTitleElement = document.querySelector("div.game-header-title:nth-child(2) > div:nth-child(2) > h1:nth-child(1) > a:nth-child(1)");
	const gameTitle = gameTitleElement.textContent.trim();

	// Create a div element and an anchor element
	const div = document.createElement("div");
	const element = document.createElement("a");

	// Set attributes and classes for the elements
	element.target = "_blank";
	div.classList.add("toolbar-action", "ng-scope");
	element.classList.add("buy-btn", "btn", "btn-sm", "ng-scope");

	// Set the href and text content of the anchor element
	element.href = `https://steamcommunity.com/workshop/browse/?appid=286160&searchtext=${encodeURIComponent(gameTitle).replaceAll('%20', '+', 'g')}`;
	element.textContent = "🎲 Search Tabletop Simulator";

	// Append the anchor element to the div element
	div.append(element);

	// Prepend the div element to the toolbar actions
	toolbarActions.prepend(div);
})();