Last active
April 15, 2019 18:35
-
-
Save mitche50/baf06d23d596409330890221ce3af53b to your computer and use it in GitHub Desktop.
A javascript redirect to deep links from GET parameters for the Nano cryptocurrency.
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
Copyright 2019 Andrew Mitchell | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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
<script> | |
//Convert from Scientific Notation to Standard Notation | |
Number.prototype.toFixedSpecial = function(n) { | |
var str = this.toFixed(n); | |
if (str.indexOf('e+') < 0) | |
return str; | |
// if number is in scientific notation, pick (b)ase and (p)ower | |
return str.replace('.', '').split('e+').reduce(function(p, b) { | |
return p + Array(b - p.length + 2).join(0); | |
}) + '.' + Array(n + 1).join(0); | |
}; | |
window.onload = function() { | |
var params = new URLSearchParams(window.location.search.slice(1)); | |
var account = params.get('address'); | |
var amount = params.get('amount'); | |
var memo = params.get('memo'); | |
var uri = "nano:" + account; | |
if (amount !== null) { | |
var raw_amount = parseFloat(amount) * "1000000000000000000000000000000"; | |
var converted_str = raw_amount.toFixedSpecial(0); | |
uri += "?amount=" + converted_str.slice(0, -1); | |
} | |
if (memo !== null) { | |
if (amount !== null) { | |
uri += "&memo=" + memo; | |
} else { | |
uri += "?memo=" + memo; | |
} | |
} | |
//Deep link URL for existing users with app already installed on their device | |
if (account !== null) { | |
//Only redirect for mobile devices | |
var isMobile = { | |
Android: function() { | |
return navigator.userAgent.match(/Android/i); | |
}, | |
BlackBerry: function() { | |
return navigator.userAgent.match(/BlackBerry/i); | |
}, | |
iOS: function() { | |
return navigator.userAgent.match(/iPhone|iPad|iPod/i); | |
}, | |
Opera: function() { | |
return navigator.userAgent.match(/Opera Mini/i); | |
}, | |
Windows: function() { | |
return navigator.userAgent.match(/IEMobile/i); | |
}, | |
any: function() { | |
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows()); | |
} | |
}; | |
if (isMobile.any()) { | |
window.location = uri; | |
}; | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment