Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const pokeAPI = "https://pokeapi.co/api/v2/pokemon/";
- const refreshRate = 1000;
- const textColor = new Color("#FFFFFF");
- const backColor = new Color("#333333");
- const accentColor = new Color("#FF9800");
- const allowedPokemon = [
- "pikachu", "pichu", "charmander", "squirtle", "ditto", "ekans", "clefairy", "jigglypuff", "oddish", "paras", "meowth", "psyduck", "cubone", "koffing", "snorlax",
- ];
- const getRandomPokemon = async () => {
- const randomIndex = Math.floor(Math.random() * allowedPokemon.length);
- const pokemonName = allowedPokemon[randomIndex];
- const response = await new Request(`${pokeAPI}${pokemonName}`).loadJSON();
- return response;
- };
- const createWidget = async (pokemon) => {
- const list = new ListWidget();
- list.backgroundColor = backColor;
- list.setPadding(12, 12, 12, 12);
- const mainStack = list.addStack();
- mainStack.layoutVertically();
- mainStack.centerAlignContent();
- // Image
- const imageUrl = pokemon.sprites.other["official-artwork"].front_default;
- const imageRequest = new Request(imageUrl);
- const image = await imageRequest.loadImage();
- const imageStack = mainStack.addStack();
- imageStack.layoutHorizontally();
- imageStack.addSpacer();
- const imageItem = imageStack.addImage(image);
- imageItem.imageSize = new Size(75, 75);
- imageItem.cornerRadius = 10;
- imageStack.addSpacer();
- // Name
- const nameStack = mainStack.addStack();
- nameStack.layoutHorizontally();
- nameStack.addSpacer();
- const nameText = nameStack.addText(pokemon.name.charAt(0).toUpperCase() + pokemon.name.slice(1).toLowerCase());
- nameText.font = Font.boldSystemFont(18);
- nameText.textColor = textColor;
- nameText.centerAlignText();
- nameStack.addSpacer();
- // Abilities (Name and damage only, smaller font)
- const abilitiesStack = mainStack.addStack();
- abilitiesStack.layoutVertically();
- for (let i = 0; i < 2 && i < pokemon.abilities.length; i++) {
- const abilityName = pokemon.abilities[i].ability.name;
- const abilityUrl = pokemon.abilities[i].ability.url;
- const abilityResponse = await new Request(abilityUrl).loadJSON();
- const abilityDamageString = abilityResponse.effect_entries.find(entry => entry.language.name === 'en')?.short_effect;
- const abilityDamage = abilityDamageString ? extractDamageNumber(abilityDamageString) : "N/A";
- const abilitySubStack = abilitiesStack.addStack();
- abilitySubStack.layoutHorizontally();
- abilitySubStack.addSpacer();
- const abilityText = abilitySubStack.addText(`${abilityName} `);
- abilityText.font = Font.regularSystemFont(13);
- abilityText.textColor = accentColor;
- abilityText.centerAlignText();
- abilitySubStack.addSpacer();
- }
- return list;
- };
- // Helper function to extract damage number (if present)
- function extractDamageNumber(text) {
- const match = text.match(/(\d+) damage/i);
- return match ? match[1] : "";
- }
- const updateWidget = async () => {
- const pokemon = await getRandomPokemon();
- const widget = await createWidget(pokemon);
- if (!config.runsInWidget) {
- await widget.presentSmall();
- }
- Script.setWidget(widget);
- Script.complete();
- };
- (async () => {
- await updateWidget();
- const timer = new Timer();
- timer.timeInterval = refreshRate;
- timer.schedule({
- repeating: true,
- behavior: Timer.Behavior.ResetAfterScheduled,
- });
- timer.onFired = updateWidget;
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement