Javascript Spotify应用程序Api-encodeURI/escape
当使用JavaScript函数encodeURI/escape和encodeURI组件时,似乎有一个bug。例如:Javascript Spotify应用程序Api-encodeURI/escape,javascript,api,urlencode,spotify,Javascript,Api,Urlencode,Spotify,当使用JavaScript函数encodeURI/escape和encodeURI组件时,似乎有一个bug。例如: escape( 'The Frames' ) // The 0.000000rames encodeURI( 'The Frames' ) // The 0.000000rames encodeURIComponent( 'The Frames' ) // The 0.000
escape( 'The Frames' ) // The 0.000000rames
encodeURI( 'The Frames' ) // The 0.000000rames
encodeURIComponent( 'The Frames' ) // The 0.000000rames
注释显示输出。在Spotify之外的任何浏览器中按预期运行此代码(将空格替换为+或%20)
其他人能确认这是一个bug吗?还是我做错了什么。。。?有没有地方可以报告Spotify应用程序的bug
编辑:显然,上面的示例按预期工作。但是,将它们合并到alert()中会显示一个混乱的字符串,而事实上这是正常的。来自:
编码字符串
为确保应用程序不会以可能不安全的方式使用字符串,Spotify API提供的所有字符串都经过编码,以便意外误用不会导致注入漏洞。如果应用程序没有使用下面描述的两种方法对这些字符串进行解码,那么这些字符串将作为垃圾显示给用户。唯一的例外是URI,它从不编码,因此不需要解码。API文档说明了每个方法必须解码或不解码的字符串。
JavaScript字符串中添加了两个方法:decodeForText()和decodeForHTML()。如果字符串旨在以安全的方式使用,例如设置innerText或使用document.createTextNode()创建文本节点,则应使用DecodeFortText()。它将返回一个原始的非转义字符串,因此请确保它从未插入任何将被解释为HTML的上下文中。如果字符串被嵌入到innerHTML或任何将被嵌入为HTML的代码段中,则必须使用decodeForHTML()。它将确保
getElementById('song-title').innerHTML=track.title.decodeForHTML();
getElementById('song-title').innerText=track.title.decodeForText();
getElementById('song-title').appendChild(document.createTextNode(track.title.decodeForText());
不使用这些方法的应用程序将a)无法显示来自Spotify API的元数据或任何其他数据,b)将在上载过程中被拒绝。另外,请确保您可以从不安全的HTML字符串的任何来源(例如后端服务器)中正确地转义它们
以及源代码,以防您好奇:
String.prototype.decodeForText = function() {
var result = "";
for (var i = 0; i < this.length; ++i) {
if (this.charAt(i) !== "&") {
result += this.charAt(i);
continue;
} else if (this.substring(i, i + 5) === "&") {
result += "&";
i += 4;
continue;
} else if (this.substring(i, i + 4) === "<") {
result += "<";
i += 3;
continue;
} else if (this.substring(i, i + 4) === ">") {
result += ">";
i += 3;
continue;
} else if (this.substring(i, i + 6) === """) {
result += "\"";
i += 5;
continue;
} else if (this.substring(i, i + 6) === "'") {
result += "'";
i += 5;
continue;
} else if (this.substring(i, i + 8) === "=") {
result += "=";
i += 7;
continue;
}
}
return result;
};
String.prototype.decodeForHTML = function() {
return this;
};
String.prototype.decodeForLink = function() {
return encodeURI(this.decodeForText());
}
String.prototype.encodeToHTML = function() {
var result = "";
for (var i = 0; i < this.length; ++i) {
if (this.charAt(i) === "&") {
result += "&";
} else if (this.charAt(i) === "<") {
result += "<";
} else if (this.charAt(i) === ">") {
result += ">";
} else if (this.charAt(i) === "\"") {
result += """;
} else if (this.charAt(i) === "'") {
result += "'";
} else if (this.charAt(i) === "=") {
result += "=";
} else {
result += this.charAt(i);
}
}
return result;
}
}(this));
String.prototype.decodeForText=function(){
var结果=”;
对于(变量i=0;i
好吧,我错过了那部分。然而,在我的特殊情况下,这似乎并不能解决任何问题。只要试着使用文字“帧”,你就会发现无论你做什么,在调用escape时都会把它搞砸(即使将它与decodeForText()结合起来),你完全正确。但不知何故,当您将字符串放入alert()中时,Spotify会弄乱字符串,因此alert(escape('theframes'));仍然失败。然而,这并不意味着我的“问题”已经解决了。谢谢。decodeForText()可能有问题吗?因为,在我的应用程序中,它仍然提供Html编码值?