ciencuadras.com price per square meter

Show the price per square meter in the search results of ciencuadras.com

  1. // ==UserScript==
  2. // @name ciencuadras.com price per square meter
  3. // @namespace https://github.com/healarconr
  4. // @version 0.1
  5. // @description Show the price per square meter in the search results of ciencuadras.com
  6. // @author Hernán Alarcón
  7. // @match https://www.ciencuadras.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13. function calculatePricePerSquareMeter() {
  14. calculatePricePerSquareMeterInSearchResults();
  15. }
  16. function calculatePricePerSquareMeterInSearchResults() {
  17. const properties = document.querySelectorAll('.inmuebles-results > app-card');
  18. for (const property of properties) {
  19. try {
  20. let pricePerSquareMeterElement = property.querySelector('p.pricePerSquareMeter');
  21. if (pricePerSquareMeterElement) {
  22. pricePerSquareMeterElement.remove();
  23. }
  24. const priceNode = property.querySelector('h3');
  25. const price = findPrice(priceNode.textContent);
  26. const area = findArea(property.querySelector('li:nth-child(3)').textContent)
  27. const pricePerSquareMeter = (price / area).toLocaleString('es-CO', {style:'currency', currency: 'COP'}) + '/m\u00B2';
  28. pricePerSquareMeterElement = document.createElement('p');
  29. pricePerSquareMeterElement.className = 'pricePerSquareMeter';
  30. pricePerSquareMeterElement.style.fontSize = 'smaller';
  31. pricePerSquareMeterElement.style.fontWeight = 'normal';
  32. pricePerSquareMeterElement.appendChild(document.createTextNode(pricePerSquareMeter));
  33. priceNode.parentNode.insertBefore(pricePerSquareMeterElement, priceNode.nextSibling);
  34. } catch (e) {
  35. // Do nothing
  36. }
  37. }
  38. }
  39. function findPrice(value) {
  40. return parseFloat(value.match(/[\d.,]+/)[0].replace(/\./g, ''));
  41. }
  42. function findArea(value) {
  43. return parseFloat(value.match(/[\d.]+/)[0]);
  44. }
  45. const propertiesContainer = document.querySelector('.inmuebles-results');
  46. if (propertiesContainer) {
  47. setInterval(calculatePricePerSquareMeterInSearchResults, 1000);
  48. new MutationObserver(calculatePricePerSquareMeter).observe(propertiesContainer, {childList: true});
  49. }
  50. calculatePricePerSquareMeter();
  51. setTimeout(calculatePricePerSquareMeter, 2000);
  52. })();