SGDB-Extension/script.js

106 lines
2.9 KiB
JavaScript

if(typeof browser === "undefined")
var browser = chrome;
const url = window.location.href;
var appID;
if(url.includes('/app/')) {
const appIDMatch = url.match(/\/app\/(\d+)/);
appID = appIDMatch[1];
}
if(appID) {
const buttonContainer = document.querySelector(".apphub_OtherSiteInfo");
if(buttonContainer) {
const sgdb_button = document.createElement('a');
sgdb_button.href = `https://steamgriddb.com/steam/${appID}`;
sgdb_button.className = "btnv6_blue_hoverfade btn_medium";
sgdb_button.style.marginRight = "3px";
sgdb_button.style.marginLeft = "3px";
const span = document.createElement('span');
span.setAttribute("data-tooltip-text", "View on SteamGridDB");
const icon = document.createElement("img");
icon.className = "ico16";
icon.style.backgroundImage = "none";
icon.src = browser.runtime.getURL("icons/sgdb_16.png");
span.append(icon);
sgdb_button.append(span);
buttonContainer.prepend(sgdb_button);
}
}
var userID;
if(url.includes('/profiles/')) {
const userIDMatch = url.match(/\/profiles\/(\d+)/);
if(userIDMatch) {
userID = userIDMatch[1];
}
} else if(url.includes('/id/')) {
const div = document.getElementById('responsive_page_template_content');
if(div) {
const scriptElements = div.getElementsByTagName('script');
for(script of scriptElements) {
const content = script.textContent || script.innerText;
const userIDMatch = content.match(/"steamid":"(\d+)"/);
if(userIDMatch) {
userID = userIDMatch[1];
break;
}
}
}
}
if(userID) {
const headerActions = document.querySelector(".profile_header_actions");
if(headerActions) {
const dropdown = document.getElementById("profile_action_dropdown");
if(dropdown) {
const popup_menu = dropdown.getElementsByClassName("popup_menu");
const dropdown_button = document.createElement('a');
dropdown_button.href = `https://steamgriddb.com/profile/${userID}`;
dropdown_button.className = "popup_menu_item";
const icon = document.createElement("img");
icon.src = browser.runtime.getURL("icons/sgdb_16.png");
dropdown_button.append(icon);
dropdown_button.append("\u00A0 SteamGridDB Profile");
popup_menu[0].append(dropdown_button);
} else {
const profile_button = document.createElement('a');
profile_button.href = `https://steamgriddb.com/profile/${userID}`;
profile_button.className = "btn_profile_action btn_medium";
profile_button.style.marginRight = "3px";
profile_button.style.marginLeft = "3px";
const span = document.createElement('span');
span.setAttribute("data-tooltip-text", "View SteamGridDB profile");
const icon = document.createElement("img");
icon.className = "ico16";
icon.style.backgroundImage = "none";
icon.src = browser.runtime.getURL("icons/sgdb_16.png");
span.append(icon);
profile_button.append(span);
headerActions.append(profile_button);
}
}
}