generally harden gather

This commit is contained in:
Nils Maier 2019-08-21 20:40:07 +02:00
parent 2d1a7eceb0
commit 7b2d07e1f5

View File

@ -92,23 +92,29 @@ class Gatherer {
this.schemes = new Set(options.schemes); this.schemes = new Set(options.schemes);
this.transferable = options.transferable; this.transferable = options.transferable;
this.collectLink = this.collectLink.bind(this); this.collectLink = this.collectLink.bind(this);
this.collectImages = this.collectImages.bind(this); this.collectImage = this.collectImage.bind(this);
this.collectMedia = this.collectMedia.bind(this); this.collectMedia = this.collectMedia.bind(this);
Object.freeze(this); Object.freeze(this);
} }
collectLink(a: HTMLAnchorElement) { collectLink(a: HTMLAnchorElement) {
const item = this.makeItem(a.href, a); try {
if (!item) { const item = this.makeItem(a.href, a);
if (!item) {
return item;
}
urlToUsable(item, item.url);
item.fileName = sanitize(a.getAttribute("download"));
item.description = extractDescription(a);
return item; return item;
} }
urlToUsable(item, item.url); catch (ex) {
item.fileName = sanitize(a.getAttribute("download")); console.error("oopsed link", ex.toString(), ex);
item.description = extractDescription(a); }
return item; return null;
} }
*collectImagesInternal(img: HTMLImageElement) { *collectImageInternal(img: HTMLImageElement) {
try { try {
const src = img.currentSrc || img.src; const src = img.currentSrc || img.src;
const item = this.makeItem(src, img); const item = this.makeItem(src, img);
@ -140,8 +146,8 @@ class Gatherer {
} }
} }
collectImages(img: HTMLImageElement) { collectImage(img: HTMLImageElement) {
return [...this.collectImagesInternal(img)]; return [...this.collectImageInternal(img)];
} }
collectMediaInternal(title: string | undefined | null, el: HTMLMediaElement) { collectMediaInternal(title: string | undefined | null, el: HTMLMediaElement) {
@ -165,13 +171,19 @@ class Gatherer {
} }
collectMedia(el: HTMLMediaElement) { collectMedia(el: HTMLMediaElement) {
const item = this.collectMediaInternal(el.getAttribute("title"), el); try {
const rv = item ? [item] : []; const item = this.collectMediaInternal(el.getAttribute("title"), el);
const title: string | undefined = item && item.title || const rv = item ? [item] : [];
const title: string | undefined = item && item.title ||
el.getAttribute("title"); el.getAttribute("title");
rv.push(...Array.from(el.querySelectorAll("source")). rv.push(...Array.from(el.querySelectorAll("source")).
map(this.collectMediaInternal.bind(this, title))); map(this.collectMediaInternal.bind(this, title)));
return rv; return rv;
}
catch (ex) {
console.log("oopsed media", ex.toString(), ex);
}
return [];
} }
*findTexts() { *findTexts() {
@ -216,8 +228,14 @@ class Gatherer {
if (!this.textLinks) { if (!this.textLinks) {
return []; return [];
} }
return Array.from(this.findTextLinks()). try {
map(link => this.makeItem(link.href, link)); return Array.from(this.findTextLinks()).
map(link => this.makeItem(link.href, link));
}
catch (ex) {
console.error("oopsed textlinks", ex.toString(), ex);
}
return [];
} }
makeItem(surl: string, el: HTMLElement, title?: string | null): any { makeItem(surl: string, el: HTMLElement, title?: string | null): any {
@ -285,7 +303,7 @@ function gather(msg: any, sender: any, callback: Function) {
gatherer.collectTextLinks()), gatherer.collectTextLinks()),
media: gatherer.makeUniqueItems( media: gatherer.makeUniqueItems(
Array.from(document.querySelectorAll("img")). Array.from(document.querySelectorAll("img")).
flatMap(gatherer.collectImages), flatMap(gatherer.collectImage),
Array.from(document.querySelectorAll("video")). Array.from(document.querySelectorAll("video")).
flatMap(gatherer.collectMedia), flatMap(gatherer.collectMedia),
Array.from(document.querySelectorAll("audio")). Array.from(document.querySelectorAll("audio")).