javascript function to encode everything?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Trahan

    #1

    javascript function to encode everything?

    I need a javascript function to hex-encode a plus sign so I can
    pass the plus sign as an argument in a GET request. escape() and
    encodeURI() don't do it (and probably shouldn't, because '+' is
    a valid character in a URI). I could write a regexp, but the
    environment I'm in won't allow replace(). I could write something
    cumbersome using indexOf, but if there's something easier, like
    a builtin function that encodes every character in the string, I'd
    prefer that. I'm a newbie at js, so type slowly, please.

  • Thomas 'PointedEars' Lahn

    #2
    Re: javascript function to encode everything?

    Richard Trahan wrote:
    [color=blue]
    > I need a javascript function to hex-encode a plus sign so I can
    > pass the plus sign as an argument in a GET request. escape() and
    > encodeURI() don't do it (and probably shouldn't, because '+' is
    > a valid character in a URI). I could write a regexp, but the
    > environment I'm in won't allow replace(). I could write something
    > cumbersome using indexOf, but if there's something easier, like
    > a builtin function that encodes every character in the string, I'd
    > prefer that.[/color]

    There is not.
    [color=blue]
    > I'm a newbie at js, so type slowly, please.[/color]

    How many characters per minute are allowed in this newsgroup?
    I cna tpye 500 wrods pre mnitue!!1 ;-)

    Seriously, I'm afraid there is no easy solution, especially because the
    available methods of your implementation are virtually unknown. Maybe
    you are looking for this:

    /**
    * @author
    * (C) 2001-2004 Thomas Lahn <string.js@Poin tedEars.de>
    * Distributed under the GNU GPL v2.
    * @partof
    * http://pointedears.de/scripts/string.js
    * @argument _ sText
    * @argument string sReplaced
    * @argument string sReplacement
    * @return type string
    */
    function replaceText(sTe xt, sReplaced, sReplacement)
    {
    var result = "";
    var t;
    if (!sText && sText.construct or == String)
    {
    sText = this;
    }

    var sNewText = sText;
    var a;
    // alert(sText);
    if (sText && sReplaced && sReplacement)
    {
    if (sText.replace)
    {
    sReplaced = sReplaced.repla ce(/\\/g, "\\\\");
    /* Version 1.23.2002.4 bugfix: allows to replace \ with other
    * strings, required for proper rxReplaced;
    * Example (no quotes, no escaping):
    * sReplaced (provided) "\\"
    * sReplaced (evaluated) \
    * sReplaced (replaced as formulated above) "\\\\"
    * sReplaced (esc. in RegExp constructor) "\\\\"
    * sReplaced (ev. in RegExp constructor) \\
    * rxReplaced (with RegExp escaping) /\\/g
    * rxReplaced (evaluated) all occurr. of \
    */
    var rxReplaced = new RegExp(sReplace d, "g");
    sText = sText.replace(r xReplaced, sReplacement);

    result = sText;
    }
    else if (sText.split
    && (sReplaced = sReplaced.repla ce(/\\/g, "\\\\"))
    && (a = sText.split(sRe placed))
    && a.join)
    {
    result = a.join(sReplace ment);
    }
    else
    {
    var i = sText.indexOf(s Replaced);

    if (i > -1)
    {
    sNewText = sText.substring (0, i);
    sNewText += sReplacement
    + replaceText(
    sText.substring (i + sReplaced.lengt h),
    sReplaced,
    sReplacement);
    }

    result = sNewText;
    }
    }

    return result;
    }

    var f = this.encodeURI || this.escape || function dummy(s) { return s; };
    var plusEscape = "%" + "+".charCodeAt( 0).toString(16) .toUpperCase();
    var x = replaceText(f(" foo"), "+", plusEscape);

    This will use the return value of encodeURI() or escape(), if available,
    to escape the usual characters (why reinvent the wheel?) which is passed
    to replaceText() which uses two alternatives if String.prototyp e.replace()
    is unavailable:

    A) sText.split(sRe placed).join(sR eplacement)
    B) calling the method recursively until every occurrence was replaced

    You wrote that String.prototyp e.replace() is not available. This method was
    introduced in JavaScript 1.2, so your implementation may be of the level of
    JavaScript 1.1. String.prototyp e.split() and Array.prototype .join() are
    available there, so it is likely that your implementation will use
    alternative A.


    HTH

    PointedEars

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: javascript function to encode everything?

      [Previous posting canceled because of flaws in the implementation]

      Richard Trahan wrote:
      [color=blue]
      > I need a javascript function to hex-encode a plus sign so I can
      > pass the plus sign as an argument in a GET request. escape() and
      > encodeURI() don't do it (and probably shouldn't, because '+' is
      > a valid character in a URI). I could write a regexp, but the
      > environment I'm in won't allow replace(). I could write something
      > cumbersome using indexOf, but if there's something easier, like
      > a builtin function that encodes every character in the string, I'd
      > prefer that.[/color]

      There is not.
      [color=blue]
      > I'm a newbie at js, so type slowly, please.[/color]

      How many characters per minute are allowed in this newsgroup?
      I cna tpye 500 wrods pre mnitue!!1 ;-)

      Seriously, I'm afraid there is no easy solution, especially because the
      available methods of your implementation are virtually unknown. Maybe
      you are looking for this:

      var f = this.encodeURI || this.escape || function dummy(s) { return s; };
      var plusEscape = "%" + "+".charCodeAt( 0).toString(16) .toUpperCase();
      var x = f("foo").split( "+").join(plusE scape);

      This will use the return value of encodeURI() or escape(), if available,
      to escape the usual characters in "foo" (why reinvent the wheel?). Then
      the "+" character is replaced with its escape sequence.

      You wrote that String.prototyp e.replace() is not available. This method
      was introduced in JavaScript 1.2, so your implementation may be of the
      level of JavaScript 1.1. String.prototyp e.split() and
      Array.prototype .join() are available there, so it is likely that this will
      work.


      HTH

      PointedEars

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: javascript function to encode everything?

        Thomas 'PointedEars' Lahn wrote:
        [color=blue]
        > Richard Trahan wrote:[color=green]
        >> I need a javascript function to hex-encode a plus sign so I can
        >> pass the plus sign as an argument in a GET request. escape() and
        >> encodeURI() don't do it (and probably shouldn't, because '+' is
        >> a valid character in a URI). [...][/color]
        >
        > [...]
        > You wrote that String.prototyp e.replace() is not available. This method
        > was introduced in JavaScript 1.2, so your implementation may be of the
        > level of JavaScript 1.1.[/color]

        Which makes me wonder why it understands then encodeURI()
        as of ECMAScript 3. Are you sure about replace()?


        PointedEars

        Comment

        • Richard Trahan

          #5
          Re: javascript function to encode everything?

          Thomas 'PointedEars' Lahn wrote:[color=blue]
          > Which makes me wonder why it understands then encodeURI()
          > as of ECMAScript 3. Are you sure about replace()?
          >[/color]
          I thank you profusely for your assistance, so I guess I
          owe you an explanation about the replace(). It's not a
          matter of js version. It's going to execute within an
          eBay ad. eBay software will not allow js in an ad that
          can modify text in such a way that simple software can't
          scan the new text for forbidden phrases.
          That means no replace() and no <script> tags
          with a src attribute, which is why the method in your
          first post won't work, but the method in your second
          post will probably be ok. Anyway, I'm using a workaround
          until I understand js a bit more; instead of using ?x=y
          syntax and CGI to unpack the lone argument,
          I'm using ?arg syntax and using ENV{QUERY_STRIN G} in the
          Perl script that reads the GET, in which case any
          character at all can be passed (it's the CGI::param method
          that throws away the plus sign).

          I'm determined to defeat eBay's restriction. They have a
          nasty habit of changing ad requirements almost weekly, which
          causes scores of people to manually edit thousands of ads
          because the ad content cannot be served remotely. One possibility
          is to reduce everything to images, but the file sizes would
          be too large for folks with dialups. Another possibility is
          to encode text characters disguised as image bytes (steganography) ,
          but I don't know if js can unpack an image into its bytes.
          Do you know?



          Comment

          • Michael Winter

            #6
            Re: javascript function to encode everything?

            On Fri, 13 Aug 2004 08:34:41 GMT, Richard Trahan <rtrahan@optonl ine.net>
            wrote:

            [snip]
            [color=blue]
            > Another possibility is to encode text characters disguised as imagebytes
            > (steganography) , but I don't know if js can unpack an imageinto its
            > bytes.
            > Do you know?[/color]

            No, it can't.

            Back to the original encoding question...

            Whilst encodeURI() won't escape a plus (+) symbol, encodeURICompon ent()
            will. As you acknowledge, a plus symbol is valid within a URI, globally,
            but the components (scheme, host, path, etc.) do not allow them.

            Of course, this is of little use with older browsers, but I thought it
            worth mentioning.

            Mike

            --
            Michael Winter
            Replace ".invalid" with ".uk" to reply by e-mail

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: javascript function to encode everything?

              Richard Trahan wrote:
              [color=blue]
              > Thomas 'PointedEars' Lahn wrote:[color=green]
              >> Which makes me wonder why it understands then encodeURI()
              >> as of ECMAScript 3. Are you sure about replace()?
              >>[/color]
              > I thank you profusely for your assistance,[/color]

              I thank *you*. I seldom get such kind responses.
              [color=blue]
              > so I guess I owe you an explanation about the replace(). It's not a matter
              > of js version. It's going to execute within an eBay ad. eBay software will
              > not allow js in an ad that can modify text in such a way that simple
              > software can't scan the new text for forbidden phrases.[/color]

              ACK
              [color=blue]
              > That means no replace() and no <script> tags with a src attribute, which
              > is why the method in your first post won't work,[/color]

              It was flawed anyway which is why I finally canceled the posting.
              [color=blue]
              > but the method in your second post will probably be ok.[/color]

              Hopefully.
              [color=blue]
              > Anyway, I'm using a workaround until I understand js a bit more; instead
              > of using ?x=y syntax and CGI to unpack the lone argument, I'm using ?arg
              > syntax and using ENV{QUERY_STRIN G} in the Perl script that reads the GET,
              > in which case any character at all can be passed (it's the CGI::param
              > method that throws away the plus sign).[/color]

              If you have the choice, you should always prefer the server-side approach
              (which can be server-side JS, too) above the client-side one since
              client-side scripting can be restricted or disabled by the the user or not
              even present, while you often have full control over your server-side app.
              Client-side scripting comes in handy when roundtrips/traffic should be
              reduced; it is nothing that should be relied upon on a Web site.
              [color=blue]
              > I'm determined to defeat eBay's restriction. They have a
              > nasty habit of changing ad requirements almost weekly, which
              > causes scores of people to manually edit thousands of ads
              > because the ad content cannot be served remotely. One possibility
              > is to reduce everything to images, but the file sizes would
              > be too large for folks with dialups.[/color]

              Yes, please do not do that. I remember the Web was not as much fun when I
              only had 56k dial-up a few months before because many sites were optimized
              (I'd say pessimized[tm]) for broadband use. I effectively had to disable
              all images (or have much time) for reasonable usage.
              [color=blue]
              > Another possibility is to encode text characters disguised as image bytes
              > (steganography) , but I don't know if js can unpack an image into its
              > bytes. Do you know?[/color]

              I'm afraid JS itself cannot, it needs a host object to do so. As the host
              environment to provide such an object is the Web browser and there is more
              than one of them, there is no way this can be reliably implemented on an
              *Internet* Web site.


              PointedEars

              Comment

              • fhof

                #8
                Re: javascript function to encode everything?

                [color=blue][color=green]
                > > I need a javascript function to hex-encode a plus sign so I can
                > > pass the plus sign as an argument in a GET request. escape() and
                > > encodeURI() don't do it (and probably shouldn't, because '+' is
                > > a valid character in a URI). I could write a regexp, but the
                > > environment I'm in won't allow replace(). I could write something
                > > cumbersome using indexOf, but if there's something easier, like
                > > a builtin function that encodes every character in the string, I'd
                > > prefer that.[/color][/color]
                [color=blue]
                > There is not.[/color]

                YES, there is : encodeURICompon ent()
                That's the "strong" version of encodeURI()

                I Hope I am not too late
                Florian Ho


                -
                fho

                Comment

                Working...