繁体   English   中英

Spotify Apps Api-encodeURI /转义

[英]Spotify Apps Api - encodeURI / escape

使用JavaScript函数encodeURI / escape和encodeURIComponent时似乎存在一个错误。 例:

escape( 'The Frames' )             // The            0.000000rames
encodeURI( 'The Frames' )          // The            0.000000rames
encodeURIComponent( 'The Frames' ) // The            0.000000rames

注释显示输出。 在任何可以正常工作的浏览器中的Spotify外部执行此代码(用+或%20替换空格)。

其他人可以确认这是一个错误吗? 还是我在这里做错了...? 是否可以报告Spotify应用程序的错误?

编辑:显然上面的示例按预期方式工作。 但是,将它们合并到alert()中将显示混乱的字符串,而实际上是可以的。

根据准则

编码字符串

为确保应用程序不会以可能不安全的方式使用字符串,对Spotify API提供的所有字符串均进行了编码,以便意外滥用不会造成注入漏洞。 如果应用程序未使用以下两种方法解码这些字符串,则这些字符串将作为垃圾显示给用户。 唯一的例外是URI,它从未被编码,因此不需要解码。 API文档说明了每种方法必须解码或不解码的字符串。 JavaScript字符串中已添加了两个方法: decodeForText()和decodeForHTML() 如果打算以安全的方式使用字符串,例如设置innerText或使用document.createTextNode()创建文本节点,则应使用encodeForText()。 它将返回原始的非转义字符串,因此请确保不要将其插入任何将被解释为HTML的上下文中。 如果将字符串放入innerHTML或将要解释为HTML的任何代码段中,则必须使用encodeForHTML()。 它将确保<和>编码为<和>等。例如:

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) === "&amp;") {
            result += "&";
            i += 4;
            continue;
        } else if (this.substring(i, i + 4) === "&lt;") {
            result += "<";
            i += 3;
            continue;
        } else if (this.substring(i, i + 4) === "&gt;") {
            result += ">";
            i += 3;
            continue;
        } else if (this.substring(i, i + 6) === "&quot;") {
            result += "\"";
            i += 5;
            continue;
        } else if (this.substring(i, i + 6) === "&apos;") {
            result += "'";
            i += 5;
            continue;
        } else if (this.substring(i, i + 8) === "&equals;") {
            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 += "&amp;";
        } else if (this.charAt(i) === "<") {
            result += "&lt;";
        } else if (this.charAt(i) === ">") {
            result += "&gt;";
        } else if (this.charAt(i) === "\"") {
            result += "&quot;";
        } else if (this.charAt(i) === "'") {
            result += "&apos;";
        } else if (this.charAt(i) === "=") {
            result += "&equals;";
        } else {
            result += this.charAt(i);
        }
    }
    return result;
}
}(this));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM