知乎用戶頁展示用戶ID

在知乎的用戶頁的用戶名下方顯示一個用戶IDa

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         知乎用戶頁展示用戶ID
// @namespace    show_zhihu_user_id
// @version      0.1
// @description  在知乎的用戶頁的用戶名下方顯示一個用戶IDa
// @author       EvilRecluse
// @match        *://www.zhihu.com/people/*
// @match        *://www.zhihu.com/org/*
// @grant        none
// @create    2020-10-15
// @lastmodified  2020-10-16
// ==/UserScript==

(function() {
    'use strict';

    // 页面的<script>中有json的用户信息
    var html_info = document.getElementById('js-initialData').text;
    html_info = html_info.replace(/\\u[\s\S]{4}/g, "/");
    html_info = JSON.parse(html_info);
    var _id = Object.values(html_info.initialState.entities.users)[0].id;
    // 添加頁面元素
    document.getElementsByClassName("ProfileHeader-title")[0].insertAdjacentHTML(
        'beforeend',
        '</br><button id="truely_id_btn" class="VoteButton VoteButton--up">ID:<span>'+ _id +'</span></button>');

    // 為顯示ID的按鈕添加點擊複製功能
    document.getElementById('truely_id_btn').addEventListener(
        'click',
        function (){
            var input = document.createElement('input');
            input.setAttribute('id', 'for_copy_id');
            input.setAttribute('value', _id);
            document.body.appendChild(input);
            document.getElementById('for_copy_id').select();
            input.setSelectionRange(0, 9999);
            if (document.execCommand('copy')){
                document.execCommand('copy');
                document.getElementById('truely_id_btn').setAttribute("class","Button Button--grey Button--withIcon Button--withLabel");
            }else{
                console.log('fail');}
            input.remove();});

})();