Forked from jhemono/enregistreurleboncoin.user.js
Last active
August 29, 2015 14:27
-
-
Save dinh/e25874e6229333bb9bae to your computer and use it in GitHub Desktop.
Enregistreur d'annonces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Enregistrement d'annonce leboncoin.fr | |
// @version 0.3 | |
// @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 fonction export pour que les annonces ne soient pas supprimée quand on nettoie les information du navigateur | |
$(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); }); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment