Last active
October 25, 2018 10:33
-
-
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]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
will become
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:
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.When that check is true, an error is thrown in the form of a warning dialog. (
notSoShowErrorMsg()
)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()
)Props to Angelos at 30secondsofcode.org for the basic "copy to clipboard" snippet.