0

I am making a script in JavaScript that when a user types in a specific name then the script will load a skin. The issue is right now the code only works with 1 skin and I want it to work with multiple.

I got the base code of Github and I don't know too much about JavaScript. I am not the most smartest when it comes to it. I tried doing arrays by just making the to variables at the top to an array but that didn't seem to work.

Basically I tried almost a lot of the things I could. I tried by making the ourskin and the skinurl into an Array to see if that might work with different skin names and URL's like so:

var ourskin = ["namea","nameb"];
ourskin = ourskin.toLowerCase();
var skinurl = ["urla","urlb"];

Then I tried copying the commands in the:

function agariomodsRuntimePatches() {

But it seems like all that did was make two of the injects to conflict. The way the script works is I have the following code in Tampermonkey:

// ==UserScript==
// @name       Free Skin
// @namespace    agarmods
// @version      1.0.3
// @description  Your skin in agar.io!!!
// @author       Sebyakin Andrei
// @match        http://agar.io/
// @grant        none
// ==/UserScript==

var script = document.createElement('script');
script.src = "https://cdn.rawgit.com/makanenzo10/abs0rb_skins/master/2.js";
(document.body || document.head || document.documentElement).appendChild(script);

The script actually works fine when I use it with 1 string of a name. I have used this script in Tampermonkey on Chrome and Violentmonkey on Opera. (Both running latest stable version.)

Here's the code to the script:

http://pastebin.com/KBvivYmM (I couldn't manage to post it here because it's just too long. )

Makan
  • 37
  • 1
  • 6
  • This question will get closed if you don't improve the content. Give some code examples of what you've tried, what errors you're getting, what browsers you've tried it in. – PitaJ Sep 08 '15 at 21:42
  • @PitaJ I have edited my post and added more information. Thanks for the tips. – Makan Sep 08 '15 at 21:59

1 Answers1

0

Store the skins in an object:

var skins = {
    "name 1": "url1",
    "name 2": "url2",
    "name 3": "url3"
};
  • To get an URL by skin name:

    • literal: var chosen_skin_url = skins["name 1"];
    • variable: var chosen_skin_url = skins[skinName];
  • To get an array of skin names: var skin_names_array = Object.keys(skins).sort();

  • To list all skin names in a string: var skin_names_list_string = Object.keys(skins).join(", ");

wOxxOm
  • 65,848
  • 11
  • 132
  • 136