Greasy Fork

Greasy Fork is available in English.

Boss直聘职位排序

A Tampermonkey script using jQuery

当前为 2024-03-05 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Boss直聘职位排序
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  A Tampermonkey script using jQuery
// @author       You
// @match        https://www.zhipin.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
  'use strict';
  try {

    const realXHR = window.XMLHttpRequest;
    window.XMLHttpRequest = function () {
      const xhr = new realXHR();

      const oldOpen = xhr.open;
      xhr.open = function (method, url, async, user, password) {
        this.addEventListener('readystatechange', function () {
          // if (this.readyState === 1) {
          //     console.log('XHR in progress with readyState ', this.readyState);
          // }
        }, false);
        oldOpen.apply(this, arguments);
      };

      xhr.addEventListener('load', function () {
        if (this.status === 200 && (this.responseURL.includes('/list.json?') || this.responseURL.includes('/joblist.json?'))) {
          console.log('XHR finished with status ', xhr.responseText);
          createScript('https://code.jquery.com/ui/1.12.1/jquery-ui.min.js').then(res => {
            createStyleLink()
            createRootDom()
            createTable(JSON.parse(xhr.responseText).zpData.jobList)
          })
        }
      }, false);

      return xhr;
    };


    function createScript(href) {
      return new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = href
        document.head.appendChild(script)
        script.onload = () => {
          resolve(true)
        }
        script.onerror = () => {
          reject(false)
        }
      })

    }

    function createTable(data) {
      // 创建新的 DOM 元素
      var newElement = $('<div>', {
        class: 'my-custom-element',
      });

      // 创建一个表格
      const list = data.sort((a, b) => {
        // 判断是否有最后修改时间
        if (!a.lastModifyTime) {
          return 1
        }
        return new Date(b.lastModifyTime) - new Date(a.lastModifyTime)
      }).map((_, index) => {
        return $('<tr>', {
          html: `<td>${index}</td><td>${_.brandName}</td><td>${_.jobName}</td><td>${_.salaryDesc}</td><td>${_.jobExperience}</td><td>${_.bossName}</td><td>${_.bossTitle}</td><td>${_.lastModifyTime ? new Date(_.lastModifyTime).toLocaleString('zh-CN', { hour12: false }) : _.lastModifyTime}</td>`
        })
      })

      // 添加表头
      const tablehead = ['序号', '公司', '职位', '薪资', '经验', '招聘人', '招聘人职位', '最后修改时间']
      list.unshift(`<thead><tr class="ui-widget-header">${tablehead.map((_) => `<th>${_}</th>`).join('')}</tr></thead>`)

      const table = $('<table>', {
        html: list,
        class: 'ui-widget ui-widget-content'
      })

      // 将表格添加到新元素中

      newElement.append(table)

      // 将新元素添加到 body 中
      $('.my-root-element').append(newElement);
    }


    function createRootDom() {
      const rootDom = $('<div>', {
        class: 'my-root-element',
      })

      // 将新元素添加到 body 中
      $('body').append(rootDom);

      // 设置可以拖动
      $('.my-root-element').draggable()
    }




    function createStyleLink() {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = 'https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css'
      document.head.appendChild(link)
    }
  } catch (error) {
    console.error(error)
  }

  // 插入样式
  const style = document.createElement('style')
  style.type = 'text/css'
  style.id = 'my-custom-style'
  const str = `
  .my-root-element {
    position: fixed;
    top: 10px;
    left: 10px;
    z-index: 9999;
    border: 1px solid #ccc;
    border-radius: 5px;
    background-color: #fff;
    height: 300px;
    overflow: hidden;
  }
  .my-custom-element {
      background-color: #f9f9f9;
      border: 1px solid #ccc;
      padding: 10px;
      margin: 10px 0;
      font-size: 16px;
      color: #333;
      height: 300px;
      overflow: auto;
  }

  .my-custom-element table {
      width: 100%;
      border-collapse: collapse;
      height: 100%;
  }

  .my-custom-element th, .my-custom-element td {
      border: 1px solid #ccc;
      padding: 5px;
      text-align: center;
  }
`
  style.innerHTML = str
  document.head.appendChild(style)
})();