显示网站ip

在所有网站右下角显示ip

目前为 2025-01-10 提交的版本。查看 最新版本

// ==UserScript==
// @name         显示网站ip
// @namespace    http://tampermonkey.net/
// @version      2025-01-11
// @description  在所有网站右下角显示ip
// @author       Rakiwine
// @match        *://*/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=chatgpt.com
// @grant        GM_getResourceText
// @grant        GM_addStyle
// @grant        GM_xmlhttpRequest
// @grant        GM_log
// @grant        GM_setValue
// @grant        GM_getValue
// @license MIT
// ==/UserScript==

(function() {
    'use strict';
    let button_style = {
                backgroundColor: 'rgba(0, 0, 0, 0.5)',// 黑色半透明
                color: 'white',// 文字颜色
                textShadow: '1px 1px 3px rgba(0, 0, 0, 0.7)', // 增加文字阴影,提升可读性
                border: 'none',// 去掉边框
                borderRadius: '16px',// 圆角
                height: '50px',// 高度
                padding: '0 20px',// 左右内边距
                fontSize: '16px',// 字体大小
                cursor: 'pointer',// 鼠标悬停时显示手型光标
                outline: 'none',// 去掉焦点时的轮廓
                position: 'fixed',// 固定定位
                bottom: '10px',// 距离顶部 10px
                right: '10px',// 距离右边 10px
                zIndex: '1000',// 确保按钮在最上层
                // pointerEvents: 'none',// 点击穿透
            }

    // 显示加载提示
    let buttonId = "ip_button_id_" + generateRandomID(10);
    let ip_button = document.createElement('button');
    ip_button.id = buttonId; // 设置按钮的 ID 为唯一标识符
    Object.assign(ip_button.style, button_style);
    ip_button.textContent = '加载中...';
    document.body.appendChild(ip_button);

    // 生成随机ID
    function generateRandomID(length) {
        const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_$';
        let result = '';

        for (let i = 0; i < length; i++) {
            result += characters.charAt(Math.floor(Math.random() * characters.length));
        }

        return result;
    }

    // 跨域 获取 IP 地址
    function getIP(domain) {
        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: 'GET',
                url: `https://dns.google/resolve?name=${domain}`,
                onload: function(response) {
                    const data = JSON.parse(response.responseText);

                    if(data.Comment){
                        const regex = /\b(?:\d{1,3}\.){3}\d{1,3}\b/g;
                        const matches = data.Comment.match(regex);

                        if (!matches || matches.length === 0) {
                            reject("失败74");
                        } else {
                            resolve(matches[0]);
                        }
                    }else {
                        reject("失败79");
                    }
                },
                onerror: function(error) {
                    reject("失败83");
                }
            });
        });
    }

    // 跨域 获取归属地
    function getLocation(ip) {
        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: 'GET',
                url: `https://qifu-api.baidubce.com/ip/geo/v1/district?ip=${ip}`,
                onload: function(response) {
                    try {
                        const data = JSON.parse(response.responseText);
                        if (data && data.data && data.data.country) {
                            resolve(`${data.data.country} | ${data.data.isp}`);
                        } else {
                            reject("失败101");
                        }
                    } catch (error) {
                        reject("失败104");
                    }
                },
                onerror: function(error) {
                    reject("失败108");
                }
            });
        });
    }

    let cachedIp = GM_getValue(location.origin + 'cachedIp') || "失败114";
    let cachedDistrict = GM_getValue(location.origin + 'cachedDistrict') || "失败115";

    getIP(location.origin).then(ip => {

        GM_setValue(location.origin + 'cachedIp', ip); // 更新缓存

        // 获取归属地信息
        getLocation(ip).then(district => {
            ip_button.textContent = `${ip} | ${district}`;

            GM_setValue(location.origin + 'cachedDistrict', district); // 更新缓存

            GM_log("127", location.origin,buttonId,ip,district)
        }).catch(error => {
            GM_log("129", location.origin,buttonId,ip,error)

            ip_button.textContent = `${ip} | ${cachedDistrict}`;
        });

    }).catch(error => {

        ip_button.textContent = `${cachedIp} | ${cachedDistrict}`;;

        GM_log("138", location.origin, error, cachedIp, cachedDistrict)
    });

    // Your code here...
})();