Greasy Fork is available in English.

Steam保存好友

点击保存好友可以把当前好友列表保存本地,点击管理列表后,点击选中非保存好友可以把所有不在列表的好友选中,然后可以手动删除,适用于频繁加陌生好友送礼物的

// ==UserScript==
// @name         Steam保存好友
// @namespace    http://tampermonkey.net/
// @version      2024-05-08-11
// @description  点击保存好友可以把当前好友列表保存本地,点击管理列表后,点击选中非保存好友可以把所有不在列表的好友选中,然后可以手动删除,适用于频繁加陌生好友送礼物的
// @author       YuoHira
// @license      MIT
// @match        https://steamcommunity.com/id/*/friends
// @icon         https://www.google.com/s2/favicons?sz=64&domain=steamcommunity.com
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==


(function () {
    'use strict';

    // 使用JavaScript代码来获取页面中所有目标元素的steamid

    // 获取所有目标元素
    const elements = document.querySelectorAll('.selectable.friend_block_v2');

    // 创建一个对象来存储每个元素的steamid
    const steamIds = {};

    // 遍历每个元素并记录其steamid
    elements.forEach(element => {
        const steamId = element.getAttribute('data-steamid');
        steamIds[steamId] = true;
    });

    // 输出所有记录的steamid
    console.log(Object.keys(steamIds));

    // Your code here...
    // 创建一个新按钮元素
    const newButton = document.createElement('button');
    newButton.textContent = '保存好友列表';
    newButton.className = 'profile_friends manage_link btnv6_blue_hoverfade btn_medium'; // 设置新按钮的样式类名
    newButton.style.width = '120px'; // 设置新按钮的宽度
    newButton.style.height = '30px'; // 设置新按钮的高度
    newButton.style.textAlign = 'center'; // 设置新按钮的文字局中

    // 给保存好友列表的按钮添加事件,点击后读取所有steamid,保存数据到本地
    newButton.addEventListener('click', function () {
        GM_setValue('savedSteamIds', Object.keys(steamIds));
        alert('好友列表已保存到本地');
    });

    // 找到目标按钮的父元素
    const targetButton = document.getElementById('manage_friends_control');

    // 在目标按钮的前面插入新按钮
    targetButton.parentNode.insertBefore(newButton, targetButton);

    // 创建一个新按钮元素
    const readButton = document.createElement('button');
    readButton.textContent = '选中非保存好友';
    readButton.className = 'profile_friends manage_link btnv6_blue_hoverfade btn_medium'; // 设置新按钮的样式类名
    readButton.style.width = '120px'; // 设置新按钮的宽度
    readButton.style.height = '30px'; // 设置新按钮的高度
    readButton.style.textAlign = 'center'; // 设置新按钮的文字局中

    readButton.addEventListener('click', function () {
        const savedSteamIds = GM_getValue('savedSteamIds');
        // 使用JavaScript代码来找到页面中所有目标按钮,并将复选框设为勾选状态

        // 获取所有目标按钮
        const buttons = document.querySelectorAll('.selectable.friend_block_v2');

        // 遍历每个按钮并将复选框设为勾选状态
        buttons.forEach(button => {
            const checkbox = button.querySelector('.select_friend_checkbox');
            const steamId = button.getAttribute('data-steamid');
            if (checkbox) {
                if (!savedSteamIds.includes(steamId)) {
                    checkbox.checked = true;
                } else {
                    checkbox.checked = false;
                }
            }
        });
    });

    // 在目标按钮的后面插入新按钮
    targetButton.parentNode.insertBefore(readButton, targetButton);

})();