Greasy Fork is available in English.
在帖子楼层中显示Steam名称。
当前为
// ==UserScript==
// @name Keylol论坛显示Steam名称
// @namespace http://tampermonkey.net/
// @version 1.2.0
// @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 * 10;
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.getElementsByTagName('steamID')[0].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.getElementsByClassName('steam_connect_user_bar');
for (var bar of bars) {
var friendlink = bar.getElementsByClassName('steam_connect_user_bar_link_friend')[0].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);
}
}
})();