Skip to content

Instantly share code, notes, and snippets.

@jhemono
Last active January 14, 2016 21:17
Show Gist options
  • Save jhemono/32444bada82ae7972d5a to your computer and use it in GitHub Desktop.
Save jhemono/32444bada82ae7972d5a to your computer and use it in GitHub Desktop.
Enregistreur d'annonces
// ==UserScript==
// @name Enregistrement d'annonce leboncoin.fr
// @version 0.4
// @description Ajoute la possibilité d'enregistrer des annonces sur leboncoin.fr
// @updateURL https://gist.githubusercontent.com/jhemono/32444bada82ae7972d5a/raw/enregistreurleboncoin.user.js
// @grant none
// @include http://www2.leboncoin.fr/ai/form/*
// @noframes
// ==/UserScript==
// TODO : Polish export import feature : show/hide fields, update list when importing (problem inconsistent data and document)
$(document).ready(function() {
var fill = function (infos) {
$("#category").val(infos.category).trigger("change");
$("#region").val(infos.region).trigger("change");
$("#dpt_code").val(infos.dpt_code);
$("#zipcode").val(infos.zipcode);
$("#city").val(infos.city);
$("#address").val(infos.address);
if (! infos.geo)
$("input[name=show_map]").trigger("click");
else {
$("input[name=latitude]").val(infos.geo.latitude);
$("input[name=longitude]").val(infos.geo.longitude);
$("input[name=geo_source]").val(infos.geo.geo_source);
$("input[name=geo_provider]").val(infos.geo.geo_provider);
}
$("#name").val(infos.name);
$("#email").val(infos.email);
$("#phone").val(infos.phone);
$("#subject").val(infos.subject);
$("#capacity").val(infos.capacity);
$("#swimming_pool").val(infos.swimming_pool);
$("#bedrooms").val(infos.bedrooms);
$("#price_min").val(infos.price_min);
$("#price_max").val(infos.price_max);
$("#body").val(infos.body);
};
var save = function () {
this.category = $("#category").val();
if (this.category != 12 && this.category != 67)
throw 'Seulement les annonces "Locations & Gîtes" et "Chambres d\'hôtes" sont supportées (mauvaise catégorie).';
this.region = $("#region").val();
this.dpt_code = $("#dpt_code").val();
this.zipcode = $("#zipcode").val();
this.city = $("#city").val();
this.address = $("#address").val();
var geo_source = $("input[name=geo_source]").val();
if (! geo_source)
throw 'Veulliez cliquer sur "Afficher sur la carte" et ajuster la position si nécessaire.';
else if (geo_source == "user") {
this.geo = {
latitude: $("input[name=latitude]").val(),
longitude: $("input[name=longitude]").val(),
geo_provider: $("input[name=geo_provider]").val(),
geo_source: geo_source
};
}
else // geo_position == "address"
this.geo = null;
this.name = $("#name").val();
this.email = $("#email").val();
this.phone = $("#phone").val();
this.subject = $("#subject").val();
this.capacity = $("#capacity").val();
this.swimming_pool = $("#swimming_pool").val();
this.bedrooms = $("#bedrooms").val();
this.price_min = $("#price_min").val();
this.price_max = $("#price_max").val();
this.body = $("#body").val();
};
var key = 'remplisseur';
var annonces = JSON.parse(window.localStorage.getItem(key) || "{}");
$(window).unload(function () {
window.localStorage.setItem(key, JSON.stringify(annonces));
});
var space = $('<div><div class="clear" /><h2>Annonces enregistrées</h2><div class="subtitle_triangle" /><div class="clear" /></div>');
$("div.maintext").after(space);
var host = $('<ul></ul>').appendTo(space);
var makeDiv = function (annonce) {
var div = $("<li></li>");
$('<a>' + annonce + '</a>').appendTo(div).on("click", function() {
fill(annonces[annonce]);
});
$('<span> </span>').appendTo(div);
$('<input type="button" value="Modifier" />').appendTo(div).on("click", function () {
if (! window.confirm('Êtes-vous sûr de vouloir modifier l\'annonce "' + annonce + '" ?'))
return;
add(annonce);
});
$('<span> </span>').appendTo(div);
$('<input type="button" value="X" />').appendTo(div).on("click", function () {
if (! window.confirm('Êtes-vous sûr de vouloir supprimer l\'annonce "' + annonce + '" ?'))
return;
delete annonces[annonce];
div.fadeOut(800, function () {
$(this).remove();
});
});
div.appendTo(host);
};
for (var annonce in annonces) {
if (annonces.hasOwnProperty(annonce))
makeDiv(annonce);
}
var add = function (nom) {
try {
var annonce = new save(); // Save first so as to check that the form was properly filled
if (! nom) { // We're adding an annonce rather than modifying one
nom = window.prompt("Choisir un nom pour cette annonce :", "");
if (! nom)
throw "Veuillez choisir un nom !";
if (annonces.hasOwnProperty(nom))
throw "Une annonce du même nom existe déjà.";
if (nom in annonce)
throw "Nom d'annonce invalide.";
makeDiv(nom);
}
annonces[nom] = annonce;
} catch (err) {
window.alert(err);
}
};
$('<a>Ajouter</a>').appendTo(space).on("click", function () {add (null); });
$('<span>&nbsp;</span>').appendTo(space);
$('<a>Exporter</a>').appendTo(space).on("click", function () {
$('<div><textarea disabled rows="20" cols="80">' + JSON.stringify(annonces) + '</textarea></div>').appendTo(space);
});
$('<span>&nbsp;</span>').appendTo(space);
$('<a>Importer</a>').appendTo(space).on("click", function () {
var importDiv = $('<div></div');
var importText = $('<textarea rows="20" cols="80" />').appendTo(importDiv);
$('<input type="button" value="Importer" />').appendTo(importDiv).on("click", function () {
annonces = JSON.parse(importText.val() || "{}");
});
importDiv.appendTo(space);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment