google-app-engine - 为什么 Google App Engine Channel API (jsapi) 无法在 Chrome 扩展程序中加载?

标签 google-app-engine google-chrome google-chrome-extension

我在 Chrome 扩展程序中使用 Channel API。

Google App Engine Channel API Javascript Reference (Python)页面上写着

Include the following in your html page before any JavaScript code that refers to it:

<script type="text/javascript" src="/_ah/channel/jsapi"></script>

所以,我把它放在我的 options.html 文件的标题中:

<html>
<head>
    <title>Extension Options</title>
    <script type="text/javascript" src="/_ah/channel/jsapi"></script>
</head>

但是 Chrome 抛出 jsapiFailed to load resource 错误。我做错了什么?

更新

根据 Moishe 的回答,我更新了对 jsapi 的调用,如下所示:

<head>
    <title>Extension Options</title>
    <!-- this does not work because it is local
    <script type="text/javascript" src="/_ah/channel/jsapi"></script>
    -->
    <script type="text/javascript" src="https://talkgadget.google.com/talkgadget/channel.js"></script>
</head>

更新

我添加了 onopen 和其他属性。现在我收到 onopen 警报,但我没有收到 evt.data 警报。我做错了什么?

<html>
<head>
    <title>Extension Options</title>
    <!-- this does not work because it is local url
    <script type="text/javascript" src="/_ah/channel/jsapi"></script>
    -->
    <script type="text/javascript" src="https://talkgadget.google.com/talkgadget/channel.js"></script>
</head>

<body>
<p>Enter your gmail address:</p>

<textarea id="getEmail" style="margin-bottom: 4px; width: 250px; height: 20px">
</textarea><br />

<button id="save">Save</button>
<!--<button id="save">Clear</button>-->

<script>
document.getElementById("getEmail").placeholder = "your gmail address" ;

//save entered gmail address
document.getElementById("save").addEventListener
(
    "click", 
    function ()
    {
        var userEmail = document.getElementById("getEmail").value;
        var formData = new FormData();
        formData.append("extension_user", userEmail);
        alert("after formData.append")

        var channel;
        var socket;
        var handler = 
        {
            onopen: function () { alert("onopen") },
            onerror: function () { alert("onerror") },
            onclose: function () { alert("onclose") },
            onmessage: 
            function (evt)
            {
                //evt.data will be what the server sends in channel.send_message
                console.log("evt.data received from authhandler: " + evt.data);
                alert("evt.data is: " + evt.data)
            }
        };    

        var xhr = new XMLHttpRequest();
        //changed to lowercase
        xhr.onreadystatechange = function()
        {
            //alert("xhr.onReadyStateChange")
            //error handling etc not included
            if (xhr.readyState == 4 && xhr.status == 200)
            {
                token = xhr.responseText;
                alert("token: " + token)
                channel = new goog.appengine.Channel(token);
                socket = channel.open(handler);
            }
        };
        xhr.open("POST", "http://ting-1.appspot.com/authsender", true);
        xhr.send(formData);
        console.log("formData sent to authsender: " + formData);
    }, false
)



</script>
</body>
</html>

最佳答案

在 chrome 扩展程序中,您需要直接指定 channel javascript 的路径 (https://talkgadget.google.com/talkgadget/channel.js)。/_ah/channel/jsapi 的请求不能被任何东西重定向,因为试图加载它的文件是本地的。

关于google-app-engine - 为什么 Google App Engine Channel API (jsapi) 无法在 Chrome 扩展程序中加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7961638/

相关文章:

google-chrome - 浏览器是否支持证书透明度?

javascript - 防止其他脚本设置 window.title?

angular - 如何在 Google App Engine 上部署 Angular 多语言应用程序

google-app-engine - 如何在谷歌应用引擎日志查看器中正确过滤日志消息?

java - 谷歌镜像API

php - Jquery 切换行为不一致

python - 应用引擎 SDK : How do I view keys in a specific namespace using the Memcache Viewer?

python - chromedriver 在 html 中选择带有 h1 和 p 部分的元素

javascript - 间歇性 "Not a Product"Javascript 错误

javascript - Chrome 扩展程序 - 页面更新两次然后在 YouTube 上删除