Skip to content

Commit

Permalink
New: Also Replace Small Images
Browse files Browse the repository at this point in the history
  • Loading branch information
FullLifeGames committed Sep 10, 2021
1 parent 2e2150a commit 933cfda
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 13 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ Any issue or merge request is appreciated.

![Example](images/UnfinishedExample.png)

See a gif version of it [here](images/UnfinishedExample.gif)!

## Feedback

As previously said, any feedback or bug report is welcomed in the [Issues](https://github.com/FullLifeGames/ShowdownHDSpriteMod/issues) tab.
As previously said, any feedback or bug report is welcomed in the [Issues](https://github.com/FullLifeGames/ShowdownHDSpriteMod/issues) tab.

## Including the Furret Turret Sprites

Expand Down
Binary file added images/UnfinishedExample.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/UnfinishedExample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ShowdownHDSpriteMod",
"version": "0.0.1",
"version": "1.2.0",
"description": "This project is a mod that replaces the default Showdown Sprites with HD Sprites from PkParaíso.",
"keywords": [],
"main": "dist/ShowdownHDSpriteMod.umd.js",
Expand All @@ -19,7 +19,7 @@
"node": ">=10.0.0"
},
"scripts": {
"lint": "eslint src/**/*.ts",
"lint": "eslint --fix src/**/*.ts",
"prebuild": "rimraf dist",
"build": "tsc --module commonjs && rollup -c rollup.config.ts && typedoc --out docs --target es6 --theme minimal --mode file src",
"start": "rollup -c rollup.config.ts -w",
Expand Down
86 changes: 76 additions & 10 deletions src/ShowdownHDSpriteMod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// @include https://*.psim.us/
// @include http://*.psim.us/*
// @include https://*.psim.us/*
// @version 1.0.4
// @version 1.2.0
// @grant none
// @run-at document-end
// ==/UserScript==
Expand All @@ -27,6 +27,8 @@

const resultList = new Map<string, { exists: boolean }>();

const animatedSmallSprites = true;

// Check the README for usage
const furretTurretPath = 'http://localhost:8080/';

Expand Down Expand Up @@ -96,7 +98,7 @@
return [monsName, monsGif.substring(monsName.length)];
}

function setHdImage(pokemonImage: HTMLImageElement, fullMonsGif: string) {
function setHdImageSrc(pokemonImage: HTMLImageElement, fullMonsGif: string) {
if (
pokemonImage.src !== fullMonsGif &&
(pokemonImage.src.indexOf('pokemonshowdown') !== -1 ||
Expand All @@ -107,7 +109,24 @@
}
}

async function checkAndSetHdImage(pokemonImage: HTMLImageElement, monsGif: string) {
function setHdImageStyle(miniPokemonImage: HTMLSpanElement, fullMonsGif: string) {
if (
miniPokemonImage.style.background !== `url(${fullMonsGif})` &&
(miniPokemonImage.style.background.indexOf('pokemonshowdown') !== -1 ||
miniPokemonImage.style.background.indexOf('psim.us') !== -1)
) {
miniPokemonImage.style.background = `url(${fullMonsGif})`;
miniPokemonImage.style.backgroundSize = 'contain';
miniPokemonImage.style.backgroundPosition = 'center';
miniPokemonImage.style.backgroundRepeat = 'no-repeat';
}
}

async function checkAndSetHdImage<HTMLElementImpl>(
pokemonImage: HTMLElementImpl,
monsGif: string,
setImage: (pokemonImage: HTMLElementImpl, fullMonsGif: string) => void
) {
const monsGifs = hdImagePaths.map(
(hdImagePath) =>
hdImagePath +
Expand All @@ -116,7 +135,7 @@
for (let fullMonsGif of monsGifs) {
if (resultList.has(fullMonsGif)) {
if (resultList.get(fullMonsGif)?.exists) {
setHdImage(pokemonImage, fullMonsGif);
setImage(pokemonImage, fullMonsGif);
break;
}
continue;
Expand All @@ -136,7 +155,7 @@
}
if (exists !== null) {
if (exists) {
setHdImage(pokemonImage, fullMonsGif);
setImage(pokemonImage, fullMonsGif);
resultList.set(fullMonsGif, { exists: true });
break;
} else {
Expand All @@ -156,31 +175,78 @@
const pokemonImage = pokemonImages[i] as HTMLImageElement;
if (!hdImagePaths.some((hdImagePath) => pokemonImage.src.indexOf(hdImagePath) !== -1)) {
if (pokemonImage.src.indexOf('sprites/ani/') !== -1) {
const monsGif = pokemonImage.src.substr(
let monsGif = pokemonImage.src.substr(
pokemonImage.src.indexOf('sprites/ani/') + 'sprites/ani/'.length
);
checkAndSetHdImage(pokemonImage, monsGif);
monsGif = monsGif.replace('-f', '');
checkAndSetHdImage(pokemonImage, monsGif, setHdImageSrc);
} else if (pokemonImage.src.indexOf('sprites/ani-back/') !== -1) {
let monsGif = pokemonImage.src.substr(
pokemonImage.src.indexOf('sprites/ani-back/') + 'sprites/ani-back/'.length
);
monsGif = monsGif.replace('.gif', '-back.gif');
monsGif = monsGif.replace('-f', '');
checkAndSetHdImage(pokemonImage, monsGif);
checkAndSetHdImage(pokemonImage, monsGif, setHdImageSrc);
} else if (pokemonImage.src.indexOf('sprites/ani-shiny/') !== -1) {
let monsGif = pokemonImage.src.substr(
pokemonImage.src.indexOf('sprites/ani-shiny/') + 'sprites/ani-shiny/'.length
);
monsGif = monsGif.replace('.gif', '-s.gif');
monsGif = monsGif.replace('-f', '');
checkAndSetHdImage(pokemonImage, monsGif);
checkAndSetHdImage(pokemonImage, monsGif, setHdImageSrc);
} else if (pokemonImage.src.indexOf('sprites/ani-back-shiny/') !== -1) {
let monsGif = pokemonImage.src.substr(
pokemonImage.src.indexOf('sprites/ani-back-shiny/') + 'sprites/ani-back-shiny/'.length
);
monsGif = monsGif.replace('.gif', '-back-s.gif');
monsGif = monsGif.replace('-f', '');
checkAndSetHdImage(pokemonImage, monsGif);
checkAndSetHdImage(pokemonImage, monsGif, setHdImageSrc);
}
}
}

if (animatedSmallSprites) {
const miniPokemonImages = $('.picon.has-tooltip');
for (let i = 0; i < miniPokemonImages.length; i++) {
const miniPokemonImage = miniPokemonImages[i] as HTMLSpanElement;
if (
!hdImagePaths.some(
(hdImagePath) => miniPokemonImage.style.background.indexOf(hdImagePath) !== -1
)
) {
let pokemonName = miniPokemonImage.attributes.getNamedItem('aria-label')?.value;

if (pokemonName !== undefined) {
pokemonName = pokemonName.toLowerCase();

pokemonName = pokemonName.replace(' (active)', '');
pokemonName = pokemonName.replace(' (fainted)', '');
pokemonName = pokemonName.replace(' (tox)', '');
pokemonName = pokemonName.replace(' (brn)', '');
pokemonName = pokemonName.replace(' (par)', '');
pokemonName = pokemonName.replace(' (frz)', '');

if (pokemonName.indexOf('%') !== -1) {
for (const seperation of pokemonName.split('(')) {
if (seperation.indexOf('%') !== -1) {
pokemonName = pokemonName.replace(
'(' + seperation.substr(0, seperation.indexOf(')') + 1),
''
);
break;
}
}
}

if (pokemonName.indexOf('(') !== -1) {
pokemonName = pokemonName.substr(pokemonName.indexOf('(') + 1);
pokemonName = pokemonName.substr(0, pokemonName.indexOf(')'));
}

pokemonName = pokemonName.trim();

checkAndSetHdImage(miniPokemonImage, `${pokemonName}.gif`, setHdImageStyle);
}
}
}
}
Expand Down

0 comments on commit 933cfda

Please sign in to comment.