// ==UserScript==
// @name Enhanced Ziroom
// @namespace https://github.com/catscarlet/enhanced_ziroom
// @description 这是一个增强自如官网访问体验的 userscript 。自如官网现在搜索到的结果把可以租的可以看的不能租的不能看的都混到一起了。这个插件可以在列表页就显示房源的状态,并用颜色标记出来。
// @description:en This is a userscript for www.ziroom.com
// @version 0.1.0
// @author catscarlet
// @match *://*.ziroom.com/z/nl/*.html*
// @require https://code.jquery.com/jquery-latest.js
// @compatible chrome 支持
// @run-at document-end
// @grant none
// @downloadURL none
// ==/UserScript==
window.jQueryLatest = $.noConflict(true);
(function() {
'use strict';
var $ = window.jQueryLatest;
$(function() {
console.log('enhanced_ziroom loaded');
getHouseList();
});
function getHouseList() {
var houselist = $('#houseList li');
houselist.each(function(index) {
var priceDetail = $(this).find('.priceDetail');
var detailUrl = priceDetail.find('.more').find('a').attr('href');
var detailId = url2id(detailUrl);
if (detailId) {
priceDetail.append('检测中...');
getDetail(detailUrl, detailId);
}
});
};
function getDetail(detailUrl, detailId) {
$.ajax({
type: 'GET',
url: detailUrl,
dataType: 'html',
async: true,
success: function(msg, textStatus, jqXHR) {
checkDetail(msg, detailId);
},
error: function(msg) {
console.log(msg);
}
});
}
function checkDetail(msg, detailId) {
var regexp_status_avaliable = new RegExp(/\(.*)\<\/a>/);
var regexp_status_unavaliable = new RegExp(/\s\(.*)\<\/a>/);
var regexp_name = new RegExp(/\(.|\n)*\((.|\n)*)\<\/h2\>/g);
var name = domtext2text(msg.match(regexp_name)[0]);
var status;
var status_value = false;
var status_avaliable = msg.match(regexp_status_avaliable);
var status_unavaliable = msg.match(regexp_status_unavaliable);
if (status_avaliable) {
status = domtext2text(status_avaliable[0]);
status_value = true;
} else {
status = domtext2text(status_unavaliable[0]);
status_value = false;
}
drawList(status_value, status, detailId);
}
function drawList(status_value, status, detailId) {
var css;
var thedom = $('#' + detailId);
if (status_value) {
thedom.text(status);
thedom.parent().parent().css('background-color', 'lightgreen');
} else {
thedom.text(status);
thedom.parent().parent().css('background-color', 'grey');
}
}
function domtext2text(domtext) {
var rst;
rst = $.trim($.text($.parseHTML(domtext)));
return rst;
}
function url2id(url) {
var regexp_url2id = new RegExp(/\/(\d+)\.html/);
var rst = url.match(regexp_url2id);
if (rst) {
return rst[1];
}
return false;
}
})();