LeBonCoin plus plus General

lbc++general

目前為 2018-06-12 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        LeBonCoin plus plus General
// @namespace   lbc_general
// @include     https://www.leboncoin.fr/*
// @version     1.0
// @grant       GM.xmlHttpRequest 
// @require     https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js
// @description lbc++general

// ==/UserScript==

this.$ = this.$$$ = jQuery.noConflict(true);


// if($( "#searcharea option:selected" ).text().indexOf('Côte') !== -1) {
//   var onlyAddInfo = true;
// } else {
//   var onlyAddInfo = false;
// }

$(function () {
  console.log("LBC++ loading...");

  var googleApiKey = "AIzaSyCHSc3xuyTzEa2fJfspmoibJduKa1iNmVs";
  var destCity = ["3 rue du docteur quignard, 21000, DIJON, France", ""];
  // http://atlas.cosmosia.com/
  var postalCodes = ["21121", "21120", "21240", "21370", "21380", "21490", "21850", "21800", "21560", "21110", "21600", "21300", "21910", "21220", "21160", "21410", "21640", "21540", "21470", "21310", "21270"];


  function getDistances(place, el, cityEl, dest, cityName) {
    console.log("place.city: "+ place.city);
    console.log("dest: "+ dest);
    // transit: https://developers.google.com/maps/documentation/distance-matrix/intro?hl=fr#travel_modes
    var request = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=21 "+place.city+"&destinations="+dest+"&mode=driving&sensor=false&key="+googleApiKey;
    console.log(`request: `, request);

    GM.xmlHttpRequest ({
      method: "GET",
      url: request,
      onload: function(result) {
        console.log(`result: `, result);
        var res = $.parseJSON(result.response);
        
        var status = res.rows[0].elements[0].status;

        if(status === "OK") {

          var duration = res.rows[0].elements[0].duration.text;
          var distance = res.rows[0].elements[0].distance.text;

          var infosAdd =  distance+ ', ' +duration;
          console.log(`infosAdd: `, infosAdd);
          // var pBlock = '<p class="item_supp">'+cityName+': '+infosAdd+'</p>';
          var pBlock = '<p class="item_supp">'+cityName+': '+infosAdd+'</p><p><a href="https://www.google.fr/maps/dir/'+ dest +'/21 '+ place.city +'/" target="_blank">map</a></p>';
          cityEl.after(pBlock);
        }
        else {
          var pBlock = '<p class="item_supp">'+cityName+': NOT FOUND (<a href="'+request+'">gmaps link</a>)</p>';
          cityEl.after(pBlock);
        }
      }
    });
  }

  function parsePageDom() {
    var offers = $('.tabsContent ul li');
    // var offers = $('#react-tabs-1 div ul li');
    console.log(`offers: `, offers);
    $.each(offers, function(i, el){
      // add target blank to a link
      // console.log('el', $(el).find('a').attr("target","_blank"));
      var href =  $(el).find('a').attr('href');
      $(el).find('a').attr("target","_blank");
			
      var ctn = $(el).find('.item_price');
      // ctn.prev().prev().append('<p class="item_supp loading" content=""><strong>Loading values...</strong></p>');


      // requestPagesBis(href, el);

      // change pro/private container style
      var isProContainer = ($(el).find('.ispro').length !== 0);
      if(isProContainer) {
        $(el).css('opacity', '0.8').css('background', '#ddd');
      }
      if(!isProContainer && $(el).find('.lbcppheader').length === 0) {
        $(el).css('border', '1px solid #aaa');
      }
      // get city name to retrieve distance and duration
      var cityEl = $(el).find('.item_infos .item_supp:eq(1)');
      var cityTxt = $.trim(cityEl.text());
      var cityParsing = cityTxt.replace(/\r\n/g, "\n").split("\n");
      var place = { "city" : cityParsing[0], "departement" : cityParsing[cityParsing.length-1].trim() };
      getDistances(place, el, cityEl, destCity[0], "Travail");
    });
  }

  parsePageDom()

});