-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser-script.js
195 lines (181 loc) · 6.94 KB
/
browser-script.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
// ==UserScript==
// @name Immo-Helper
// @namespace http://tampermonkey.net/
// @icon https://www.google.com/s2/favicons?sz=64&domain=immobilienscout24.de
// @version 0.3
// @description Add and hide listings on ImmobilienScout24
// @author Felix Jonas Wiegleb
// @match https://www.immobilienscout24.de/Suche/*
// @grant GM_xmlhttpRequest
// @connect immo.mathia.xyz
// @connect localhost
// @require https://code.jquery.com/jquery-3.6.0.min.js
// ==/UserScript==
(function() {
'use strict';
const username = "Amronas";
const address = "http://localhost:3001";
/* globals jQuery, $, waitForKeyElements */
function fetchListings() {
GM_xmlhttpRequest({
method: 'GET',
url: `${address}/listings`,
onload: function(response) {
if (response.status === 200) {
const listings = JSON.parse(response.responseText);
updateListings(listings);
} else {
console.error('Error fetching listings:', response);
}
}
});
}
function addButtons() {
$('li.result-list__listing').each(function() {
const listingId = $(this).data('id');
if (!$(this).find('.immo-helper-buttons').length) {
const buttonContainer = $('<div class="immo-helper-buttons"></div>');
const addButton = $('<button class="immo-helper-button status-add">Gefällt uns</button>')
.click(() => handleButtonClick(listingId, 'add'));
const maybeButton = $('<button class="immo-helper-button status-maybe">Mal schauen</button>')
.click(() => handleButtonClick(listingId, 'maybe'));
const hideButton = $('<button class="immo-helper-button status-hide">Verstecken</button>')
.click(() => handleButtonClick(listingId, 'hide'));
buttonContainer.append(addButton).append(maybeButton).append(hideButton);
$(this).append(buttonContainer);
}
});
}
function updateListings(listings) {
$('li.result-list__listing').each(function() {
const listingId = $(this).data('id');
const listing = listings.find(listing => listing.id == listingId);
if (listing) {
const resultElement = $(this);
setElementByStatus(resultElement, listing.status, listingId, listing.user);
}
});
}
function handleButtonClick(listingId, action) {
GM_xmlhttpRequest({
method: 'POST',
url: `${address}/${action}`,
data: JSON.stringify({ listingId, username }),
headers: {
'Content-Type': 'application/json'
},
onload: function(response) {
if (response.status === 200) {
const resultElement = $(`li[data-id="${listingId}"]`);
setElementByStatus(resultElement, action, listingId, username);
} else {
console.error('Error:', response);
}
}
});
}
function setElementByStatus(resultElement, status, listingId, listedBy) {
let title = resultElement.find('.result-list-entry__data').find('a').first().text().trim();
let address = resultElement.find('.result-list-entry__address button').text().trim().split(", ");
let url = resultElement.find('.result-list-entry__data').find('a').first().attr('href');
if (resultElement.hasClass('result-list__listing--xl')) {
title = resultElement.find('.result-list-entry__brand-title').text().trim();
}
if (status === 'add' || status === 'hide' || status === 'maybe') {
const div = $(`
<div class="collapsed status-${status}">
<div class="collapsed-content">
<div class="collapsed-title">${title}</div>
<div class="collapsed-address">${address[1]}, ${address[0]}</div>
</div>
<div style="text-align: end; display: flex; flex-direction: column; justify-content: space-between">
<div><button class="undo-button">Undo</button></div>
<div style="font-style: italic; font-size: 12px">von ${listedBy}</div>
</div>
</div>
`);
div.find('.collapsed-title').click(() => window.open(url, '_blank'));
div.find('.undo-button').click(() => {
handleButtonClick(listingId, 'remove');
resultElement.show();
div.remove();
});
resultElement.hide().after(div);
}
}
const css = `
.immo-helper-buttons {
margin-top: 2px;
text-align: center;
}
.immo-helper-button {
border: none;
border-radius: 8px;
color: white;
padding: 6px 12px;
font-size: 16px;
margin-right: 5px;
cursor: pointer;
}
.status-add {
background-color: #04AA6D;
}
.status-hide {
background-color: #555555;
}
.status-maybe {
background-color: #d19120;
}
.undo-button {
border: none;
border-radius: 3px;
color: white;
padding: 2px 4px;
font-size: 12px;
background-color: #ec9494;
cursor: pointer;
}
.collapsed {
display: flex;
border-radius: 8px;
padding: 6px 12px;
margin: 12px 0;
font-size: 16px;
border-radius: 8px;
}
.collapsed.status-add {
border: solid #04AA6D;
background-color: #a8e2cd;
}
.collapsed.status-hide {
border: solid #adadad;
background-color: #e2e2e2;
}
.collapsed.status-maybe {
border: solid #d19120;
background-color: #f7dcb4;
}
.collapsed-title {
cursor: pointer;
display: inline-block;
margin-right: 18px;
font-weight: bold;
}
.status-hide .collapsed-title,
.status-hide .collapsed-address {
color: #878787;
}
.collapsed-content {
flex-grow: 1;
}
`;
const style = document.createElement('style');
style.type = 'text/css';
style.appendChild(document.createTextNode(css));
document.head.appendChild(style);
$(document).ready(() => {
if (username !== 'Amronas') addButtons();
fetchListings();
});
$(document).ajaxComplete(addButtons);
})();