View previous topic :: View next topic |
Author |
Message |
gogodr I post too much
Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Thu Jun 10, 2010 4:09 pm Post subject: Javascript help |
|
|
does anyone finds something wrong with this script?
it just doesnt want to work...
UPDATE:
fixed misspelling and missing }
now hen it runs FF just freezes I think it may be because of the while loop
UPDATE2: it still freezes even if there are no quotes..
Code: |
// ==UserScript==
// @name adding text styling test
// @namespace http://forum.cheatengine.org/*
// @include *forum.cheatengine.org/*
// ==/UserScript==
var postButton = document.forms.namedItem("post").elements.namedItem("post");
var newmsg = "";
var oldmsg = "";
function FLQ(){
var evaluationMessage = document.forms.namedItem("post").elements.namedItem("message");
var originalMessage = document.forms.namedItem("post").elements.namedItem("message");
var newmsgpos;
if (evaluationMessage.value.search("[/quote]") != -1){
while (evaluationMessage.value.search("[/quote]") != -1 ){
newmsgpos = evaluationMessage.value.search("[/quote]");
evaluationMessage.value.replace("[/quote]","[/oldquote]");
}
newmsg = originalMessage.value.substring(newmsgpos + 8);
oldmsg = originalMessage.value.substring(0,newmsgpos + 8);
}
else if (evaluationMessage.value.search("[/quote]") != -1){
oldmsg = "";
newmsg = evaluationMessage.value
}
}
function styling(newmsg,oldmsg){
FLQ();
var postMessage = document.forms.namedItem("post").elements.namedItem("message");
postMessage.value=oldmsg+"[color=darkblue][size=9] "+newmsg+" [/size][/color]";
}
postButton.addEventListener("click", styling, true);
|
Last edited by gogodr on Thu Jun 10, 2010 8:16 pm; edited 3 times in total |
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu Jun 10, 2010 6:43 pm Post subject: |
|
|
looks like you're missing a curly brace.
i spot two {, but only one closing }
|
|
Back to top |
|
 |
gogodr I post too much
Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Thu Jun 10, 2010 6:48 pm Post subject: |
|
|
slovach wrote: | looks like you're missing a curly brace.
i spot two {, but only one closing } |
test
you were right there was a } missing
but it is still not working..
I just added it and updated the OP
---- fixed that minor things. but now it freezes
|
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu Jun 10, 2010 8:22 pm Post subject: |
|
|
ok.
i looked it up and the string search returns -1 if there is no match.
Code: | if (evaluationMessage.value.search("[/quote]") != -1){
while (evaluationMessage.value.search("[/quote]") != -1 ){ |
if there isn't a match, start looping while there isn't a match. looks like an infinite loop.
|
|
Back to top |
|
 |
gogodr I post too much
Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Thu Jun 10, 2010 8:27 pm Post subject: |
|
|
slovach wrote: | ok.
i looked it up and the string search returns -1 if there is no match.
Code: | if (evaluationMessage.value.search("[/quote]") != -1){
while (evaluationMessage.value.search("[/quote]") != -1 ){ |
if there isn't a match, start looping while there isn't a match. looks like an infinite loop. |
what is inside changes the evaluated variable
newmsgpos = evaluationMessage.value.search("[/quote]");
evaluationMessage.value.replace("[/quote]","[/oldquote]");
its basically searching for the position of the last quote.
and
Code: | if (evaluationMessage.value.search("[/quote]") != -1){
while (evaluationMessage.value.search("[/quote]") != -1 ){ |
is start looping if there is a match. is it is not -1 it means there is a match
oh wait I noticed I made else if != -1 x_X
Last edited by gogodr on Thu Jun 10, 2010 8:37 pm; edited 1 time in total |
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu Jun 10, 2010 8:37 pm Post subject: |
|
|
Code: | if (evaluationMessage.value.search("[/quote]") != -1){
while (evaluationMessage.value.search("[/quote]") != -1 ){
newmsgpos = evaluationMessage.value.search("[/quote]");
evaluationMessage.value.replace("[/quote]","[/oldquote]");
} |
if there is no match for "[/quote]" then...
loop while there is no match for "[/quote]"
ok, then inside the while loop, it looks like newmsgpos will simply end up as -1. you've done the same search 3 times in a row and it's failed the first 2 attempts, the 3rd has no reason to be different.
so if "[/quote]" didn't exist in the beginning, it still doesn't exist now.
replace() is probably returning "[/quote]" i assume, but you're not doing anything with it.
so the loop seems to just run forever.
at least this is how i figure it. never done javascript.
|
|
Back to top |
|
 |
gogodr I post too much
Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Thu Jun 10, 2010 8:51 pm Post subject: |
|
|
it is pretty much my first time in javascript but it should be like this != is <>
and when search gives -1 it means that there are no matches
Code: | if (evaluationMessage.value.search("[/quote]") != -1){ |
if the search on evaluationMessage doesn't end in no matches then
Code: | newmsgpos = evaluationMessage.value.search("[/quote]");
evaluationMessage.value.replace("[/quote]","[/oldquote]"); |
I'm writting newmsgpos before doing the last replace to it stores the last [/quote] position before replacing it.
|
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu Jun 10, 2010 9:40 pm Post subject: |
|
|
oops that was my bad, was trying to play a game and write that at the same time and mixed myself up. i'm not normally that retarded i swear.
so, if there is a match, loop while there is a match. something here is redundant either way, you do the same search 3 times before finally taking the result...
i don't have what you're doing infront of me, so i'm taking a stab in the dark somewhat, but i would think you could just rewrite it as...
edit: give me a sec
|
|
Back to top |
|
 |
gogodr I post too much
Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Thu Jun 10, 2010 9:54 pm Post subject: |
|
|
I have been trying to figure out another method to do it but I just cant find another one... the javascript string search is pretty dumb... it should find all the matches and give you an array but noooo it only gives you the first match, same for replace, only replaces the first match.
|
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu Jun 10, 2010 10:24 pm Post subject: |
|
|
Code: | var newmsg = "";
var oldmsg = "";
function FLQ()
{
var msg = document.forms.namedItem("post").elements.namedItem("message");
var msgpos = msg.value.search("[/quote]"); //first search
newmsg = msg.value; //newmsg = the original msg text... i think
while(msgpos != -1) //so, while matching
{
msg.value.replace("[/quote]", "[/oldquote]");
msgpos = msg.value.search("[/quote]");
}
} |
seems right in my head, would replace every [/quote] with [/oldquote]. i have no idea if the syntax is valid.
i'm not entirely sure what you were going for with the substrings.
edit: apparently you can just use regular expressions with the replace function.
http://www.tizag.com/javascriptT/javascript-string-replace.php
scroll down, i didn't at first.
|
|
Back to top |
|
 |
gogodr I post too much
Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Thu Jun 10, 2010 10:51 pm Post subject: |
|
|
its not right.. with that one
msgpos will end up being -1 no matter what
maybe you dont understand what I'm trying to do ?
FLQ == Find Last Quote
msgpos == Message Position
I dont want to replace the [/quotes] but to separate the text after the last quote and before the last quote.
|
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu Jun 10, 2010 11:06 pm Post subject: |
|
|
no, i wasn't entirely sure what you were doing.
and msgpos should indeed end up something else eventually once all the quotes got gobbled.
but anyway: http://www.w3schools.com/jsref/jsref_obj_string.asp
in particular:
lastIndexOf() Returns the position of the last found occurrence of a specified value in a string.
sounds useful to me.
|
|
Back to top |
|
 |
gogodr I post too much
Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Thu Jun 10, 2010 11:10 pm Post subject: |
|
|
slovach wrote: | no, i wasn't entirely sure what you were doing.
and msgpos should indeed end up something else eventually once all the quotes got gobbled.
but anyway: http://www.w3schools.com/jsref/jsref_obj_string.asp
in particular:
lastIndexOf() Returns the position of the last found occurrence of a specified value in a string.
sounds useful to me. |
That ! That is what I was looking for! Thank you.
--------------
k changed approach, now it doesnt freezes but it doesnt work ....
Code: | // ==UserScript==
// @name adding text styling test
// @namespace http://forum.cheatengine.org/*
// @include *forum.cheatengine.org/*
// ==/UserScript==
var postButton = document.forms.namedItem("post").elements.namedItem("post");
function styling(){
var newmsg = "";
var oldmsg = "";
var postMessage = document.forms.namedItem("post").elements.namedItem("message");
var newmsgpos = 0;
if (parseInt(postMessage.value.search("[/quote]")) = -1){
newmsg = postMessage.value;
oldmsg = "";
}
else if (postMessage.value.search("[/quote]") != -1){
newmsgpos = postMessage.value.lastIndexOf("[/quote]");
newmsg = postMessage.value.substring(newmsgpos + 8);
oldmsg = postMessage.value.substring(0,newmsgpos + 8);
}
postMessage.value=oldmsg+"[color=darkblue][size=9] "+newmsg+" [/size][/color]";
}
postButton.addEventListener("click", styling, true); |
|
|
Back to top |
|
 |
HolyBlah Master Cheater
Reputation: 2
Joined: 24 Aug 2007 Posts: 446
|
Posted: Fri Jun 11, 2010 11:05 am Post subject: |
|
|
if (parseInt(postMessage.value.search("[/quote]")) == -1){
|
|
Back to top |
|
 |
Haxory' Grandmaster Cheater Supreme
Reputation: 92
Joined: 30 Jul 2007 Posts: 1900
|
|
Back to top |
|
 |
|