Faction Target Finder

Adds a button to the top of the page that opens a live raid target from the faction list.

As of 2024-10-10. See the latest version.

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 or Violentmonkey 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         Faction Target Finder
// @version      1.0.3
// @namespace    http://tampermonkey.net/
// @description  Adds a button to the top of the page that opens a live raid target from the faction list.
// @author       Omanpx [1906686]
// @license      MIT
// @match        https://www.torn.com/*
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';

    // These are faction IDs, listed in priority order
    // If no live target is found in the first one on the list, it moves on to the next
    // The current list includes Insignia + Med-Evil factions
    let facIDs = [50231,50157,50244,50609,50961,50586,50498,51275,50597,51684,50994,51668,50664,50194,50186,52471,50103,51562,51612,50406,51313,50273,50375,50272,50386,50328,50401,50216,51145,50433,50094];

    // Fetch API key from storage
    let apiKey = '';
    let maxLevel = 100;

    let storedAPIKey;

        storedAPIKey = localStorage.getItem('API');

    if (storedAPIKey) {
        apiKey = storedAPIKey;
    }
    // Set a maximum level of acceptable target
    let storedLevel;
        storedLevel = localStorage.getItem('LEVEL');

    if (storedLevel) {
        maxLevel = storedLevel;
    } else {
        storedLevel = localStorage.setItem('LEVEL',100);
    }
    // Prompt the user to enter their API key
    function promptAPIKey() {
        let enterAPIKey = null;
            enterAPIKey = prompt('Enter a public API key here:');

        if (enterAPIKey !== null && enterAPIKey.trim() !== '') {
            localStorage.setItem('API', enterAPIKey);
            alert('API key set! The page will now refresh.');
        } else {
            alert('No valid API key entered!');
        }
    }
    // Add the option to change settings
    function changeSettings() {
        // API entry
        let enterAPIKey = localStorage.getItem('API');
        enterAPIKey = prompt('Enter a public API key:',enterAPIKey);
        if (enterAPIKey !== null && enterAPIKey.trim() !== '') {
            localStorage.setItem('API', enterAPIKey);
            alert('API key set!');
        } else {
            alert('No valid API key entered!');
        }
        // LEVEL entry
        let LEVEL = localStorage.getItem('LEVEL');
        LEVEL = prompt('Enter the highest player level to look for:',LEVEL);
        if (LEVEL !== null && LEVEL >= 0 && LEVEL <= 100) {
            localStorage.setItem('LEVEL', LEVEL);
            alert(`Maximum level set to ${LEVEL}!`);
        } else {
            alert('Please enter a value between 0 and 100!');
        }
    }
    // Condition to check on each response
    function checkCondition(response) {
        let roster = JSON.parse(response.responseText);
        if ("error" in roster) {
            alert(`Failed fetching faction roster. Reason: ${roster.error.error}`);
            return 123456789;
        } else {
            for (let user in roster.members) {
                if (roster.members[user].level <= maxLevel & roster.members[user].status.state == "Okay") {
                    return user;
                    break;
                }
            }
        }
    }
    // Function to process each URL
    function processUrls(index) {
        if (index >= facIDs.length) {
            console.log("No players met the conditions.");
            return;
        }
        const ID = facIDs[index];
        console.log(`Checking faction ID: ${ID}`);
        console.log(Date.now());
        let url = `https://api.torn.com/faction/${ID}?selections=basic&timestamp=${Date.now()}&key=${apiKey}`;

        GM_xmlhttpRequest({
            method: "GET",
            url: url,
            onload: function(response) {
                if(checkCondition(response)>0 && checkCondition(response) !== 123456789){
                    console.log(checkCondition(response));
                    //let profileLink = `https://www.torn.com/profiles.php?XID=${checkCondition(response)}`;
                    // Uncomment below link to go to attack loader directly
                    let profileLink = `https://www.torn.com/loader.php?sid=attack&user2ID=${checkCondition(response)}`;
                    // Comment this line and uncomment the one below it if you want the profile to open in a new tab
                    window.location.href = profileLink;
                    //window.open(profileLink, '_blank');
                } else if (checkCondition(response) !== 123456789) {
                    processUrls(index + 1);
                }
            },
            onerror: function() {
                console.log(`Error loading URL: ${url}`);
                processUrls(index + 1);
            }
        });
    }

    // Create a button element
    const button = document.createElement('button');
    button.innerHTML = 'Raid';
    button.style.position = 'fixed';
    //button.style.top = '10px';
    //button.style.right = '10px';
    button.style.top = '27%'; // Adjusted to center vertically
    button.style.right = '0%'; // Center horizontally
    //button.style.transform = 'translate(-50%, -50%)'; // Center the button properly
    button.style.zIndex = '9999';

    // Add CSS styles for a green background
    button.style.backgroundColor = 'green';
    button.style.color = 'white';
    button.style.border = 'none';
    button.style.padding = '6px';
    button.style.borderRadius = '6px';
    button.style.cursor = 'pointer';
    // Create an API remove button
    const setSettings = document.createElement('button');
    setSettings.innerHTML = 'Settings';
    setSettings.style.position = 'fixed';
    //setSettings.style.top = '10px';
    //setSettings.style.right = '10px';
    setSettings.style.top = '32%'; // Adjusted to center vertically
    setSettings.style.right = '0%'; // Center horizontally
    //setSettings.style.transform = 'translate(-50%, -50%)'; // Center the setSettings properly
    setSettings.style.zIndex = '9999';

    // Add CSS styles for a green background
    setSettings.style.backgroundColor = 'orange';
    setSettings.style.color = 'black';
    setSettings.style.border = 'none';
    setSettings.style.padding = '3px';
    setSettings.style.borderRadius = '3px';
    setSettings.style.cursor = 'pointer';
    // Add a click event listener
    button.addEventListener('click', function() {
        if(localStorage.getItem('API')== null) {
            promptAPIKey();
            location.reload();
        } else {
            processUrls(0);
        }
    });
    setSettings.addEventListener('click', function() {
        changeSettings();
        location.reload();
    })
    // Add the button to the page
    document.body.appendChild(button);
    document.body.appendChild(setSettings);
})();