Topeka
Junior Member
Posts: 94
Topeka said 0 great things
|
Post by Topeka on Mar 7, 2010 19:41:15 GMT -8
Browser tested: IE and FFPlace in Global Footer This will find a random letter in a display name, pick a random color, and give that random letter that color. <script type="text/javascript"> <!-- //random color for random letter in name //by Topeka /*edited to now also work by group if need you do not need to fill in both, if you just want username, or just want groups then only fill that part in*/ /*Wormos RegExp idea*/
usrnamei=/user=(topeka|test|user4|wormo)$/; groupidi=/(group2|group3|group4|group15)$/;
for (myn=document.getElementsByTagName('a'), z=0; z<myn.length; z++) { if ((myn[z].className.match(/group/i) && myn[z].href.match(usrnamei)) || myn[z].className.match(groupidi)) {
//finds random color code var chars = "0123456789ABCDEF"; var string_length = 6; var randomstring = ''; for (var i=0; i<string_length; i++) { var rnum = Math.floor(Math.random() * chars.length); randomstring += chars.substring(rnum,rnum+1); }
//finds random letter if (myn[z].firstChild.nodeType=="1") { fix=myn[z].firstChild } else { fix=myn[z] } i=Math.floor(Math.random()*fix.innerHTML.length); ltr=fix.innerHTML.substring(i,i+1) if (!fix.innerHTML.match(/font/i)) { fix.innerHTML=fix.innerHTML.replace(ltr,'<font color="#'+randomstring+ '">' +ltr+ '</font>'); } } } // --> </script>
preview: none yet This is my first time that i have submitted a code, so dont be that big of a critic.
|
|
|
Post by Wormopolis on Mar 7, 2010 21:12:03 GMT -8
running live preview on Topeka's name in this thread.
|
|
|
Post by Wormopolis on Apr 3, 2010 20:43:34 GMT -8
one thing that would make this code better, is if instead of it running a loop for each name in the name array everytime it encounters a displayname, that it uses regexp. you set up the regexp correctly, then compare the username in the display names href to that regexp.
it will be MUCH faster especially if they have a bunch of names in the usernamei list.
|
|
Topeka
Junior Member
Posts: 94
Topeka said 0 great things
|
Post by Topeka on Apr 4, 2010 8:59:50 GMT -8
ill have to look at how to do it with regexp. never used regexp alot but ill work on it. also had the idea to add a groupid along with username. i have one that works for groups & user. but i never presuded it, i just fixed it for that one forum. might add that with the regexp if i can figure it out.
Edit: are you sorta saying that instead of it running a loop for each name in the name array everytime it encounters a displayname, (as you said) to run a loop once, and if the regexp matches one of the names, that it will give it a random letter/color?
|
|
|
Post by Wormopolis on Apr 4, 2010 12:08:39 GMT -8
currently you have it:
loop a: go through each link - compare if its a display name loop b: at each name, go through each entry in the array and match it
lets say there was 40 names on the page, and 20 entries in the name array. thats 800 iterations it has to do before finishing.
if you use regexp, you only have to run 40 iterations.
namechecks=/user=^(admin|user2|user3|user4|wormo)$/; if (link.href.match(namechecks)) do color stuff.
if you wanted to add in the group check, you just need a second regexp and an OR in the if statement to check for name or group id.
|
|
Topeka
Junior Member
Posts: 94
Topeka said 0 great things
|
Post by Topeka on Apr 4, 2010 16:59:20 GMT -8
hmm... i've never done that before. i gota look up how you got this.
i can probably get it to work from what you've shown. i just wanna know how you've shown it.
Edit: I did get it to work but. i cannot figure out why you have the carrot '^' there. after the user=
i saw this... but its not helping. I dont see how its relivent but its the only thing that shows a ^ in RegExp.
Quantifier ^n Matches any string with n at the beginning of it.
<script type="text/javascript"> //random color for random letter in name //by Topeka /*edited to now also work by group if need you do not need to fill in both, if you just want username, or just want groups then only fill that part in*/ /*Wormos RegExp idea*/
usrnamei=/user=^(admin|topeka|test|user4|wormo)$/; groupidi=/(group1|group2|group3|group4|group15)$/;
for (myn=document.getElementsByTagName('a'), z=0; z<myn.length; z++) { if ((myn[z].className.match(/group/i) && usrnamei == myn[z].href.match(usrnamei)) || myn[z].className.match(groupidi)) {
//finds random color code var chars = "0123456789ABCDEF"; var string_length = 6; var randomstring = ''; for (var i=0; i<string_length; i++) { var rnum = Math.floor(Math.random() * chars.length); randomstring += chars.substring(rnum,rnum+1); }
//finds random letter if (myn[z].firstChild.nodeType=="1") { fix=myn[z].firstChild } else { fix=myn[z] } i=Math.floor(Math.random()*fix.innerHTML.length); ltr=fix.innerHTML.substring(i,i+1) if (!fix.innerHTML.match(/font/i)) { fix.innerHTML=fix.innerHTML.replace(ltr,'<font color="#'+randomstring+ '">' +ltr+ '</font>'); } } } </script>
|
|
|
Post by Wormopolis on Apr 5, 2010 0:18:58 GMT -8
the caret makes it so that the match starts at the bginning of the words inside the parenthesis. it took me a bit to undertsand them as well, but there is a GOOD reason.
lets say you had in the regexp: /(admin|catman|berkie|joe)$/
and you had two users, one named joe, and another named superjoe
superjoe would match to the regexp because joe is inside of it.
joemama wouldnt, because the mama part goes against the $, which is the opposite of ^, meaning the end of the match.
|
|
Topeka
Junior Member
Posts: 94
Topeka said 0 great things
|
Post by Topeka on Apr 5, 2010 8:43:40 GMT -8
o ok. so if i had it as regexp /^(admin|joe|dad|phill)$/ administrator wouldnt work due to the $ yourdad wouldnt work due to the ^ and imjoeson wouldnt work due to both ^ and $ ^ making the word start with it. and $ making it end with it. so it has to be equal to match
Very nice. lol thanks wormo
|
|
|
Post by Wormopolis on Apr 5, 2010 16:34:23 GMT -8
ok well it looks like I owe an apology. you already have some stuff in the regexp BEFORE the stuff in the ( ).. so its already basically got a "start of sting" bit it compares with.
using the examples I pasted above would be the case IF there want the "user=" part in there. like if we wanted to search for JUST a username we would need the ^, however since there is more to the beginning, the caret isnt necessary. the $ still would be however in order to keep names like joesupremem from matching to joe.
also, in your if statement you had a regexp == match thing going on. that just needs to be the match.
the final code would be:
<script type="text/javascript"> <!-- //random color for random letter in name //by Topeka /*edited to now also work by group if need you do not need to fill in both, if you just want username, or just want groups then only fill that part in*/ /*Wormos RegExp idea*/
usrnamei=/user=(topeka|test|user4|wormo)$/; groupidi=/(group2|group3|group4|group15)$/;
for (myn=document.getElementsByTagName('a'), z=0; z<myn.length; z++) { if ((myn[z].className.match(/group/i) && myn[z].href.match(usrnamei)) || myn[z].className.match(groupidi)) {
//finds random color code var chars = "0123456789ABCDEF"; var string_length = 6; var randomstring = ''; for (var i=0; i<string_length; i++) { var rnum = Math.floor(Math.random() * chars.length); randomstring += chars.substring(rnum,rnum+1); }
//finds random letter if (myn[z].firstChild.nodeType=="1") { fix=myn[z].firstChild } else { fix=myn[z] } i=Math.floor(Math.random()*fix.innerHTML.length); ltr=fix.innerHTML.substring(i,i+1) if (!fix.innerHTML.match(/font/i)) { fix.innerHTML=fix.innerHTML.replace(ltr,'<font color="#'+randomstring+ '">' +ltr+ '</font>'); } } } // --> </script>
and in doing this.. I somehow erased ALL of the scripts I had in my footer for the submissions board.. which sucks.
|
|
Topeka
Junior Member
Posts: 94
Topeka said 0 great things
|
Post by Topeka on Apr 6, 2010 11:44:06 GMT -8
its all good. lol. atleast i know what the ^n is used for now. so apology accepted. lol
nice catch though. i just copied this line from the first script if (usrnamei == myn[z].href.split('user=')[1]) { and changed what was needed. i wasnt really thinking about the (username == .match(username))
O man. haha sorry. i did that once only to the main footer. Wasnt my best day... but luckly i had almost everything that was erased in another board.
|
|
|
Post by Wormopolis on Apr 6, 2010 14:37:45 GMT -8
turns out it wasnt me. Support had quite a fe wposts about people suddenyl losing all their scripts in whatever headers they were working on.
Im tankful for 1. having a back up for the submissions area scripts 2. that I wasnt working on the global headers of which I DIDNT have a current backup
the modified code is currently running in the thread so I will be moving it over to the database shortly.
|
|
Topeka
Junior Member
Posts: 94
Topeka said 0 great things
|
Post by Topeka on Apr 6, 2010 17:29:57 GMT -8
haha. wish i could of blamed my 'miss-hap' before on them, but working fast, highlighting one part, (not knowing it was everything) pressing delete, and submitting.. well.. it cant quite be anyone else's fault but mine. lol
But anyway. cool. Thanks for the idea and help on the script.
|
|
|
Post by Streamstrider on Oct 18, 2011 20:22:52 GMT -8
Do you mind if I ask about having only one color for a random letter in the name? Kind of like Wormo's. XD I thought about restricting the color range to just 0 for black, but I wasn't sure if there was a better way or not. XD
|
|
|
Post by Streamstrider on Nov 12, 2011 21:29:10 GMT -8
*pokes* Wormo? *twiddles fingers*
|
|
|
Post by Wormopolis on Nov 12, 2011 22:12:52 GMT -8
so the same color for every name it gets called on?
|
|