Skip to content

A minimalist chat script that adds a modern UI with automatic message translation, country detection, and flag display. Features IP-based geolocation, translation caching, anti-flood protection, avatar rendering with frame support, and dual output for HTML and plain-text environments.

Notifications You must be signed in to change notification settings

lexicon06/iOSPill-Script.JS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

Code Refactoring - DRY Improvements

Main Issues Fixed

1. Duplicate Translation Functions ❌ → ✅

Before: You had two nearly identical functions:

  • traduzirAutomatico() - for Portuguese
  • traduzirParaES() - for Spanish

After: Created a single reusable function:

function traduzirPara(texto, idioma, onSuccess)

This eliminates ~40 lines of duplicate code!


2. Duplicate Message Sending Functions ❌ → ✅

Before: You had two separate functions:

  • enviarTraducao() - for translated messages
  • enviarSemTraducao() - for non-translated messages

After: Created a single unified function:

function enviarMensagem(user, original, traduzido)

The function automatically detects if translation is needed by comparing strings.


3. Repeated User Data Extraction ❌ → ✅

Before: This code was repeated in multiple places:

var nome = escaparHTML(user.name);
var countryCode = paisUsuario[user.externalIp] || "UN";
var emoji = bandeiraEmoji(countryCode);
var avatar = "(•‿•)";

After: Created helper functions:

function getUserCountryCode(user)
function getUserDisplayData(user)

4. Magic Strings & Hardcoded Values ❌ → ✅

Before: URLs and values scattered throughout:

"http://ip-api.com/json/" + ip + "?fields=countryCode"
"http://translate.googleapis.com/translate_a/single?..."
"https://flagcdn.com/w20/" + countryCode

After: Centralized configuration:

var CONFIG = {
    intervaloAvatar: 15,
    molduraURL: "http://i.imgur.com/9uYxq8W.png",
    apiURLs: {
        geo: "http://ip-api.com/json/",
        translate: "http://translate.googleapis.com/translate_a/single",
        flag: "https://flagcdn.com/w20/"
    }
};

5. Repeated URI Encoding ❌ → ✅

Before:

var uri = stripColors(textoOriginal);
uri = encodeURIComponent(uri);

After: Combined into single line in traduzirPara():

var uri = encodeURIComponent(stripColors(texto));

Key Benefits

Reduced from ~300 lines to ~240 lines (20% reduction)

Easier to maintain - Change translation logic once, not twice

Less error-prone - No risk of updating one function but forgetting the other

More flexible - Easy to add new languages (just call traduzirPara(texto, "fr", callback))

Better organization - Related constants grouped together

Clearer intent - Function names clearly describe what they do


Usage Examples

Adding a new language:

// Old way: Write another 30-line function
// New way: One line!
traduzirPara(texto, "fr", function(traducao) {
    // handle French translation
});

Changing flag API:

// Old way: Find and replace in multiple places
// New way: Change once in CONFIG
CONFIG.apiURLs.flag = "https://new-api.com/flags/";

What Stayed the Same

  • All functionality is preserved
  • No changes to the visual output
  • Same API/event structure
  • Same caching logic
  • Same performance characteristics

About

A minimalist chat script that adds a modern UI with automatic message translation, country detection, and flag display. Features IP-based geolocation, translation caching, anti-flood protection, avatar rendering with frame support, and dual output for HTML and plain-text environments.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published