在我的书签扩展中,我需要将用户的 Gmail 地址发送到 Google 应用引擎,以便以用户“所有者”身份将书签写入数据库。
我的理解是我不能使用弹出窗口,因为我有一个背景页面(我记得读过这个,但我找不到它了)。我也在 Chrome 商店中阅读安装过程。如果有人能将我引导到文档中的正确位置,我将不胜感激。
我复制了下面的 background.html
,其中包含变量 extension_user
。当用户上传扩展程序时,如何从用户那里获取此变量? 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>
最佳答案
您可以在单个扩展程序中同时使用弹出窗口
和背景页面
。我的很多扩展程序都使用...使用后台页面
来通信并保存弹出
页面的数据...
您可以提示用户在安装时将其电子邮件地址保存在后台页面中,如下所示:
<script type="text/javascript">
addEventListener('load', function(){
var MAJOR_VERSION=1.0;
if(!localStorage.updateread||localStorage.updateread!=MAJOR_VERSION)
{
var email=prompt("Enter the Email Address : ")
localStorage["Email"] = Email;
localStorage.updateread=MAJOR_VERSION
}
}, 0);
</script>
此脚本仅在您首次安装扩展时运行。用户的电子邮件地址将保存在扩展 LocalStorage
中,直到卸载为止...调用此变量现在可以在您的后台页面
和弹出窗口
页面...
关于javascript - 当有人安装扩展程序时,如何将用户电子邮件保存为变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7855830/