Javascript Bookmarklet - remove spaces from input - javascript

I am pretty new to this so please take it easy on me.
I have a javascript bookmark that I use to look up tracking number for parcels on Purolators website (Canadian shipping company). What I want it to do is to take the input (tracking number) and remove any spaces in it before opening the URL. The tracking site is stupid and uses spaces as delimiters for new tracking numbers.
Also, as a bonus, can this be made to open in a new tab?
javascript:var%20trackID%20=%20escape(prompt('Enter%20Tracking%20#'));window.location='https://eshiponline.purolator.com/SHIPONLINE/Public/Track/TrackingDetails.aspx?pin='%20+%20trackID;
I have tried adding trackID%20=%20trackID.replace(/\s+/g,%20''); before the window.location but no luck.
Any ideas?

You don't need to use escape in this scenario. This is actually replacing your spaces with %20 special characters which is why your replace wasn't working
Check http://www.w3schools.com/jsref/jsref_escape.asp for more info.
Your corrected code should be something like
javascript:var trackID=prompt('Enter%20Tracking%20#').replace(/ /g,"");window.location='https://eshiponline.purolator.com/SHIPONLINE/Public/Track/TrackingDetails.aspx?pin='+trackID;

Related

How to linkify the URL in a text?

I am using Html, Jquery and Javascript. I am using linkify javascript library and is working fine. But if there is no space before the URL then it does not work. How to solve this? I have below URL along with text.
Please click the linkhttp://www.asdfgh.com
Please see there is no space before the URL. How to linkify the URL here?Can it be done using Javascript?
don't Know linkify. Maybe there is an option for setting individual patterns.
Otherways you could search the text for this string and replace it, before linkify do its work. But its dirty.
yourtext=yourtext.replace("linkhttp://","link http://");

Javascript Bookmarklet to change the URL

Let's say the structure of the page URL is http://string.static.com/*
string may vary but will only consist of any combination of alphanumeric characters and dashes.
static.com is self-explanatory.
* could be anyting containing letters, numbers, /,:,? and similar characters, so basically there can be directories and parameters as well.
What I'm trying to achieve is this:
The bookmarklet should replace everyting after .com/... so the url becomes http://string.static.com/archive
I've created two (very similar) JS bookmarklets so far but I can't wrap my head around this one. It should be fairly simple but I haven't had any luck. Thanks in advance.

Programmatically removing newlines inside Google Documents

I wrote a trivial google apps script that runs thru a google document to divide pieces of text. Now, the text that my code need to divide seems to include newlines.
My problem is that I just don't understand how to remove these programmatically (I tried: mydoc.replaceText("/\n/",""); and mydoc.replaceText(/(\r\n|\n|\r)/gm,""); plus other similar variations with no success.
I can't comment but maybe something like this works for you? replaces whitespace with single spaces
mydoc.replaceText("/\s/"," ");

Twitter auto shorten URL not working

I am trying to add custom tweet button to my website and all is well but it is not shortening the URL. I am using URL query string instead of javascript. Is that the case?
<a class="btn btn-tweet" target="_blank" href="https://twitter.com/share?text='.$title.'&url='.$url.'&via='.$user.'&wrap_links=true">Twitter</a>
I have read some API document and saying above 19 characters twitter shorten url itself. Just wonder why it is not converting with this.
I am also having trouble to open in popup instead of new window. Does this all possible without adding any separate javascript code but directly inline in anchor tag?
I was hung up on this and realized I was trying to shorten "http://localhost:5000/blog/url" which t.co is unable to do.
Change to a real, non-localhost URL and it worked fine.
It won't be visibly shortened in the compose window, but the compose window does detect URLs and adjusts the character count accordingly. Try pasting a huge long URL - it'll only use up 22 characters in the count.
Do note that Twitter shortens all URLs, even when "shortening" actually makes them longer. For example, "http://bit.ly" will use up 22 characters (not 19), not 13.

Regex for URL's that don't appear in quotes? (javascript)

Currently I have a regular expression that will find all URL's within a block of html. It looks like this:
elementHTML.match(/(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,#?^=%&:\/~+#-]*[\w#?^=%&\/~+#-])?/ig);
When a URL is detected it is replaced with something that looks like:
<div data="URL_THAT_WAS_DETECTED">Information about that url</div>
The data attribute is custom added.
How can I continue to look for URL's without picking up the previously detected URL?
Ideally, I would like either ignore URL's that are in quotes or possibly html tags but I'm open to suggestions.
Any help is greatly appreciated, Thanks!
This regex will do it:
/(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+(?![^\s]*?")([\w.,#?^=%&:\/~+#-]*[\w#?^=%&\/~+#-])?/ig
I've added a negative lookahead for double quotes " characters.
Live Demo

Categories

Resources