Skip to content

Instantly share code, notes, and snippets.

@parisni
Last active September 25, 2023 19:57
Show Gist options
  • Save parisni/ece9addf65c9fb19daff6193f52c5b36 to your computer and use it in GitHub Desktop.
Save parisni/ece9addf65c9fb19daff6193f52c5b36 to your computer and use it in GitHub Desktop.
grease monkey script to generate a rss feed from a leboncoin search
// ==UserScript==
// @name Leboncoin RSS
// @namespace https://gist.github.com/parisni/ece9addf65c9fb19daff6193f52c5b36
// @author parisni
// @description A kind of RSS for LeBonCoin with your personnal search
// @include https://www.leboncoin.fr/recherche*
// @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js
// @require https://raw.githubusercontent.com/eligrey/FileSaver.js/master/dist/FileSaver.min.js
// @version 1.2
// @grant GM_addStyle
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_xmlhttpRequest
// @grant window.close
// @grant window.focus
// ==/UserScript==
/*
Have fun with the config variable !
*/
function buildRss(title, date, posts){
return `<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
<id>${title}</id>
<title>${title}</title>
<updated>${date}</updated>
<logo>https://www.leboncoin.fr/logo.svg</logo>
<subtitle>Leboncoin search feed</subtitle>
${buildRssItems(date, posts)}
</feed>`;
}
function buildRssItems(date, items) {
return items
.map((item) => {
return `
<entry>
<id>${item.link}</id>
<title>${item.title}</title>
<link href="${item.link}" rel="alternate"/>
<content type="xhtml"><div>${item.descr}</div></content>
<updated>${date}</updated>
</entry>`;
})
.join("");
}
$(function(){
if (window.top !== window.self) { //prevent running in iframes
return;
}
var lbcUrl = document.location.href
GM_xmlhttpRequest({ //allow CORS
method: "GET",
url: lbcUrl,
onload: function(data) {
var content = []
var res = document.querySelectorAll('a[data-qa-id="aditem_container"]')
for (let i = 0; i < res.length; i++) {
var title = res[i].computedName
var link = res[i].href
var descr = res[i].innerHTML
var tmp = {'link': link, 'title': title, 'descr': descr}
content.push(tmp)
console.log(tmp)
}
if(content.length!==0){
var blob = new Blob([buildRss(lbcUrl, new Date(Date.now()).toISOString(), content)], {type: "text/plain;charset=utf-8"});
saveAs(blob, "atom.xml")
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment