当有人安装扩展程序时,如何将用户电子邮件保存为变量?
在我的书签扩展中,我需要将用户的 Gmail 地址发送到 Google 应用程序引擎,以便以用户“所有者”身份将书签写入数据库。
我的理解是,我无法使用弹出窗口,因为我有一个背景页面(我记得读过有关此内容的内容,但我无法再次找到它)。我也在 Chrome 商店中阅读安装过程。如果有人能引导我到文档中的正确位置,我将不胜感激。
我复制了下面的 background.html
,其中包含变量 extension_user
。当用户上传扩展程序时,如何从用户那里获取此变量? 这是我之前的问题。
<html>
<script>
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.getSelected(null, function(tab) {
// Send a request to the content script.
chrome.tabs.sendRequest(tab.id, {action: "getDOM"}, function(response) {
var firstParagraph = response.dom;
var formData = new FormData();
formData.append("url", tab.url);
formData.append("title", tab.title);
formData.append("pitch", firstParagraph);
//***the variable with user email to send to backend:***//
//formData.append("extension_user", extension_user)
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://ting-1.appspot.com/submithandlertest", true);
xhr.onreadystatechange = function (aEvt) {
if (xhr.readyState == 4) {
if (xhr.status == 200){
console.log("request 200-OK");
chrome.browserAction.setBadgeText ( { text: "done" } );
setTimeout(function () {
chrome.browserAction.setBadgeText( { text: "" } );
}, 2000);
}else{
console.log("connection error");
chrome.browserAction.setBadgeText ( { text: "ERR" } );
}
}
};
xhr.send(formData);
}); //chrome.tabs.sendRequest
});
});
</script>
</html>
In my bookmarking extension, I need to send the gmail address of the user to google app engine in order to write the bookmark to the database as the user as "owner".
My understanding is that I cannot use a popup because I have a background page (I remember reading about this but I could not find it again). I am also reading the installation process in Chrome store. I would appreciate if anyone can direct me to the right place in the documentation.
I copy my background.html
below with the variable extension_user
included. How do I get this variable from the user when they upload the extension? This is my previous question.
<html>
<script>
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.getSelected(null, function(tab) {
// Send a request to the content script.
chrome.tabs.sendRequest(tab.id, {action: "getDOM"}, function(response) {
var firstParagraph = response.dom;
var formData = new FormData();
formData.append("url", tab.url);
formData.append("title", tab.title);
formData.append("pitch", firstParagraph);
//***the variable with user email to send to backend:***//
//formData.append("extension_user", extension_user)
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://ting-1.appspot.com/submithandlertest", true);
xhr.onreadystatechange = function (aEvt) {
if (xhr.readyState == 4) {
if (xhr.status == 200){
console.log("request 200-OK");
chrome.browserAction.setBadgeText ( { text: "done" } );
setTimeout(function () {
chrome.browserAction.setBadgeText( { text: "" } );
}, 2000);
}else{
console.log("connection error");
chrome.browserAction.setBadgeText ( { text: "ERR" } );
}
}
};
xhr.send(formData);
}); //chrome.tabs.sendRequest
});
});
</script>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在单个扩展程序中同时使用
弹出窗口
和背景页面
。我的很多扩展程序都使用...使用后台页面
进行通信并保存弹出
页面的数据...您可以提示用户保存他们的电子邮件地址在后台页面中安装,如下所示:
此脚本仅在您首次安装扩展时运行。用户的电子邮件地址将保存在扩展程序
LocalStorage
中,直到卸载为止...调用此变量现在可以在您的后台页面
和弹出窗口< /代码> 页...
You can use both a
popup
andbackground page
in a single extension. A lot of my extensions use both... Use thebackground page
to communicate and save data for yourpopup
page...You can prompt your user to save their email address on install in your background page as follows:
This script will only run when you first install the extension. And the users email address will be saved in the extensions
LocalStorage
until they uninstall... Calling this variable will now work in both yourbackground page
and yourpopup
page...