mirror of
https://github.com/chylex/Discord-History-Tracker.git
synced 2025-04-14 07:37:13 +03:00
Fix viewer image detection and file name parsing when the URL includes a query
This commit is contained in:
parent
176a81e055
commit
cd418f4871
@ -78,6 +78,12 @@ const DISCORD = (function() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const isImageUrl = function(url) {
|
||||||
|
const dot = url.pathname.lastIndexOf(".");
|
||||||
|
const ext = dot === -1 ? "" : url.pathname.substring(dot).toLowerCase();
|
||||||
|
return ext === ".png" || ext === ".gif" || ext === ".jpg" || ext === ".jpeg";
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
setup() {
|
setup() {
|
||||||
templateChannelServer = new TEMPLATE([
|
templateChannelServer = new TEMPLATE([
|
||||||
@ -176,9 +182,8 @@ const DISCORD = (function() {
|
|||||||
},
|
},
|
||||||
|
|
||||||
isImageAttachment(attachment) {
|
isImageAttachment(attachment) {
|
||||||
const dot = attachment.url.lastIndexOf(".");
|
const url = DOM.tryParseUrl(attachment.url);
|
||||||
const ext = dot === -1 ? "" : attachment.url.substring(dot).toLowerCase();
|
return url != null && isImageUrl(url);
|
||||||
return ext === ".png" || ext === ".gif" || ext === ".jpg" || ext === ".jpeg";
|
|
||||||
},
|
},
|
||||||
|
|
||||||
getChannelHTML(channel) { // noinspection FunctionWithInconsistentReturnsJS
|
getChannelHTML(channel) { // noinspection FunctionWithInconsistentReturnsJS
|
||||||
@ -235,11 +240,14 @@ const DISCORD = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return value.map(attachment => {
|
return value.map(attachment => {
|
||||||
if (this.isImageAttachment(attachment) && SETTINGS.enableImagePreviews) {
|
const url = DOM.tryParseUrl(attachment.url);
|
||||||
|
|
||||||
|
if (url != null && isImageUrl(url) && SETTINGS.enableImagePreviews) {
|
||||||
return templateEmbedImage.apply({ url: attachment.url, src: attachment.url });
|
return templateEmbedImage.apply({ url: attachment.url, src: attachment.url });
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const sliced = attachment.url.split("/");
|
const path = url == null ? attachment.url : url.pathname;
|
||||||
|
const sliced = path.split("/");
|
||||||
|
|
||||||
return templateAttachmentDownload.apply({
|
return templateAttachmentDownload.apply({
|
||||||
"url": attachment.url,
|
"url": attachment.url,
|
||||||
|
@ -51,4 +51,15 @@ class DOM {
|
|||||||
const date = new Date(timestamp);
|
const date = new Date(timestamp);
|
||||||
return date.toLocaleDateString() + ", " + date.toLocaleTimeString();
|
return date.toLocaleDateString() + ", " + date.toLocaleTimeString();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a url string into a URL object and returns it. If the parsing fails, returns null.
|
||||||
|
*/
|
||||||
|
static tryParseUrl(url) {
|
||||||
|
try {
|
||||||
|
return new URL(url);
|
||||||
|
} catch (ignore) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user