-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredfin_bookmarklet.js
202 lines (176 loc) · 5.65 KB
/
redfin_bookmarklet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* 1. Convert to a bookmarklet using https://chriszarate.github.io/bookmarkleter/
* (minifies, escapes, and adds javascript prefix)
* 2. Manually create in chrome (right click bookmarks bar, Add Page)
* 3. Set the URL to the text from (1)
*/
class ListingDetails {
constructor(
url,
addressStreet,
addressCityStateZip,
beds,
baths,
listPrice,
estimatedPrice,
pricePerSqFt,
sqFt,
lotSize,
yearBuilt,
commutes,
schools,
floodRisk
) {
this.url = url;
this.addressStreet = addressStreet;
this.addressCityStateZip = addressCityStateZip;
this.beds = beds;
this.baths = baths;
this.listPrice = listPrice;
this.estimatedPrice = estimatedPrice;
this.pricePerSqFt = pricePerSqFt;
this.sqFt = sqFt;
this.lotSize = lotSize;
this.yearBuilt = yearBuilt;
this.commutes = commutes;
this.schools = schools;
this.floodRisk = floodRisk;
}
getCommuteText() {
let strings = [];
for (const c of this.commutes) {
strings.push(c.getText());
}
return strings.join('. ');
}
getSchoolsText() {
let strings = [];
for (const s of this.schools) {
strings.push(s.getText());
}
return strings.join('\n ');
}
getSummaryText() {
let t = this;
return (`${t.addressStreet}, ${t.addressCityStateZip}:\n` +
`${t.listPrice} (${t.estimatedPrice}), ${t.pricePerSqFt}/sqFt\n` +
`${t.beds}br/${t.baths}. ${t.sqFt} sqFt, ${t.getLotSizeText()} lot. ${t.beds}br/${t.baths}. Built ${t.yearBuilt}\n` +
`${t.getCommuteText()}\n ${t.getSchoolsText()}\n` +
`${t.floodRisk}`
);
}
getLotSizeText() {
return `${this.lotSize.split(" ")[0]}`;
}
}
class Commute {
constructor(time, dest) {
this.time = time;
this.dest = dest;
}
getText() {
return `${this.time}${this.dest.split(',')[0]}`;
}
}
class School {
constructor(name, rating, subinfo, distance) {
this.name = name;
this.rating = rating;
this.subinfo = subinfo;
this.distance = distance;
}
getText() {
let t = this;
return `(${t.rating}): ${t.name}. ${t.subinfo} ${distance}`;
}
}
function getCommute(doc, listingDetails) {
ret = [];
commutes = doc.getElementsByClassName('Commute');
for (const c of commutes) {
time = c.children[1].children[0].textContent;
dest = c.children[1].children[1].textContent;
ret.push(new Commute(time, dest));
}
listingDetails.commutes = ret;
}
function getSchools(doc, listingDetails) {
ret = [];
schools = doc.getElementsByClassName('schools-table-row');
for (const s of schools) {
name = s.children[0].getElementsByClassName('school-title')[0].textContent;
ratingElement = s.getElementsByClassName('rating-num')[0];
rating = ratingElement ? ratingElement.textContent : 'NR';
distance = s.getElementsByClassName('distance-col')[0].children[0].textContent;
subinfo = s.getElementsByClassName('sub-info')[0].textContent;
ret.push(new School(name, rating, subinfo, distance));
}
listingDetails.schools = ret;
}
/**
* Get bed/bath details at top of page
*/
function getBedBathSqftDetails(doc, listingDetails) {
keyDetails = doc.getElementsByClassName('stat-block');
fields = {
'Beds': 'beds',
'Baths': 'baths',
'Sq Ft': 'sqFt'
};
for (const d of keyDetails) {
header = d.children && d.children[1].textContent;
content = d.children && d.children[0].textContent;
if (header in fields) {
listingDetails[fields[header]] = content;
}
}
}
function getKeyDetails(doc, listingDetails) {
keyDetails = doc.getElementsByClassName('keyDetail');
fields = {
'List Price': 'listPrice',
'Redfin Estimate': 'estimatedPrice',
'Price/Sq.Ft.': 'pricePerSqFt',
'Year Built': 'yearBuilt',
'Lot Size': 'lotSize',
};
for (const d of keyDetails) {
header = d.children && d.children[0].textContent;
content = d.children && d.children[1].textContent;
if (header in fields) {
listingDetails[fields[header]] = content;
}
}
}
function getAddress(doc, listingDetails) {
listingDetails['addressStreet'] = doc.getElementsByClassName('street-address')[0].children[0].textContent;
listingDetails['addressCityStateZip'] = doc.getElementsByClassName('homeAddress-variant')[0].children[1].textContent;
}
function getFloodRisk(doc, listingDetails) {
floodRiskDescription = doc.getElementsByClassName('floodPreview')[0].children[0].textContent;
listingDetails['floodRisk'] = floodRiskDescription;
}
function getRedfinDetails() {
details = new ListingDetails();
details.url = document.URL;
getAddress(document, details);
getBedBathSqftDetails(document, details);
getKeyDetails(document, details);
getCommute(document, details);
getSchools(document, details);
getFloodRisk(document, details);
return details;
}
textSummary = getRedfinDetails().getSummaryText();
(function (text) {
var node = document.createElement('textarea')
var selection = document.getSelection()
node.textContent = text
document.body.appendChild(node)
selection.removeAllRanges()
node.select()
document.execCommand('copy')
selection.removeAllRanges()
document.body.removeChild(node)
})(textSummary);
alert(textSummary);