See LinkedIn Profile Views

10/20/2023

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        See LinkedIn Profile Views
// @namespace   Violentmonkey Scripts
// @match       https://www.linkedin.com/analytics/profile-views/
// @grant       none
// @version     1.1
// @author      Atreides_
// @license     MIT
// @description 10/20/2023
// ==/UserScript==

// Ensure DOM is fully loaded before executing script
window.addEventListener('DOMContentLoaded', function () {

    // Function to serialize document to string
    function serializeDocument() {
        return new XMLSerializer().serializeToString(document);
    }

    // Function to extract profiles from a line of serialized document
    function extractProfiles(line) {
        let profiles = [];
        try {
            let res = JSON.parse(line)["included"] ?? null;
            if (res !== null) {
                for (let item of res) {
                    if (item["$type"] === "com.linkedin.voyager.identity.shared.MiniProfile") {
                        profiles.push(item);
                    }
                }
            }
        } catch (e) {
            console.error(e);
        }
        return profiles;
    }

    // Function to construct HTML for a profile
    function constructProfileHTML(profile) {
        let photoUrl = profile["picture"]["rootUrl"] +
            profile["picture"]["artifacts"].filter(a => a.width == 200)[0]["fileIdentifyingUrlPathSegment"];

        return `
            <div style="display:flex; flex-direction:row; margin-bottom: 10px;">
                <div style="display:flex;">
                    <img style="min-width: 52px; max-width: 52px" src="${photoUrl}">
                </div>
                <div style="display:flex flex-direction:column; margin-left: 12px;">
                    <strong><a href="https://linkedin.com/in/${profile.publicIdentifier}"><h4>${profile.firstName} ${profile.lastName}</h4></a></strong>
                    <i>${profile.occupation}</i>
                </div>
            </div>`;
    }

    // Main function to process document and display profiles
    function displayProfiles() {
        let docStr = serializeDocument();
        let stalkersHTML = "<u><h3>PROFILE VIEWERS</h3></u><br>";

        for (let line of docStr.split('\n')) {
            if (line.search("firstName") != 0 && line.search("lastName") != 0) {
                let profiles = extractProfiles(line);
                for (let profile of profiles) {
                    stalkersHTML += constructProfileHTML(profile);
                }
            }
        }

        let list = document.createElement("div");
        list.style.padding = "18px";
        list.style.backgroundColor = "lightgray";
        list.style.borderRadius = "5px";
        list.style.color = "black";
        list.style.fontFamily = ["Comic Sans", "Sans-Serif"];
        list.style.display = "flex";
        list.style.flexDirection = "column";
        list.innerHTML = stalkersHTML;

        console.log(list);

        let el = document.getElementById("ember37");
        if (el) {
            el.appendChild(list);
        } else {
            console.error('Element with id "ember37" not found.');
        }
    }

    // Execute main function
    displayProfiles();

});