Greasy Fork

Greasy Fork is available in English.

Keylol论坛显示Steam名称

在帖子楼层中显示Steam名称。

当前为 2020-10-10 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Keylol论坛显示Steam名称
// @namespace    http://tampermonkey.net/
// @version      1.3.1
// @description  在帖子楼层中显示Steam名称。
// @author       Android_KitKat
// @icon         https://keylol.com/favicon.ico
// @match        *://keylol.com/t*
// @match        *://keylol.com/forum.php?mod=viewthread&tid=*
// @grant        GM_xmlhttpRequest
// @connect      steamcommunity.com
// @connect      api.steampowered.com
// ==/UserScript==

(function() {
    'use strict';

    // 是否使用WebAPI来获取数据,true为启用,false为禁用。
    var webapi = false;
    // 请在此处填写你的Steam网页API密钥。
    // 注册链接: https://steamcommunity.com/dev/apikey
    var apikey = '';
    // 缓存过期时间,默认一小时。
    var expire = 1000 * 60 * 60;

    function AppendName(bar) {
        var friendlink = bar.querySelector('.steam_connect_user_bar_link_friend').href;
        var steamids = friendlink.split('friends/add/')[1];
        var cache = JSON.parse(localStorage.getItem('ksCache')) || {};
        if (cache[steamids] && (new Date().getTime() - cache[steamids].last) < expire) {
            SetBarName(bar, cache[steamids].name);
        } else {
            webapi ? AppendNameFromWebAPI(bar, steamids): AppendNameFromCommunity(bar, steamids);
        }
    }

    function AppendNameFromCommunity(bar, steamids) {
        GM_xmlhttpRequest({
            method: 'get',
            url: `https://steamcommunity.com/profiles/${steamids}/?xml=1`,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            timeout: 3e5,
            onload: function(res) {
                var data = res.responseXML;
                var name = data.querySelector('steamID').textContent;
                CacheName(steamids, name);
                SetBarName(bar, name);
            }
        });
    }

    function AppendNameFromWebAPI(bar, steamids) {
        GM_xmlhttpRequest({
            method: 'get',
            url: `https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=${apikey}&steamids=${steamids}`,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            timeout: 3e5,
            onload: function(res) {
                var data = JSON.parse(res.responseText);
                var name = data.response.players[0].personaname;
                CacheName(steamids, name);
                SetBarName(bar, name);
            }
        });
    }

    function CacheName(steamids, name) {
        var cache = JSON.parse(localStorage.getItem('ksCache')) || {};
        cache[steamids] = {'name': name, 'last': new Date().getTime()};
        localStorage.setItem('ksCache', JSON.stringify(cache));
    }

    function SetBarName(bar, name) {
        bar.insertBefore(document.createTextNode(name), bar.firstChild);
    }

    var bars = document.querySelectorAll('.steam_connect_user_bar');
    bars.forEach(AppendName);

    var postlist = document.getElementById("postlist");
    var observer = new MutationObserver(function(recs) {
        for(let rec of recs) {
            for(let node of rec.addedNodes) {
                if(node.querySelectorAll) {
                    let addedbars = node.querySelectorAll('.steam_connect_user_bar');
                    addedbars.forEach(AppendName);
                }
            }
        }
    });
    observer.observe(postlist, {childList: true, subtree: true});
})();