【问题标题】:How can I keep the value of variable after reload browser in chrome extensions在 chrome 扩展中重新加载浏览器后如何保持变量的值
【发布时间】:2014-12-19 21:17:03
【问题描述】:

我正在编写我的第一个 chrome 扩展程序,我是这个领域的新手。

我的目标是将 url 和电子邮件发送到 PHP 服务器,但现在我将它们发送到控制台。

我的问题是将用户输入的最后一封电子邮件保存到我的扩展程序中。 现在,当我关闭浏览器时,电子邮件的值将变为 undefined。

我把我的扩展的整个代码,但我认为问题一定出在getemail函数中。

background.js

//variables defenitions:
var email;
var urll;
//functions:

function getemail(){
     email = window.prompt("Pleas  enter your Email address:");
     chrome.storage.sync.set({'email':email}, function() {
      alert('You entered \n[' + email + "]\n as Email address.\nThank you for registration." );
      });
        var id = chrome.contextMenus.create({"title": "Submit your url","onclick":consollog });
        var id = chrome.contextMenus.create({"title": email+"/Change" ,"onclick":getemail });
        chrome.contextMenus.remove("1", null);
        chrome.contextMenus.remove("2", null);
        var id = chrome.contextMenus.create({"id":"2","title": "SUPPORT","onclick":support});
     }


function create(){
        var id = chrome.contextMenus.create({"title": "Submit your url","onclick":consollog });
        var id = chrome.contextMenus.create({"title": email+"/Change" ,"onclick":getemail });
        chrome.contextMenus.remove("1", null);
        chrome.contextMenus.remove("2", null);
        var id = chrome.contextMenus.create({"id":"2","title": "SUPPORT","onclick":support});
     }
function support(){
    alert("Contact ways:\nPhone number:+989378114692\nEmail address:amukiarash@gmail.com");
  }

function consollog(info, tab) {
  chrome.tabs.query({
  'active':true,"currentWindow": true,
}, function(lTabs) {
    urll=lTabs[0].url;
  console.log(urll)
  console.log(email)
  alert('You sent "' + urll + '" to our server');
});
}
//menu items:
if(email==undefined){
   var id = chrome.contextMenus.create({"id":"1","title": "Register Email","onclick":getemail });
    } 
   else {create();}


var id = chrome.contextMenus.create({"id":"2","title": "SUPPORT","onclick":support});

manifest.json:

{
   "name": "Jenco",
   "description": "This extension helps you to send url",
   "version": "0.9",
   "browser_action":{
      "default_icon":"32.png",
      "default_popup":"mypopup.html"},
   "permissions": ["contextMenus","tabs","activeTab", "http://*/*", "https://*/*","storage"],
   "background": {
      "scripts": ["background.js"]
      },
   "manifest_version": 2,
   "icons": { "16": "16.png",
              "32": "32.png",
              "48": "48.png" },
   "commands": {
      "send-url": {
         "suggested_key": { "default": "Ctrl+Shift+Z" },
         "description": "Send an url"
                    }
                }
}

【问题讨论】:

  • window.prompt 和 window.alert 不能在 chrome 环境中工作,因为它是同步的。你不应该使用这个函数。
  • 我可以用什么代替?

标签: javascript google-chrome google-chrome-extension


【解决方案1】:

我保存变量和加载变量的代码:

// Saving
chrome.storage.sync.set({"variableName": value});
// Loading 1 thing
chrome.storage.sync.get("variableName", function(result){
    // Showing the requested variable value
    alert(result.variableName);
});
// Loading more things
chrome.storage.sync.get(["variableName", "variableNameTheSecond"], function(result){
    // Showing first the first one and then the second one
    alert(result.variablename);
    alert(result.variableNameTheSecond);
});

希望对你有帮助,问候

埃贝

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 2013-08-26
    相关资源
    最近更新 更多