我终于设法将Jeffrey Thomas的Objective-c 版本转换为JavaScript,并且成功了。URLTokenDecode
这是功能:
function URLTokenDecode(token) {
if (token.length == 0) return null;
// The last character in the token is the number of padding characters.
var numberOfPaddingCharacters = token.slice(-1);
// The Base64 string is the token without the last character.
token = token.slice(0, -1);
// '-'s are '+'s and '_'s are '/'s.
token = token.replace(/-/g, '+');
token = token.replace(/_/g, '/');
// Pad the Base64 string out with '='s
for (var i = 0; i < numberOfPaddingCharacters; i++)
token += "=";
return token;
}
如果你使用 AngularJS,这里是 $filter:
app.filter('URLTokenDecode', function () {
return function (token) {
if (token.length == 0) return null;
// The last character in the token is the number of padding characters.
var numberOfPaddingCharacters = token.slice(-1);
// The Base64 string is the token without the last character.
token = token.slice(0, -1);
// '-'s are '+'s and '_'s are '/'s.
token = token.replace(/-/g, '+');
token = token.replace(/_/g, '/');
// Pad the Base64 string out with '='s
for (var i = 0; i < numberOfPaddingCharacters; i++)
token += "=";
return token;
}
});