Javascript match function problem

Discussion in 'JavaScript' started by prakashr85, Dec 18, 2012.

  1. #1
    The following is my greasemonkey script I have text "ABC-2105" in my text file in a line and in the website I have "ABC-210" the will contain like this " ACTD:ABC-210 " it is actually matching both text and getting the text value I dont know what went wrong. Also I have included ignorecache true but still it gets from cache. Any help is much appreciated. Thanks in advance.

    function trimAll(sString)
    {
    while (sString.substring(0,1) == ' ')
    {
    sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
    sString = sString.substring(0,sString.length-1);
    }
    return sString;
    }
    GM_xmlhttpRequest({
        method: 'GET',
        url: 'http://*******/newscript/Test.txt',
        ignoreCache: true,
        headers:
                {
                    'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
                    'Accept':'text/plain',
                    },
        onload: function(responseDetails)
        {
             
             var atcid = new Array();
             atcid = responseDetails.responseText.split("\n");
             var tempo =document.getElementsByTagName('p')[6].textContent;
             var cid = tempo.split(":")
             var tc = trimAll(cid[1]);
             var tempcid = tc.split(" ")
             var datcid = tempcid[0];
             var logo = document.createElement("div");
             logo.style.color = 'red';
             var text = document.createTextNode(" None ");
             for (x in atcid)
             {
             var str = trimAll(atcid[x]);
             var ttcid = trimAll(datcid);
             var result = str.match(ttcid);
             if (result != null)
              {text.textContent = ' ' +atcid[++x];
              break;}
             else{ ++x;
              text.textContent = ' ';}
             }
    Code (markup):

     
    prakashr85, Dec 18, 2012 IP
  2. AlaskaWebDesign

    AlaskaWebDesign Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I need help on what you are trying to accomplish? Your question isn't obvious to me.
     
    AlaskaWebDesign, Jan 8, 2013 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    Like AlaskaWebDesign I'm not sure I'm following what it is you are trying to do... though your brute-forcing trim instead of using... TRIM with a replacement for IE is a bit wonky... slow... it's just bad.

    I would rip that trimall function out, add this bit of code:
    
    if (!String.prototype.trim) {
    	String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g, '');
    };
    Code (markup):
    To implement TRIM in IE 8/lower.

    Then you would use it thus:

    var str = atcid[x].trim();
    var ttcid = datcid.trim();

    You don't have to brute force that.

    Also that we aren't seeing the entire code, have no clue what your data is, etc, etc... it's a bit hard to figure out what you are doing in there... like is datacid ever actually changed after being set outside the loop? If so why trim inside the loop? Are you sure you want to store x being added when you are immediately breaking out of the loop after? You do know that legacy browsers (ok, browser, IE) will choke on "for (x in atcid)", and it's dangerous to use that form in all browsers if you are arbitrarily changing the value inside the loop? I'd probably also not rely on the sixth P always being the one you want -- makes you have to go in and edit if you change the markup -- there's a reason to use ID's... and textContent is unreliable too... and are you sure you want to overwrite it every time it loops instead of appending to the end?

    You're making a lot of variables in certain scopes that really don't belong wasting memory as variables since, well... they're not used. Inside that for loop for example... I'm guessing here since we don't have the full code for the onload method, but:

    
    		var
    			atcid = responseDetails.responseText.split("\n"),
    			tempo = document.getElementsByTagName('p')[6].textContent,
    			cid = tempo.split(':'),
    			tc = 	cid[1].trim(),
    			tempcid = tc.split(' '),
    			datcid = tempcid[0],
    			ttcid = datacid.trim(),
    			text = document.createTextNode(
    				atcid.length==0 ? ' None ' : ' '
    			),
    			logo = document.createElement('div');
    			logo.style.color = 'red';
    			for (var x=0; x<atcid.length; x++) {
    				if (str.match(atcid[x].trim()) {
    					text.textContent = ' ' +atcid[x+1];
    					break;
    				}
    			}
    
    Code (markup):
    Would be a start to cleaning that up -- though it's unclear if you are using those values or not later in the method... generally speaking if you are declaring them as VAR inside the loop, they probably shouldn't be relied on existing after the loop... and good code 101, don't do anything inside a loop you don't have to.

    You also don't need to say VAR so much -- comma's exist for a reason :D

    Do you have a generic sample of the data being processed? I've got the feeling you've made this far more complex than it needs to be.
     
    deathshadow, Jan 14, 2013 IP