Skip to content

Instantly share code, notes, and snippets.

@reteps
Last active December 27, 2018 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reteps/da2bf4ae147f74f025012b929874de94 to your computer and use it in GitHub Desktop.
Save reteps/da2bf4ae147f74f025012b929874de94 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Estately + Zillow Plugin
// @namespace http://tampermonkey.net/
// @version 0.1.1
// @description Plugin
// @author You
// @match https://www.estately.com/*
// @grant none
// @run-at document-end
// @require https://code.jquery.com/jquery-3.3.1.min.js
// ==/UserScript==
function run() {
'use strict';
const Http = new XMLHttpRequest();
var parser = new DOMParser();
var houses = JSON.parse(xpath("/html/head/script[14]").innerText)["@graph"];
var provider = "https://api.allorigins.ml/get?url=";
let markers = document.querySelectorAll(".map-marker");
var labels = document.querySelectorAll(".result-item");
var numHouses = houses.length;
var styleText = "";
if (markers.length != labels.length) {
numHouses = 0;
alert(`the # of markers is ${markers.length} and the # of houses listed is ${houses.length}. ERROR`);
}
console.log("houses received by DATA:",houses.length / 2);
for (var i=0; i<numHouses; i+=2) {
var house = houses[i].address;
var adjustedIndex = Math.floor(i / 2);
var houseMarker = markers[adjustedIndex];
var houseLabel = labels[adjustedIndex];
console.log(adjustedIndex,houseMarker,houseLabel);
var price = parseFloat(houses[i+1].offers.price);
var uri = `zws-id=X1-ZWz1fvm3ocr9jf_4rdd9&address=${house.streetAddress}&citystatezip=${house.postalCode}`;
var url = "https://www.zillow.com/webservice/GetDeepSearchResults.htm?" + uri;
console.log("Price:" + price);
styleText += modifyHouseOnPage(houseMarker, houseLabel, url, price)
}
};
function addStyle(styleText) {
console.log("Style:" + styleText);
var head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style');
style = document.createElement('style');
style.type = 'text/css';
if (style.styleSheet){
style.styleSheet.cssText = styleText;
} else {
style.appendChild(document.createTextNode(styleText));
}
head.appendChild(style);
}
function modifyHouseOnPage(houseMarker, houseLabel, url, price) {
let goodThreshold = 72;
let greatThreshold = 60;
let uniqueLabelTag = houseLabel.classList[3]
var style = "";
$.getJSON('https://api.allorigins.ml/get?url=' + encodeURIComponent(url) + '&callback=?', function(data){
let zpid = data.contents.split("<zpid>",-1)[1].split("</zpid>",-1)[0];
let url = data.contents.split("<homedetails>",-1)[1].split("</homedetails>",-1)[0];
console.log("ZPID:" + zpid);
console.log("URL:" + url);
if (zpid != undefined) {
var zestURL = `https://www.zillow.com/webservice/GetZestimate.htm?zws-id=X1-ZWz1fvm3ocr9jf_4rdd9&zpid=${zpid}&rentzestimate=true`
console.log(zestURL);
$.getJSON('https://api.allorigins.ml/get?url=' + encodeURIComponent(zestURL) + '&callback=?', function(data){
let zestimate = data.contents.split("<rentzestimate>",-1)[1].split("</amount>",-1)[0].replace("<amount currency=\"USD\">","",-1);
console.log("zestimate:" + zestimate);
let content = `<a href="${url}">${zestimate}</a>`
let score = price / zestimate;
console.log("score:" + score);
var color = ""
// 10000 / 500 = 20
if (score < greatThreshold) {
color = "green";
} else if (score < goodThreshold) {
color = "yellow";
} else {
color = "red";
}
document.querySelector(`.${houseLabel.classList[3]}`).href = url;
style += `
#${houseMarker.id} {
background-color: ${color} !important;
}
.${houseLabel.classList[3]} .result-price::after {
content: " $${zestimate}" !important;
}
`
addStyle(style);
});
} else {
console.log("error");
}
});
}
function xpath(path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
window.addEventListener ("load", run);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment