链家租房列表增加一些信息

比如加上小区的建筑年代

目前為 2021-07-24 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         链家租房列表增加一些信息
// @namespace    https://greasyfork.org/zh-CN/users/177458-bd777
// @version      0.1
// @description  比如加上小区的建筑年代
// @author       windeng
// @match        https://gz.lianjia.com/zufang/*
// @icon         https://www.google.com/s2/favicons?domain=lianjia.com
// @grant        GM_xmlhttpRequest
// ==/UserScript==

var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver

async function Sleep(sleepSecs) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve()
        }, sleepSecs * 1000)
    })
}

async function WaitUntil(conditionFunc, sleepSecs) {
    sleepSecs = sleepSecs || 1
    return new Promise((resolve, reject) => {
        if (conditionFunc()) resolve()
        let interval = setInterval(() => {
            if (conditionFunc()) {
                clearInterval(interval)
                resolve()
            }
        }, sleepSecs * 1000)
    })
}

function Request(url, opt={}) {
	Object.assign(opt, {
		url,
		timeout: 2000,
		responseType: 'json'
	})

	return new Promise((resolve, reject) => {
		/*
		for (let f of ['onerror', 'ontimeout'])
			opt[f] = reject
		*/

		opt.onerror = opt.ontimeout = reject
		opt.onload = resolve

		GM_xmlhttpRequest(opt)
	}).then(res => {
        // console.log('?', url, res)
        if (res.status === 200) return Promise.resolve(res.response || res.responseText)
        else return Promise.reject(res)
    }, err => {
        return Promise.reject(err)
    })
}

function Get(url, opt={}) {
    Object.assign(opt, {
        method: 'GET'
    })
    return Request(url, opt)
}

function Post(url, opt={}) {
    Object.assign(opt, {
        method: 'POST'
    })
    return Request(url, opt)
}

async function GetElementByText(startElem, selector, text, exist) {
    /*
    selector: 选择器
    text: 内容
    exist: 是否只要存在就ojbk
    */
    exist = exist || false
    let elemList = startElem.querySelectorAll(selector)
    for (let i = 0; i < elemList.length; ++i) {
        let elem = elemList[i]
        if (exist) {
            if (elem.innerText.search(text) !== -1) return elem
        } else {
            if (elem.innerText === text) return elem
        }
    }
}

async function GetXiaoquPage(xiaoquId) {
    return Get(`https://gz.lianjia.com/xiaoqu/${xiaoquId}`)
}

async function GetXiaoquInfo(xiaoquId) {
    let resp = await GetXiaoquPage(xiaoquId)
    // console.log(resp)
    let el = document.createElement('html')
    el.innerHTML = resp
    // console.log(el)
    let elemList = el.querySelectorAll('div.xiaoquInfo > div.xiaoquInfoItem')
    let result = {}
    for (let i=0; i<elemList.length; ++i) {
        let elem = elemList[i]
        let label = elem.querySelector('span.xiaoquInfoLabel').innerText.trim()
        let content = elem.querySelector('span.xiaoquInfoContent').innerText.trim()
        result[label] = content
    }

    return result
}

async function DoLogic() {
    await WaitUntil(() => {
        return !!document.querySelector('div.content__list')
    })

    let divList = document.querySelectorAll('div.content__list--item')
    for (let i=0; i<divList.length; ++i) {
        let div = divList[i]
        // 获取小区id
        let a = div.querySelector('p.content__list--item--des > a:nth-of-type(3)')
        let matches = a.getAttribute('href').match(/zufang\/c(\d+)/)
        // console.log(a, matches)
        if (!matches) continue
        let xiaoquId = matches[1]


        let xiaoquInfo = await GetXiaoquInfo(xiaoquId)
        console.log(xiaoquInfo)
        let desElem = div.querySelector('p.content__list--item--des')
        let span = document.createElement('span')
        span.innerHTML = `<i>/</i> ${xiaoquInfo["建筑年代"]}`
        desElem.appendChild(span)

        /*
        GetXiaoquInfo(xiaoquId).then(xiaoquInfo => {
            console.log(xiaoquInfo)
            let desElem = div.querySelector('p.content__list--item--des')
            let span = document.createElement('span')
            span.innerHTML = `<i>/</i> ${xiaoquInfo["建筑年代"]}`
            desElem.appendChild(span)
        }).catch(err => {
            console.error(err)
        })
        */
    }
}

(async function() {
    'use strict';

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