Pirater For Web

Transforms all writing on your web browser to pirate speak, courtesy of https://github.com/sayhan1610/pirater api

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Pirater For Web
// @namespace    mailto:[email protected]
// @version      1.1.0
// @description  Transforms all writing on your web browser to pirate speak, courtesy of https://github.com/sayhan1610/pirater api
// @author       Christopher Body
// @match        *://*/*
// @icon         https://uxwing.com/wp-content/themes/uxwing/download/resize.php?size=512x512&file=ship-wheel-icon.png&category_slug=transportation-automotive
// @grant        none
// @license      N/A
// ==/UserScript==

/*
    Author: Christopher Body
    Github: https://github.com/cookiemonsternz
    Discord: cookiemonsternz
    Greasyfork: https://greasyfork.org/en/users/1397890-cookiemonsternz
*/

var texts = Array.from(document.getElementsByTagName('p'));

async function translateText(text) {
    // use promise.all to do concurrently
    try {
        const translations = await Promise.all(texts.map(async (elt) => {
            // skip empty text
            if (!elt.innerText.trim()) {
                return null;
            }

            const options = {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    text: elt.innerText
                })
            }

            try {
                const res = await fetch('https://pirater-api.onrender.com/translate/', options);
                if(!res.ok) {
                    throw new Error('HTTP error! status: ' + res.status);
                }
                const data = await res.json();
                return {element: elt, translation: data.pirate_translation};
            }
            catch (error) {
                console.error('Error:', error);
                return null;
            }
        }));

        translations.forEach(result => {
            if (result) {
                result.element.innerText = result.translation;
            }
        });
    }
    catch (error) {
        console.error('Error:', error);
    }
}

translateText(texts);