Skip to content

Instantly share code, notes, and snippets.

@s-nt-s
Created May 3, 2023 16:01
Show Gist options
  • Save s-nt-s/29d9129b8cd5a15d118692d38eabae30 to your computer and use it in GitHub Desktop.
Save s-nt-s/29d9129b8cd5a15d118692d38eabae30 to your computer and use it in GitHub Desktop.
idealista.search.user.js
// ==UserScript==
// @name Idealista List
// @version 1
// @include https://www.idealista.com/*
// @run-at document-end
// @grant none
// ==/UserScript==
function my_hack_ajax() {
const MAPA_URL = "/ajax/listingcontroller/listingmapajaxgrouped.ajax";
const LIST_URL = "/ajax/listingcontroller/listingajax.ajax"
const waitForGlobal = function(key, callback) {
const gvar = window[key];
if (gvar == null)
setTimeout(waitForGlobal, 100, arguments);
else
callback.apply(gvar, Array.from(arguments).slice(2));
};
const gJSON = function(obj) {
if (typeof obj === "string") obj = {
dataType: "json",
url: obj,
async: false
}
return $.ajax(obj).responseJSON;
}
const gAd = function(id) {
return gJSON({
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "GET",
url: "/ajax/mapitemdetails.ajax?adIds="+id,
async: false,
}).data.items[0];
}
const find = function(str, regexp, index) {
let m = str.match(regexp);
if (m==null) {
return null;
}
let v, n;
if (index==undefined) {
index = Math.min(1, m.length-1)-1;
}
v = m[index+1]
n = Number(v.replace(".", ""));
if (!isNaN(n)) return n;
if (v.endsWith(", Madrid")) v = v.substring(0, v.length-8);
return v;
}
const addMethods = function(obj, flds) {
Object.entries(flds).forEach(([key, value]) => {
obj[key] = (function(cached, fnc) {
if (this[cached] == null) {
this[cached] = fnc.apply(this, Array.from(arguments).slice(2));
}
return this[cached];
}).bind(obj, "_"+key, value);
});
}
waitForGlobal(
"jQuery",
(ad_filter) => {
jQuery.ajaxSetup({
dataFilter: function(data, type) {
let ad;
if (type != "json") return data;
if (this.url.startsWith(MAPA_URL)) {
const js = JSON.parse(data);
if (!js.data || !js.data.map || !js.data.map.items || !js.data.map.items.length) return data;
console.log(js.data.map.items);
js.data.map.items.forEach(point=>{
point.ads.forEach(ad=>{
ad = gAd(ad.adId);
ad_filter(ad);
})
})
}
if (this.url.startsWith(LIST_URL)) {
const js = JSON.parse(data);
if (!js.jsonResponse || !js.jsonResponse.ads || !js.jsonResponse.ads.length) return data;
console.log(js.jsonResponse.ads);
js.jsonResponse.ads.forEach(ad=>{
ad = gAd(ad.id);
ad_filter(ad);
})
}
return data;
}
})
const input = jQuery("input[name='adfilter_toberestored']")
input.prop("checked", !input.prop("checked"))
input.click()
},
(ad) => {
let planta = find(ad.floorNumber, /Planta (\d+)ª /);
if (planta==null) {
if (ad.floorNumber.startsWith("Entreplanta")) planta=0.5;
if (ad.floorNumber.startsWith("Bajo")) planta=0;
}
if (planta==null) console.log(ad.floorNumber)
}
);
}
const script = document.createElement("script");
script.textContent = "(" + my_hack_ajax.toString() + ")();";
document.head.appendChild(script);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment