LeBonCoin plus plus General

lbc++general

  1. // ==UserScript==
  2. // @name LeBonCoin plus plus General
  3. // @namespace lbc_general
  4. // @include https://www.leboncoin.fr/*
  5. // @version 1.0
  6. // @grant GM_xmlhttpRequest
  7. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js
  8. // @description lbc++general
  9.  
  10. // ==/UserScript==
  11.  
  12. this.$ = this.$$$ = jQuery.noConflict(true);
  13.  
  14.  
  15. // if($( "#searcharea option:selected" ).text().indexOf('Côte') !== -1) {
  16. // var onlyAddInfo = true;
  17. // } else {
  18. // var onlyAddInfo = false;
  19. // }
  20.  
  21. $(function() {
  22. console.log( "LBC++ loading..." );
  23.  
  24. var googleApiKey = "AIzaSyCHSc3xuyTzEa2fJfspmoibJduKa1iNmVs";
  25. var destCity = ["6 allée des clairs soleils, 21490 Ruffey-lès-Echirey, France", ""];
  26. // http://atlas.cosmosia.com/
  27. // var postalCodes = ["21121", "21120", "21240", "21370", "21380", "21490", "21850", "21800", "21560", "21110", "21600", "21300", "21910", "21220", "21160", "21410", "21640", "21540", "21470", "21310", "21270"];
  28.  
  29. function getDistances(place, el, cityEl, dest, cityName) {
  30. // console.log("getting: "+ place.city);
  31. // transit: https://developers.google.com/maps/documentation/distance-matrix/intro?hl=fr#travel_modes
  32. var request = "https://maps.googleapis.com/maps/api/distancematrix/json?origins="+place.city+"&destinations="+dest+"&mode=driving&sensor=false&key="+googleApiKey;
  33.  
  34. GM_xmlhttpRequest({
  35. method: "GET",
  36. url: request,
  37. onload: function(result) {
  38. var res = $.parseJSON(result.response);
  39. var status = res.rows[0].elements[0].status;
  40.  
  41. if(status === "OK") {
  42.  
  43. var duration = res.rows[0].elements[0].duration.text;
  44. var distance = res.rows[0].elements[0].distance.text;
  45.  
  46. var infosAdd = distance+ ', ' +duration;
  47. var pBlock = '<p class="item_supp">'+cityName+': '+infosAdd+'</p>';
  48. cityEl.after(pBlock);
  49.  
  50. }
  51. }
  52. });
  53. }
  54. function parsePageDom() {
  55. var offers = $('.tabsContent ul li');
  56. $.each(offers, function(i, el){
  57. // add target blank to a link
  58. // console.log('el', $(el).find('a').attr("target","_blank"));
  59. var href = $(el).find('a').attr('href');
  60.  
  61. $(el).find('a').attr("target","_blank");
  62.  
  63. var ctn = $(el).find('.item_price');
  64. ctn.prev().prev().append('<p class="item_supp loading" content=""><strong>Loading values...</strong></p>');
  65.  
  66.  
  67. requestPagesBis(href, el);
  68.  
  69. // change pro/private container style
  70. var isProContainer = ($(el).find('.ispro').length !== 0);
  71. if(isProContainer) {
  72. $(el).css('opacity', '0.8').css('background', '#ddd');
  73. }
  74. if(!isProContainer && $(el).find('.lbcppheader').length === 0) {
  75. $(el).css('border', '1px solid #aaa');
  76. }
  77. // get city name to retrieve distance and duration
  78. var cityEl = $(el).find('.item_infos .item_supp:eq(1)');
  79. var cityTxt = $.trim(cityEl.text());
  80. var cityParsing = cityTxt.replace(/\r\n/g, "\n").split("\n");
  81. var place = { "city" : cityParsing[0], "departement" : cityParsing[cityParsing.length-1].trim() };
  82. getDistances(place, el, cityEl, destCity[0], "Maison");
  83. getDistances(place, el, cityEl, destCity[1], "beaune");
  84. });
  85. }
  86.  
  87.  
  88. function requestPagesBis(requestUrl, container, i) {
  89. // console.log("Requesting" +requestUrl +"...");
  90.  
  91. // window.open(requests[i].request);
  92. // console.log("["+i+"] - Requesting lbc page ("+requests[i].request+") with "+requests[i].postalCodes.length+" postal codes...");
  93. GM_xmlhttpRequest({
  94. method: "GET",
  95. url: requestUrl,
  96. onload: function(result) {
  97. var resx = {};
  98. // console.log("result", result);
  99. resx.brand = $(result.response).find('.value[itemprop="brand"]').text();
  100. resx.model = $(result.response).find('.value[itemprop="model"]').text();
  101. resx.releaseDate = $(result.response).find('.value[itemprop="releaseDate"]').text().trim();
  102.  
  103. // console.log(brand.text());
  104.  
  105. var a = $(result.response).find('.line');
  106.  
  107. $.each(a, function(i, el){
  108. // console.log('el', $(el).find('.property').text())
  109. // if( $(el).find('.property').text() == "Année-modèle" ) {
  110. // var annemodele = $(el).find('.value').text();
  111. // }
  112. if( $(el).find('.property').text() == "Kilométrage" ) {
  113. resx.km = $(el).find('.value').text();
  114. }
  115. if( $(el).find('.property').text() == "Carburant" ) {
  116. resx.carbu = $(el).find('.value').text();
  117. }
  118. if( $(el).find('.property').text() == "Boîte de vitesse" ) {
  119. resx.boite = $(el).find('.value').text();
  120. }
  121. // console.log(brand, model, releaseDate, km, carbu, boite);
  122. });
  123. console.log($(container).find('.item_price').prev().prev());
  124. var ctn = $(container).find('.item_price');
  125. ctn.prev().prev().append('<p class="item_supp" content="">&nbsp;</p>');
  126. ctn.prev().prev().append('<p class="item_supp" content="">Kilométrage:<b>'+resx.km+'</b></p>');
  127. ctn.prev().prev().append('<p class="item_supp" content="">Année:<b>'+resx.releaseDate+'</b></p>');
  128. ctn.prev().prev().append('<p class="item_supp" content="">Marque:'+resx.brand+'</p>');
  129. ctn.prev().prev().append('<p class="item_supp" content="">Modèle:'+resx.model+'</p>');
  130. ctn.prev().prev().append('<p class="item_supp" content="">&nbsp;</p>');
  131. ctn.prev().prev().append('<p class="item_supp" content="">Carburant:'+resx.carbu+'</p>');
  132. ctn.prev().prev().append('<p class="item_supp" content="">boite:'+resx.boite+'</p>');
  133. $(container).find('.loading').remove();
  134. // if(i === requests.length-1) {
  135. // // parsePageDom();
  136. // }
  137. }
  138. });
  139.  
  140. }
  141.  
  142. // return container as dom el
  143. function cleanupPage() {
  144. $('body').find('.information-immo').empty();
  145. $('body').find('.pagination').empty();
  146. $('body').find('.tabsContent').empty();
  147. $('body').find('.tabsContent').append('<ul id="lbcpp"><li><a class="list_item clearfix lbcppheader"><section class="item_infos"><h2 class="item_title">LBC PP</h2></section></a></li></ul>');
  148. $('.grid-2-1:eq(1)').after('<br />'+postalCodes.join(", "));
  149. var container = $('body').find('.tabsContent ul');
  150. return container;
  151. }
  152. function getDomBlock(postalCodes, requestLink, numberOfEls) {
  153. return '<li><a target="_blank" href="'+requestLink+'" class="list_item clearfix lbcppheader"><section class="item_infos"><h2 class="item_title">'+postalCodes.join(", ")+' - ('+numberOfEls+')</h2></section></a></li>';
  154. }
  155. // return requests as array
  156. function buildRequest() {
  157. var requests = [];
  158. var host = location.origin+location.pathname+'?location=';
  159. //var host = 'https://www.leboncoin.fr/ventes_immobilieres/offres/bourgogne/?location=';
  160. var pcString = 'Toutes%20les%20communes%20';
  161. var and = '%2C';
  162. // var closeReq = '&parrot=0&ret=1';
  163. var uriArguments = window.location.search.replace('?', '&');
  164. console.log(uriArguments);
  165.  
  166. var PostalCodeBy3 = [];
  167. var pcList = JSON.stringify(postalCodes);
  168. var pcList = JSON.parse(pcList);
  169. while (pcList.length > 0) {
  170. var splittedPC = pcList.splice(0, 3);
  171. PostalCodeBy3.push(splittedPC);
  172. }
  173.  
  174. for (var i = 0; i < PostalCodeBy3.length; i++) {
  175. var request = host;
  176. for (var j = 0; j < PostalCodeBy3[i].length; j++) {
  177. var pc = PostalCodeBy3[i][j];
  178. if (j !== PostalCodeBy3[i].length-1) {
  179. var str = pcString + pc + and;
  180. var request = request.concat(str);
  181. } else {
  182. var str = pcString + pc + uriArguments;
  183. var request = request.concat(str);
  184. requests.push({"request":request, "postalCodes": PostalCodeBy3[i]});
  185. }
  186. }
  187. }
  188. return requests;
  189. }
  190.  
  191. function requestPages(requests, container, i) {
  192. console.log("Requesting" +requests.length +" pages, with at least 3 postal codes...");
  193.  
  194. // window.open(requests[i].request);
  195. console.log("["+i+"] - Requesting lbc page ("+requests[i].request+") with "+requests[i].postalCodes.length+" postal codes...");
  196. GM_xmlhttpRequest({
  197. method: "GET",
  198. url: requests[i].request,
  199. onload: function(result) {
  200. var els = $(result.response).find('.tabsContent ul li');
  201. container.append(getDomBlock(requests[i].postalCodes, requests[i].request, els.length));
  202. container.append(els);
  203.  
  204. if(i === requests.length-1) {
  205. parsePageDom();
  206. }
  207. }
  208. });
  209.  
  210. }
  211.  
  212. // Process...
  213.  
  214. var isPlaceFieldEmpty = $('.nude') === null || $('.location').length === 0;
  215.  
  216. // console.log(onlyAddInfo);
  217. // if(isPlaceFieldEmpty && !onlyAddInfo) {
  218. // var requests = buildRequest();
  219. // var container = cleanupPage();
  220.  
  221. // setTimeout(function() {
  222. // for(var i=0; i<requests.length; i++) {
  223. // requestPages(requests, container, i);
  224. // }
  225. // }, 500);
  226. // } else {
  227. // console.log('pc or city field not empty: aborting script loading...');
  228. // }
  229.  
  230. // if(onlyAddInfo) {
  231. parsePageDom();
  232. // }
  233.  
  234. });