我在 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/