Last active
January 20, 2022 13:48
-
-
Save kaisermann/bae4db359722898f6d2607a6b4998a94 to your computer and use it in GitHub Desktop.
Immoscout snippets
This file contains hidden or 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
javascript:function sanitize(e){return e.replace(/[^a-zA-Z0-9_]/g,"_").trim()}function getImagesInfo(){const e=new Map;[...document.querySelectorAll("#fullscreenGallery img, img[data-src]")].filter(e=>(e.dataset.src||e.src).includes("https://pictures.immobilienscout24.de/listings/")).map(e=>({src:(e.dataset.src||e.src).match(/(.*?)\/ORIG/)[1],title:sanitize(e.title||e.alt||e.dataset.caption)})).forEach(t=>e.set(t.src,t));return[...e.values()]}function getExposeInfo(){return{title:sanitize(document.querySelector("#expose-title").innerText),id:window.location.href.match(/expose\/(\d*)\??/)[1],content:document.querySelector("#is24-content").innerText,imagesInfo:getImagesInfo()}}async function printPage(){const{toBlob:e}=await import("https://cdn.skypack.dev/html-to-image");return[...document.querySelectorAll(".show-more a")].forEach(e=>e.click()),e(document.body,{backgroundColor:"white"})}async function createExposeZip({imagesInfo:e,title:t,id:o,content:n}){const{default:i}=await import("https://cdn.skypack.dev/jszip"),{default:{saveAs:s}}=await import("https://cdn.skypack.dev/file-saver");let c=(new i).folder("collection"),a=1;for(const t of e){const e=`img-${a++}-${t.title}.jpg`,o=await fetch(t.src).then(e=>e.blob()),n=new File([o],e);c.file(e,n)}const l=await printPage();c.file("page.jpg",new File([l],"page.jpg")),c.file("content.txt",n),s(await c.generateAsync({type:"blob"}),`${o}-${t}.zip`)}async function getExpose(){console.log("Getting expose things");const e=getExposeInfo();console.log("Expose things:",e),console.log("ignore the following errors"),await createExposeZip(e),console.log("Expose saved")}getExpose(); |
This file contains hidden or 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
function sanitize(str) { | |
return str.replace(/[^a-zA-Z0-9_]/g, '_').trim(); | |
} | |
function getImagesInfo() { | |
const imageMap = new Map(); | |
const imgEls = [ | |
...document.querySelectorAll('#fullscreenGallery img, img[data-src]'), | |
].filter((img) => | |
(img.dataset.src || img.src).includes( | |
'https://pictures.immobilienscout24.de/listings/', | |
), | |
); | |
const imgInfos = imgEls | |
.map((img) => ({ | |
src: (img.dataset.src || img.src).match(/(.*?)\/ORIG/)[1], | |
title: sanitize(img.title || img.alt || img.dataset.caption), | |
})) | |
.forEach((imageInfo) => imageMap.set(imageInfo.src, imageInfo)); | |
return [...imageMap.values()]; | |
} | |
function getExposeInfo() { | |
const exposeTitle = sanitize( | |
document.querySelector('#expose-title').innerText, | |
); | |
const exposeId = window.location.href.match(/expose\/(\d*)\??/)[1]; | |
const content = document.querySelector('#is24-content').innerText; | |
return { | |
title: exposeTitle, | |
id: exposeId, | |
content, | |
imagesInfo: getImagesInfo(), | |
}; | |
} | |
async function printPage() { | |
const { toBlob } = await import('https://cdn.skypack.dev/html-to-image'); | |
const allShowMore = [...document.querySelectorAll('.show-more a')]; | |
allShowMore.forEach((a) => a.click()); | |
return toBlob(document.body, { | |
backgroundColor: 'white', | |
}); | |
} | |
async function createExposeZip({ imagesInfo, title, id, content }) { | |
const { default: JSZip } = await import('https://cdn.skypack.dev/jszip'); | |
const { | |
default: { saveAs }, | |
} = await import('https://cdn.skypack.dev/file-saver'); | |
let zip = new JSZip(); | |
let folder = zip.folder('collection'); | |
let i = 1; | |
for (const imageInfo of imagesInfo) { | |
const filename = `img-${i++}-${imageInfo.title}.jpg`; | |
const imageBlob = await fetch(imageInfo.src).then((response) => | |
response.blob(), | |
); | |
const imageFile = new File([imageBlob], filename); | |
folder.file(filename, imageFile); | |
} | |
const pageBlob = await printPage(); | |
folder.file(`page.jpg`, new File([pageBlob], 'page.jpg')); | |
folder.file('content.txt', content); | |
const zipFile = await folder.generateAsync({ type: 'blob' }); | |
saveAs(zipFile, `${id}-${title}.zip`); | |
} | |
async function getExpose() { | |
console.log('Getting expose things'); | |
const exposeInfo = getExposeInfo(); | |
console.log('Expose things:', exposeInfo); | |
console.log('ignore the following errors'); | |
await createExposeZip(exposeInfo); | |
console.log('Expose saved'); | |
} | |
getExpose(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment