为什么 LinkedIn 身份验证的脚本标签无效 JavaScript?

Why is the script tag for LinkedIn auth not valid JavaScript?

The LinkedIn guide for setting up auth with the Javascript SDK 显示了一个代码示例,但对于作者的实际含义来说非常令人困惑。

<script type="text/javascript" src="//platform.linkedin.com/in.js">
    api_key:   [API_KEY]
    onLoad:    [ONLOAD]
    authorize: [AUTHORIZE]
    lang:      [LANG_LOCALE]
</script>

它不是有效的 JS,没有逗号,也省略了引号。此外,该示例显示了方括号,但必须省略这些方括号,否则将引发错误。一个真实世界的例子会减轻混淆,但是,当然,none 被提供了。

这是一个有效的示例,但使用的是虚构的 API 密钥(API 密钥与客户端 ID 相同——指南不费心解释)

<script type="text/javascript" src="//platform.linkedin.com/in.js">
    api_key: 93h7nnksxj3ccd
    authorize: true
    lang: en_US
</script>

,我不是唯一一个困惑的人,这个人和我有同样的想法,那就是需要引号。

这段代码实际上是如何工作的?对于类型为 text/javascript 的脚本标记,这是否被视为有效语法?

[edit] 回复:可能重复,似乎不是。查看所选答案。 [/编辑]

外部脚本正在直接读取和解析脚本标签的 innerHTMLRoger Hu:

的这篇博客文章对此进行了介绍

文章检查了 LinkedIn 的 JavaScript 并确定了使用的解析器:

Basically the code below appears to extract out the innerHTML and then set the variables r and K to be the key/value pairs. White spaces are removed with the replace() function.

这里是一个简短的描述,然后是更长的摘录:

数据是从 innerHTML:

原始提取的
l = f.innerHTML.replace(A, n)

每行定义一个正则表达式:

g = (/^[\s]*(.*?)[\s]*:[\s]*(.*)[\s]*$/),

每行使用它,将 r 设置为键,将 K 设置为值。

W = s.match(g);
r = W[1].replace(A, n);
K = W[2].replace(A, n)

如果没有匹配,它会提供以下错误:

script tag contents must be key/value pairs separated by a colon.

更大的摘录:

for (U = 0, q = t.length; U < q; U++) {
    var f = t[U];
    if (!m.test(f.src)) {
        continue
    }
    if (b.test(f.src)) {
        c = true
    }
    try {
        l = f.innerHTML.replace(A, n)
    } catch (z) {
        try {
            l = f.text.replace(A, n)
        } catch (y) {}
    }
}
l = l.replace(J, "").replace(A, n).replace(F, n);
ab = C.test(l.replace(j, n));
for (var U = 0, T = l.split(k), q = T.length; U < q; U++) {
    var s = T[U];
    if (!s || s.replace(j, n).length <= 0) {
        continue
    }
    try {
        W = s.match(g);
        r = W[1].replace(A, n);
        K = W[2].replace(A, n)
    } catch (Y) {
        if (!ab) {
            console.warn("script tag contents must be key/value pairs separated by a colon. Source: " + Y)
        }
        continue
    }
    N(r, K)
}

正则表达式集定义在脚本的顶部:

var S = {
        "bootstrapInit": +new Date()
    },
    p = document,
    m = (/^https?:\/\/.*?linkedin.*?\/in\.js.*?$/),
    b = (/async=true/),
    D = (/^https:\/\//),
    J = (/\/\*((?:.|[\s])*?)\*\//m),
    F = (/\r/g),
    j = (/[\s]/g),
    g = (/^[\s]*(.*?)[\s]*:[\s]*(.*)[\s]*$/),
    x = (/_([a-z])/gi),
    A = (/^[\s]+|[\s]+$/g),
    u = (/^[a-z]{2}(_)[A-Z]{2}$/),
    C = (/suppress(Warnings|_warnings):true/gi),
    d = (/^api(Key|_key)$/gi),