当有人安装扩展程序时,如何将用户电子邮件保存为变量?

发布于 2024-12-11 13:20:14 字数 1705 浏览 0 评论 0原文

在我的书签扩展中,我需要将用户的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久而酒知 2024-12-18 13:20:14

您可以在单个扩展程序中同时使用弹出窗口背景页面。我的很多扩展程序都使用...使用后台页面进行通信并保存弹出页面的数据...

您可以提示用户保存他们的电子邮件地址在后台页面中安装,如下所示:

<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 中,直到卸载为止...调用此变量现在可以在您的后台页面弹出窗口< /代码> 页...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文