Steam 家庭共享检测(图标提示)

检测Steam游戏是否支持家庭共享,并在游戏标题后面显示图标,鼠标悬浮显示提示

目前為 2024-12-09 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Steam 家庭共享检测(图标提示)
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  检测Steam游戏是否支持家庭共享,并在游戏标题后面显示图标,鼠标悬浮显示提示
// @author       你
// @license      MIT
// @match        https://store.steampowered.com/app/*
// @grant        none
// ==/UserScript==

/*
MIT License

Copyright (c) 2024 你

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

(function() {
    'use strict';

    // 创建图标元素
    const iconElement = document.createElement('span');
    iconElement.style.fontSize = '24px'; // 设置图标大小
    iconElement.style.marginLeft = '10px'; // 设置与标题的间距

    // 监听 DOM 变动,直到找到游戏标题元素
    const observer = new MutationObserver(function(mutations) {
        const gameTitleElement = document.querySelector('.apphub_AppName');
        
        // 如果找到了游戏标题元素,则执行插入操作
        if (gameTitleElement) {
            // 查找是否有包含“家庭共享”的链接
            const familySharingLink = Array.from(document.querySelectorAll('a')).find(link =>
                link.textContent.includes('家庭共享') || link.textContent.includes('Family Sharing')
            );

            // 如果找到了“家庭共享”链接
            if (familySharingLink) {
                // 模拟点击该链接并检测跳转 URL
                const originalHref = familySharingLink.href;
                const isValidLink = originalHref && originalHref.startsWith("https://store.steampowered.com/");

                // 根据跳转 URL 判断是否支持家庭共享
                if (isValidLink) {
                    iconElement.textContent = '✔';  // 显示绿色√
                    iconElement.style.color = 'green';  // 设置为绿色
                    iconElement.title = '支持家庭共享';  // 鼠标悬浮提示
                } else {
                    iconElement.textContent = '❌';  // 显示红色×
                    iconElement.style.color = 'red';  // 设置为红色
                    iconElement.title = '不支持家庭共享';  // 鼠标悬浮提示
                }
            } else {
                // 如果没有找到“家庭共享”链接,认为不支持家庭共享
                iconElement.textContent = '❌';  // 显示红色×
                iconElement.style.color = 'red';  // 设置为红色
                iconElement.title = '不支持家庭共享';  // 鼠标悬浮提示
            }

            // 将图标插入到游戏标题后面
            gameTitleElement.appendChild(iconElement);

            // 停止观察,避免不必要的重复操作
            observer.disconnect();
        }
    });

    // 配置观察器以监听 DOM 变动
    observer.observe(document.body, { childList: true, subtree: true });
})();