views:

85

answers:

4

I'm injecting a script via JSONP and using it to call a method in my web application like so:

(function jsonp(src){
    var b = document.body;
    var t = document.title;
    var u = encodeURI(document.location.href);

    var o = document.getElementById('srv-call');
    o && b.removeChild(o);

    var s = document.createElement('script');   
    s.id = 'srv-call';
    s.src = src + '?w=' + textSelection + '&cb=autoCall&u=' + u + '&pt=' + t + '&t=' + (new Date().getTime());
    b.appendChild(s);
})('http://localhost:8888/wordmark/words/add_word');

Unfortunately, my document.title is getting filled with non-breaking spaces. An example http request is this:

http://localhost:8888/wordmark/words/add_word?w=problems&cb=autoCall&u=http://www.boingboing.net/2010/10/01/kid-demonstrates-eng.html&pt=%E2%80%8BK%E2%80%8Bi%E2%80%8Bd%E2%80%8B%20%E2%80%8Bd%E2%80%8Be%E2%80%8Bm%E2%80%8Bo%E2%80%8Bn%E2%80%8Bs%E2%80%8Bt%E2%80%8Br%E2%80%8Ba%E2%80%8Bt%E2%80%8Be%E2%80%8Bs%E2%80%8B%20%E2%80%8BE%E2%80%8Bn%E2%80%8Bg%E2%80%8Bl%E2%80%8Bi%E2%80%8Bs%E2%80%8Bh%E2%80%8B%20%E2%80%8Bl%E2%80%8Ba%E2%80%8Bn%E2%80%8Bg%E2%80%8Bu%E2%80%8Ba%E2%80%8Bg%E2%80%8Be%E2%80%8B%20%E2%80%8Bi%E2%80%8Bn%E2%80%8B%20%E2%80%8B2%E2%80%8B4%E2%80%8B%20%E2%80%8Ba%E2%80%8Bc%E2%80%8Bc%E2%80%8Be%E2%80%8Bn%E2%80%8Bt%E2%80%8Bs%E2%80%8B%20%E2%80%8B-%E2%80%8B%20%E2%80%8BB%E2%80%8Bo%E2%80%8Bi%E2%80%8Bn%E2%80%8Bg%E2%80%8B%20%E2%80%8BB%E2%80%8Bo%E2%80%8Bi%E2%80%8Bn%E2%80%8Bg&t=1285982312594

The script that is injected in the page has the correct src, but the HTTP request is incorrect. Any idea why these are being inserted and if I have any way to avoid this, other than parsing them out via regex?

Thanks so much for any help you can give.

+1  A: 

Have you tried with decodeURIComponent(t) instead of just t?

s.src = src + '?w=' + textSelection + '&cb=autoCall&u=' + u + '&pt=' + decodeURIComponent(t) + '&t=' + (new Date().getTime());
Sebastián Grignoli
Unfortunately, even when using decodeURIComponent, I get the same URL in the HTTP request.
Joshua Cody
That's because you are putting spaces in the url and they are being encoded by the browser. It's OK, the server you are requesting to will decode them itself, and it will see your title as it is.
Sebastián Grignoli
So I would expect something like `Kid%20demonstrates%20English%20language%20in%2024%20accents%20-%20Boing%20Boing` once I encoded this, but it's not doing this. It's placing zero-width spaces between **every single** character. Am I misunderstanding?
Joshua Cody
+1  A: 

what you need to do is take the variable t off in your line that says

s.src = src + '?w=' + textSelection + '&cb=autoCall&u=' + u + '&pt=' + t + '&t=' + (new Date().getTime());

so your link would look something like this instead:

http://localhost:8888/wordmark/words/add_word?w=problems&cb=autoCall&u=http://www.boingboing.net/2010/10/01/kid-demonstrates-eng.html&&t=1285982312594

and if you must have the t variable then insert it into the line like so

....(code before) '&pt=' + decodeURIComponent(t) + (code after)......

Hope this helps. thanks

PK

Pavan
I do need to pass the title as a query string, so I can't simply remove it, and I'm not having any luck with decodeURIComponent, either. Any other ideas? Thanks so much for your help.
Joshua Cody
A: 

Those are not non-breaking spaces, but zero-width spaces (U+200B). They are normally not visible, and may be present in the original title (for text wrapping, or whatever other reason).

Piet Delport
Thanks for the clarification Piet. They seem to be present on most pages I'm trying, and I've no idea why. This makes sense as it's being inserted as `\u200b` in my database. Do I have options for removing this?
Joshua Cody
+1  A: 

And I just realized the culprit. I apologize for wasting everyone's time, but in the event anyone else runs across this problem, the issue was the SMRT Safari Extension to alter Safari's URL auto-complete feature. -1 for me for not disabling all extensions and trying multiple browsers. Thanks, all.

Joshua Cody
+1 for posting the response and documenting an obscure issue that Google will be able to answer now.
Sebastián Grignoli