【问题标题】:Spotify Apps Api - encodeURI / escapeSpotify Apps Api - encodeURI / 转义
【发布时间】:2012-03-01 06:31:10
【问题描述】:

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

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

cmets 显示输出。在任何浏览器中按预期在 Spotify 之外执行此代码(用 + 或 %20 替换空格)。

其他人可以确认这是一个错误吗?或者我在这里做错了什么......?是否有地方可以报告 Spotify 应用程序的错误?

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

【问题讨论】:

    标签: javascript api urlencode spotify


    【解决方案1】:

    来自guidelines

    编码字符串

    为确保应用程序不会以可能不安全的方式使用字符串,Spotify API 提供的所有字符串都经过编码,以便意外滥用不会导致注入漏洞。如果应用程序不解码这些字符串,使用下面描述的两种方法,这些字符串将向用户显示为垃圾。唯一的例外是 URI,它从不编码,因此不需要解码。 API 文档为每个方法说明了哪些字符串必须被解码。 JavaScript 字符串中添加了两个方法:decodeForText() 和 decodeForHTML()。如果要以安全的方式使用字符串,例如设置 innerText 或使用 document.createTextNode() 创建文本节点,则应使用 decodeForText()。它将返回一个原始的非转义字符串,因此请确保它永远不会插入到任何将被解释为 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) === "&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));
    

    【讨论】:

    • 好吧,我错过了那部分。但是,在我的特殊情况下,这似乎并不能解决任何问题。只需使用文字“The Frames”尝试一下,您会发现无论您做什么,在调用 escape 时都会弄乱它(即使将它与 decodeForText() 结合使用)
    • 你完全正确。但是不知何故,当您将字符串放入 alert() 时,Spotify 会弄乱字符串,所以 alert( escape( 'The Frames' ) );仍然失败。然而,这确实意味着我的“问题”得到了解决。谢谢。
    • 有没有可能,decodeForText() 有问题?因为,在我的应用程序上,它仍然提供了 Html 编码值?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 2012-01-27
    • 2019-07-18
    相关资源
    最近更新 更多