gpt4 book ai didi

javascript - 如何向 Chrome 扩展程序发送数据?

转载 作者:太空宇宙 更新时间:2023-11-03 15:25:22 26 4
gpt4 key购买 nike

我正在为我的书签扩展开发用户身份验证。该扩展适用于 http://ting-1.appspot.com/ 的 Google App Engine (Python) 后端.

第一次运行扩展时,background.html 在新选项卡中打开 options.html,用户输入他的电子邮件地址。我像这样将电子邮件地址发送到应用程序(尚未尝试):

in options.html
---------------

var formData = new FormData();

formData.append("extension_user", userEmail);

var xhr = new XMLHttpRequest();
xhr.open("POST", "http://ting-1.appspot.com/authsender", true);
xhr.send(formData);

在应用程序中,处理程序 AuthSender 向用户发送确认电子邮件:

class AuthSender(webapp.RequestHandler):
def post(self):
new_user = self.request.get("extension_user")
mail.send_mail(
sender="Ting <ting@gmail.com>",
to=new_user,
subject="Ting Bookmarking: confirm your email",
body="""click on the link below to confirm your email:
<a href="http://ting-1.appspot.com/authHandler"></a>

Happy Bookmarking!""")

如果用户单击电子邮件中的链接,应用程序中的 AuthReceive 处理程序将处理请求并向扩展程序发送确认。但我不知道如何将确认发送给分机。

当用户点击链接时,如何将确认发送回扩展程序?我要再次使用 XMLHttpRequest 吗?如何?我需要在扩展中放置什么样的监听器?

下面是应用程序中 AuthReceive 处理程序的草图:

class AuthReceive(InboundMailHandler):
def receive(self, message):

#---------------------------------------
#receive email // I know this part
#send confirmation to extension // I need help with this
#--------------------------------------
...

在 background.html 中

//...
// add listener and save the variable to localStorage
//...

感谢您的帮助。

更新

这是符合 Moishe's answer 的代码.除了最后一步,一切正常:handler.onmessage 未被触发。

options.html

<html>
<head><title>Extension Options</title></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 type="text/javascript">

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

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

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

var xhr = new XMLHttpRequest();
xhr.onReadyStateChange = function()
{
//error handling etc not included
if (xhr.readyState == 4 && xhr.status == 200)
{
token = xhr.responseText;
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>

AuthSender 和 AuthHandler

class AuthSender(webapp.RequestHandler):
def post(self):
new_user = self.request.get("extension_user")
link = "http://ting-1.appspot.com/authhandler?new_user=%s" % new_user

message = mail.EmailMessage()
message.sender="Ting <ting@gmail.com>"
message.to=new_user
message.subject="Ting Bookmarking - Confirm your email"
message.body="""Click on the link to confirm your email: %s """ % link
message.send()

logging.info("message sent to: %s " % message.to)

token = channel.create_channel(new_user)
self.response.out.write(token)

class AuthHandler(webapp.RequestHandler):
def get(self):
new_user = self.request.get("new_user")

channel.send_message(new_user, new_user)

logging.info("new_user sent to client: %s" % new_user)


工作版本

( Related question )

options.html

<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>

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

document.getElementById("save").addEventListener
(
"click",
function ()
{
var userEmail = document.getElementById("getEmail").value;
var formData = new FormData();
formData.append("extension_user", userEmail);

var channel;
var socket;
var handler =
{
onopen: function () { alert("onopen") },
onerror: function () { alert("onerror") },
onclose: function () { alert("onclose") },
onmessage:
function (evt)
{
alert("evt.data is: " + evt.data)
}
};

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4 && xhr.status == 200)
{
token = xhr.responseText;
channel = new goog.appengine.Channel(token);
socket = channel.open(handler);
}
};
xhr.open("POST", "http://ting-1.appspot.com/authsender", true);
xhr.send(formData);
}, false
)
</script>
</body>
</html>

AuthSender 和 AuthHandler

class AuthSender(webapp.RequestHandler):
def post(self):
new_user = self.request.get("extension_user")
link = "http://ting-1.appspot.com/authhandler?new_user=%s" % new_user

message = mail.EmailMessage()
message.sender="Ting <ting@gmail.com>"
message.to=new_user
message.subject="Ting Bookmarking - Confirm your email"
message.body="""Click on the link to confirm your email: %s """ % link
message.send()

token = channel.create_channel(new_user)

self.response.out.write(token)

class AuthHandler(webapp.RequestHandler):
def get(self):
new_user = self.request.get("new_user")

self.response.out.write(new_user)

channel.send_message(new_user, new_user)

最佳答案

您可以使用 Channel API来完成这个。在发出 XMLHttpRequest 的同时请求一个 token ,并打开 channel 来监听消息。当您的应用程序处理与用户点击链接对应的 HTTP 请求时,向为该用户的扩展程序创建的 channel 发送一条消息。

更多细节:

基本上,当您制作 XHR 时,在 options.html 中,执行如下操作:

var channel;
var socket;
var handler = {
onmessage: function(evt) {
// evt.data will be what your server sends in channel.send_message
}
};
var xhr = new XMLHttpRequest();
xhr.onReadyStateChange = function() {
// error handling and whatnot elided
if (xhr.readyState == 4 and xhr.status == 200) {
// We got a response from the server. The responseText is
// a channel token so we can listen for a "verified" message.
token = xhr.responseText;
channel = new goog.appengine.Channel(token);
socket = channel.open(handler);
}
};
xhr.open("POST", "http://ting-1.appspot.com/authsender", true);
xhr.send(formData);

然后在您的服务器上,“authsender”页面的处理程序将执行如下操作:

class AuthSenderHandler(webapp.RequestHandler):
def post(self):
# get whatever data is in the form to send an email.
# let's say that user_id is a field we extracted either from a cookie or from
# the POST parameters themselves.
link = "http://your.server.com/authHandler?user_id=%s" % user_id
message = mail.EmailMessage()
message.body = """some stuff %s""" % link
# fill in other message fields, then send it.

# now we'll create a channel token using the user_id and return
# it to the client.
token = channel.create_channel(user_id)
self.response.out.write(token)

以上两个函数将使您的客户收听 channel 。下一步是:当用户点击链接时会发生什么?

之前,我们在电子邮件中发送了包含 user_id 参数的链接(出于说明目的;您可能想使用其他参数)。现在,当用户单击该链接时,它将向 authHandler 路径发出 HTTP 请求,并将 user_id 作为参数。所以我们可以使用 user_id 来标识发送消息的 channel ,如下所示:

class AuthHandler(webapp.RequestHandler):
def get(self):
user_id = self.request.get("user_id")
# send a message indicating the user received the email and
# verified to our client page. You may want to send other data.
channel.send_message(user_id, "authorized")
# probably want to show a nice "you've been authorized" page or something

然后 handler.onmessage 回调将被调用,现在您可以做任何您需要做的事情,因为用户已经验证了他们的电子邮件地址。

希望对您有所帮助!

关于javascript - 如何向 Chrome 扩展程序发送数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7939633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com