Last active
March 30, 2016 15:36
-
-
Save firedfox/731505a4b76be30caf5c to your computer and use it in GitHub Desktop.
format url for phantom
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
/** | |
* @file format url for phantom | |
* @author firedfox | |
*/ | |
/* | |
* format url before opened by a WebPage instance | |
* | |
* @param {string} url web page url | |
* @return {string} url formatted | |
*/ | |
var formatURL = function (url) { | |
// removes whitespace | |
url = url.trim(); | |
// encode non-ASCII characters. necessary for urls including Chinese / Japanese, etc. | |
url = url.split('').map(function (character) { | |
return character.charCodeAt(0) <= 255 ? character : encodeURIComponent(character); | |
}).join(''); | |
// add default protocol | |
if (url.indexOf('http://') !== 0 && url.indexOf('https://') !== 0) { | |
url = 'http://' + url; | |
} | |
return url; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment