用简单的 JavaScript 自动添加 LinkedIn 人脉

published_on
5 分钟阅读
--- 阅读量

引言

在求职市场上,LinkedIn 几乎已经不可或缺。个人主页上光鲜的 500+ 联系人远不只是一个数字,更是人脉能力和职业潜力的象征。但说实话,我们 LinkedIn 上超过 99% 的联系人,对我们的职业发展并没有真正的影响。

我常常发现自己在 LinkedIn 上待得太久,漫无目的地添加网上的陌生人,只为了让联系人数往上涨。可是,这种没完没了地手动点击“Connect”(加为联系人)的做法,真的已经是最高效的了吗?

是时候换个更聪明的办法了:让我们把它自动化。

思路

在 LinkedIn 的“My Network”(我的人脉)页面上,我们可以检查 HTML 代码,看看这些“Connect”按钮是怎么定义的:

<button
  aria-label="Invite [UserName] to connect"
  id="[UserID]"
  class="artdeco-button artdeco-button--2 artdeco-button--secondary ember-view"
>
  {' '}
  <li-icon 
    aria-hidden="true" 
    type="connect" 
    class="artdeco-button__icon" 
    size="small"
  >
    <svg
      xmlns="http://www.w3.org/2000/svg"
      viewBox="0 0 16 16"
      data-supported-dps="16x16"
      fill="currentColor"
      class="mercado-match"
      width="16"
      height="16"
      focusable="false"
    >
      <path d="[SVG data]"></path>
    </svg>
  </li-icon>
  <span class="artdeco-button__text">Connect</span>
</button>

整个按钮的代码可能有点多了。我们只需要足够的信息,能把这个按钮和无关的元素区分开就行。在这个例子里,只看包含按钮文字的那个 span 就足够了:

<span class="artdeco-button__text">Connect</span>

由于这个 span 位于按钮内部,点击 span 就等于点击按钮。对我们来说,只需要找出所有这样的 span 元素并逐个点击即可。

此外,为了不被识别成机器人,我们可以在点击之前先把按钮滚动到可视区域内,并在两次点击之间加上随机的时间间隔。

有了这些背景知识,脚本需要做什么就很清楚了:

  1. 找出可能的按钮:

    • 使用 querySelectorAll 找到所有具有目标属性的元素。
      • 元素的类型应为 span。
      • 元素应带有 class "artdeco-button__text"。
  2. 核对按钮文字:

    • 使用 textContent.trim 获取元素的文本内容。
      • 元素的文本内容应为 Connect。
  3. 执行点击:

    • 元素一旦通过上述检查,就是我们要操作的目标按钮。
    • 使用 window.scrollTo 将目标元素滚动到可视区域内。
    • 使用 Math.random 和 setTimeout 等待一段随机的时间。
    • 调用 click() 方法点击按钮。

代码

// Function to scroll an element to the middle of the screen
function scrollToMiddle(element, delay) {
    return new Promise(resolve => {
        // Get the bounding rectangle of the element
        const bounding = element.getBoundingClientRect();

        // Calculate the position to scroll to (element's position - half of the viewport height)
        const scrollY = window.scrollY + bounding.top - (window.innerHeight / 2) + (bounding.height / 2);

        // Scroll to the calculated position
        window.scrollTo({ top: scrollY, behavior: 'smooth' });

        // Wait for the specified delay, then click the element
        setTimeout(() => {
            element.click();
            resolve();
        }, delay);
    });
}

// Function to process all elements
async function processElements() {
    // Select all span elements with the specified class
    const spanElements = document.querySelectorAll('span.artdeco-button__text');

    // Iterate over the NodeList
    for (let span of spanElements) {
        // Check if the content of the span is 'Connect'
        if (span.textContent.trim() === 'Connect') {
            // Generate a random delay from 1000 ms to 1500 ms (or from 1 second to 1.5 seconds)
            // Feel free to tailor the numbers to your own needs
            const delay = Math.floor(Math.random() * (500)) + 1000;

            // Scroll the element to the middle of the screen and click it with the delay
            await scrollToMiddle(span, delay);
        }
    }
}

// Call the function to start the process
processElements();

使用方法

  1. 启动浏览器:

    • 在电脑上打开 Chrome 或 Firefox。
  2. 进入 LinkedIn:

    • 打开 LinkedIn 的“My Network”页面。
  3. 打开开发者控制台:

    • 在 Chrome 中:
      • Windows 上按 Ctrl + Shift + J 或 F12。
      • Mac 上按 Cmd + Option + J。
    • 在 Firefox 中:
      • Windows 上按 Ctrl + Shift + I 或 F12。
      • Mac 上按 Cmd + Option + I。
  4. 进入 Console 标签页:

    • 开发者控制台打开后,找到并点击“Console”(控制台)标签页。
  5. 运行脚本:

    • 将上面提供的脚本粘贴到控制台中。
    • 按下 Enter 执行。
  6. 坐看自动化:

    • 往后一靠,看着脚本替你干活吧!