Skip to content

Instantly share code, notes, and snippets.

@samthecodingman
Last active October 25, 2018 10:33
Show Gist options
  • Save samthecodingman/c039bd88061f98c9fecb968d5c22319e to your computer and use it in GitHub Desktop.
Save samthecodingman/c039bd88061f98c9fecb968d5c22319e to your computer and use it in GitHub Desktop.
Copies the shortened URL for the AliExpress item on the current page [Javascript Bookmarklet] [CC-BY License]
javascript:(function(d,u){if(u.indexOf("aliexpress.com/item/")<0){alert("Error:%20You%20are%20not%20on%20the%20right%20AliExpress%20page.")}else{var e=d.createElement('textarea'),u=u.replace(/\/[\w-]+\/(\d+)\.html.*/,function(m,id){return '/-/'+id+'.html'});e.value=u;d.body.appendChild(e);e.select();d.execCommand('copy');d.body.removeChild(e);alert('"'+u+'"\nwas%20copied%20to%20the%20clipboard')}})(document,window.location.href)
@samthecodingman
Copy link
Author

samthecodingman commented Oct 15, 2018

AliExpress Item URL Shortener

What is it?

AliExpress uses really long URLs for items on its site. However there is an undocumented solution to this where you can replace the item name with a single hyphen. During normal use of the website, a number of random tracking markers can be added to the end of URLs that aren't the best for sharing on social media where space is at a premium. So this little script will trim the fat and return just the shortened URL.

https://www.aliexpress.com/item/Some-Really-Long-Item-Name-Containing-A-Catchy-Buy-Me-Message/01234567890.html?spm=jfnas.9042311.0.0.27424c4ddufgfd

will become

https://www.aliexpress.com/item/-/01234567890.html

How to use

To use this bookmarklet, add it as a Bookmark to your favourites bar of your browser under the 'URL' or 'Location' field. When clicked, the Javascript function will run and will copy the shortened URL form to the clipboard.

Why use a bookmarklet?

Well, in comparison to a full plugin, a bookmarklet consists of a small amount of text that just performs the function you need. It may not be as pretty as a plugin, but it doesn't require updating, doesn't have version incompatibilities and most importantly won't actually read any of the page's contents. So there is zero snooping/logging/tracking risk.

How it works

On the surface...
The bookmarklet is actually quite primitive and simplistic (albeit using some minification), consisting of some simple error checking using the ternary operator:

isThisAnAliExpressItem() ? notSoShowErrorMsg() : isSoCopyShortForm()

A more in depth look...
Using that analogy, isThisAnAliExpressItem() checks if the current page is an AliExpress item by searching for the substring "aliexpress.com/item/" in the page URL.

window.location.href.indexOf("aliexpress.com/item/")<0

When that check is true, an error is thrown in the form of a warning dialog. (notSoShowErrorMsg())

alert("Error:%20You%20are%20not%20on%20the%20right%20AliExpress%20page.")

If the page is an AliExpress item, the current page URL is manipulated so that the item's name (and any trailing tracking garbage) is removed and then the new URL is copied to the clipboard and an alert is shown to indicate success. (isSoCopyShortForm())

var e = document.createElement('textarea');
var shortUrl = window.location.href.replace(/\/[\w-]+\/(\d+)\.html.*/, function(match, itemId) {
  return '/-/' + itemId + '.html';
});
e.value = shortUrl;
document.body.appendChild(e);
e.select();
document.execCommand('copy');
document.body.removeChild(e);
alert('"' + shortUrl + '"\nwas%20copied%20to%20the%20clipboard');

Props to Angelos at 30secondsofcode.org for the basic "copy to clipboard" snippet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment