inherit
173855
0
Apr 23, 2024 9:59:44 GMT -8
Texas
I check in every once in a while...
869
November 2011
petermaggio
|
Post by Texas on Aug 8, 2013 7:32:55 GMT -8
So, I'm working on a plugin that will change {displayname} to @(username). The plugin works fine on my test board. But I'm sure how well it will work on a big forum like this. So I'm trying to run it via console. But for some reason I get an illegal string error. The illegal string error is being pulled up by this code below, and I don't see whats wrong with it.
$('textarea').each(function(){ $(this).keyup(function(){ var atdata = JSON.parse(localStorage.atperson); for (i = 0; i < atdata.length; i++) { var reg = RegExp('\{' + atdata[i].displayname + '\}', 'i'); if ( reg.test( $(this).val() ) ){ if (!$(this).val().match(atdata[i].atusername)) { $(this).val($(this).val().replace(reg, atdata[i].atusername)); } } } }); });
|
|
inherit
The Dream Crusher (Ret.)
164921
0
Apr 1, 2014 11:00:25 GMT -8
Tim Camara
Teach a man to fish, etc., etc.
1,721
March 2011
tcamara
|
Post by Tim Camara on Aug 8, 2013 9:47:07 GMT -8
In Chrome, I get 'SyntaxError: Unexpected token u'. Looks like it's trying to JSON.parse localStorage.atperson, but that variable is evaluating to 'undefined', which it then tries (and fails) to parse, since it's not JSON. Your code should probably do a definedness check before attempting to parse it, and you'll probably need to console magic some data into localStorage before attempting to run it on support.
|
|
inherit
173855
0
Apr 23, 2024 9:59:44 GMT -8
Texas
I check in every once in a while...
869
November 2011
petermaggio
|
Post by Texas on Aug 8, 2013 10:01:42 GMT -8
I ran the part that compiles the JSON into the localStorage. So it's there, that was the first thing I checked...shouldn't mentioned that. The full code is below. It also does do a definedness check...it's just in the other part of the code...which I didn't post....
$(document).ready(function(){ // Remember to update this or the code will not clear the localStorage.atperson which could cause problems if(localStorage.displaynameupdate != "v1.2.6"){ localStorage.atperson=undefined; localStorage.displaynameupdate = "v1.2.6"; } if (localStorage.atperson == "undefined" || localStorage.atperson == undefined || localStorage.atperson == null || localStorage.atperson == "null") { var atdata = atdata || []; localStorage.atperson = JSON.stringify(atdata); alert('true'); } $('a').each(function () { var atdata2 = JSON.parse(localStorage.atperson); if ($(this).prop('title').match(/@/)) { if (atdata2 == "") { atdata2.push({ "atusername" : $(this).prop('title'), "displayname" : $(this).text() }); } else { for (i = 0; i < atdata2.length; i++) { sessionStorage.exists = "no"; if ($(this).prop('title').match(atdata2[i].atusername)) { sessionStorage.exists = "yes"; atdata2[i].displayname = $(this).text(); break; } } if (sessionStorage.exists == "no") { atdata2.push({ "atusername" : $(this).prop('title'), "displayname" : $(this).text() }); } } localStorage.atperson = JSON.stringify(atdata2); } }); $('textarea').each(function (){ $(this).keyup(function (){ var atdata = JSON.parse(localStorage.atperson); for (i = 0; i < atdata.length; i++) { var reg = RegExp('\{' + atdata[i].displayname + '\}', 'i'); if ( reg.test( $(this).val() ) ){ if (!$(this).val().match(atdata[i].atusername)) { $(this).val($(this).val().replace(reg, atdata[i].atusername)); } } } }); }); });
|
|
inherit
The Dream Crusher (Ret.)
164921
0
Apr 1, 2014 11:00:25 GMT -8
Tim Camara
Teach a man to fish, etc., etc.
1,721
March 2011
tcamara
|
Post by Tim Camara on Aug 8, 2013 10:13:17 GMT -8
If I run it in the console (stripped out of the document.ready, ofc) it seems to run fine. Replaces my displayname properly and everything.
|
|
inherit
173855
0
Apr 23, 2024 9:59:44 GMT -8
Texas
I check in every once in a while...
869
November 2011
petermaggio
|
Post by Texas on Aug 8, 2013 10:30:56 GMT -8
Hmmm could be a FF error. But I'm not sure what's going on.
The reason I'm trying to test it here on support is when I first wrote this code ( several months ago ) I couldn't get it to work with all the special characters people use in there names. My coding experience has gone up a little since then so I decided to try it again. I'm trying to cache the usernames on the first couple pages of the "members" page and then run the code, but I'm getting that error for some reason.
EDIT: It was just a FF error but I think I've got it sorted.
|
|
inherit
The Dream Crusher (Ret.)
164921
0
Apr 1, 2014 11:00:25 GMT -8
Tim Camara
Teach a man to fish, etc., etc.
1,721
March 2011
tcamara
|
Post by Tim Camara on Aug 8, 2013 11:32:19 GMT -8
Okay, after pulling the first four or so pages of support members into localStorage, the error I get is:
Looks like you'll need to escape any characters that can mess up your regex. Also, I'd recommend using Chrome to debug JS issues, as it usually has the most in-depth error messages. (And don't even get me started on IE's debug: [Object] object)
|
|
inherit
173855
0
Apr 23, 2024 9:59:44 GMT -8
Texas
I check in every once in a while...
869
November 2011
petermaggio
|
Post by Texas on Aug 8, 2013 12:18:36 GMT -8
I got that too, I used try...catch to skip over the ones that aren't valid.
|
|