当有人安装扩展程序时,如何将用户电子邮件作为变量保存? [英] How do I save user email as a variable when someone installs the extension?

查看:95
本文介绍了当有人安装扩展程序时,如何将用户电子邮件作为变量保存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的书签扩展中,我需要将用户的Gmail地址发送到谷歌应用程序引擎,以便将书签作为用户以所有者身份写入数据库。



我的理解是,我不能使用弹出窗口,因为我有一个背景页面(我记得阅读过这个内容,但是我找不到它)。我也正在阅读Chrome商店中的安装过程。



我复制了我的 background.html 我非常感谢任何人都可以将我引导至文档的正确位置。下面包含变量 extension_user 。如何在用户上传扩展时从用户获取此变量? 这是我以前的问题

 < html> 
< script>
chrome.browserAction.onClicked.addListener(function(tab){
chrome.tabs.getSelected(null,function(tab){

//发送请求到内容脚本。
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);
// ***将用户电子邮件发送给后端的变量:*** //
//formData.append(expand_user,extension_user)

var xhr = new XMLHttpRequest();
xhr.open(POST,http://ting-1.appspot.com/submithandlertest,true);
xhr.onreadystatechange =函数(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)
{$ b $ (输入电子邮件地址:)
localStorage [电子邮件] =电子邮件;
localStorage.updateread = MAJOR_VERSION
}
},0);
< / script>

这个脚本只会在您第一次安装扩展时运行。用户的电子邮件地址将保存在扩展名 LocalStorage 中,直到它们被卸载为止。调用此变量现在可以在后台页面中使用 code>和你的弹出页面...


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>

解决方案

You can use both a popup and background page in a single extension. A lot of my extensions use both... Use the background page to communicate and save data for your popup page...

You can prompt your user to save their email address on install in your background page as follows:

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

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 your background page and your popup page...

这篇关于当有人安装扩展程序时,如何将用户电子邮件作为变量保存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆