Skip to content

Commit

Permalink
mediaTweaks: add No WebP and No Thumbnail Size
Browse files Browse the repository at this point in the history
  • Loading branch information
Cynosphere committed Oct 15, 2024
1 parent 23855d9 commit dcd2865
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/mediaTweaks/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ExtensionWebpackModule, Patch } from "@moonlight-mod/types";
import { createElement } from "@moonlight-mod/wp/react";

export const patches: Patch[] = [
// Image URLs
Expand Down Expand Up @@ -58,6 +59,15 @@ export const patches: Patch[] = [
`${createElement}(require("mediaTweaks_enlargeVideoButton").default,${props}),`
}
]
},

// No WebP and No Thumbnail Size
{
find: /\(.{1,2}\+="\?"\+.{1,2}\.stringify\(.{1,2}\)\)/,
replace: {
match: /if\((.)\.sourceWidth<.\.targetWidth\){/,
replacement: (orig, props) => `require("mediaTweaks_imagePropsProcessor").default(${props});${orig}`
}
}
];

Expand All @@ -68,6 +78,10 @@ export const webpackModules: Record<string, ExtensionWebpackModule> = {

enlargeVideoButton: {
dependencies: [{ id: "react" }, { ext: "spacepack", id: "spacepack" }, { id: "discord/components/common/index" }]
},

imagePropsProcessor: {
dependencies: []
}
};

Expand Down
27 changes: 23 additions & 4 deletions src/mediaTweaks/manifest.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
{
"$schema": "https://moonlight-mod.github.io/manifest.schema.json",
"id": "mediaTweaks",
"version": "1.0.1",
"version": "1.0.2",
"meta": {
"name": "Media Tweaks",
"tagline": "Various tweaks to images and videos. Every feature togglable.",
"authors": ["Cynosphere"],
"tags": ["qol", "chat"],
"authors": [
"Cynosphere"
],
"tags": [
"qol",
"chat"
],
"source": "https://github.com/Cynosphere/moonlight-extensions"
},
"dependencies": ["spacepack"],
"dependencies": [
"spacepack"
],
"settings": {
"imageUrls": {
"displayName": "Image URLs",
Expand Down Expand Up @@ -39,6 +46,18 @@
"description": "Adds a button to open videos in the image preview modal",
"type": "boolean",
"default": true
},
"noWebp": {
"displayName": "No WebP",
"description": "Disables fetching WebP versions of images for better clarity",
"type": "boolean",
"default": true
},
"noThumbnailSize": {
"displayName": "No Thumbnail Size",
"description": "Removes size parameters from thumbnails. **This will cause your client to freeze in channels with lots of high resolution images in them**",
"type": "boolean",
"default": false
}
},
"apiLevel": 2
Expand Down
34 changes: 34 additions & 0 deletions src/mediaTweaks/webpackModules/imagePropsProcessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const filetypeWhitelist = [".gif", ".mov", ".mp4", ".webm", ".webp"];

type ImageProps = {
src: string;
sourceWidth: number;
sourceHeight: number;
targetWidth: number;
targetHeight: number;
format?: string;
quality?: number;
animated?: boolean;
srcIsAnimated?: boolean;
};

export default function processProps(props: ImageProps) {
if (moonlight.getConfigOption<boolean>("mediaTweaks", "noWebp") ?? true) {
let whitelisted = false;
for (const type of filetypeWhitelist) {
if (props.src.indexOf(type) > -1) {
whitelisted = true;
break;
}
}

if (props.format === "webp" && !whitelisted && !props.animated && !props.srcIsAnimated) {
props.format = undefined;
}
}

if (moonlight.getConfigOption<boolean>("mediaTweaks", "noThumbnailSize") ?? false) {
props.targetWidth = props.sourceWidth * window.devicePixelRatio;
props.targetHeight = props.sourceHeight * window.devicePixelRatio;
}
}

0 comments on commit dcd2865

Please sign in to comment.