在Chrome扩展程序中维护会话

时间:2018-02-26 09:58:55

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

我正在开发Chrome扩展程序。我在Chrome扩展程序中进行了基本登录和注册。我面临的困难是每个标签我必须登录并注册。

的manifest.json:

    {
      "manifest_version": 2,
      "name": "Login Signup",
      "description": "User profile extension ",
      "version": "1.0",
      "background": {
        "scripts": ["login_cookies.js"],
        "persistent": false
      },
      "options_page": "login_cookies.html",
      "content_scripts": [ {
        "matches":["<all_urls>"],
        "js":["user_login.js","login_cookies.js","user_registration.js"]
      }
      ],
      "browser_action": {
       "default_icon": "icon.png",
       "default_popup": "popup.html"
      },
      "externally_connectable": {
      "matches": ["http://127.0.0.1:8000/polls/login/"],
      "ids":["gljlejolcfnidahkdnllngpebdgeippm"] 
      },
      "permissions": [
        "identity"
      ]
    }

的login.html:

<!DOCTYPE html>
<html>
  <head>
    <title>User</title>
    <script src="user_login.js"></script>
  </head>
  <body>
  <form id="test2" action="login_cookies.html" method="post">
    <div class="form-group">
      <label for="email">Email</label>
      <input class="form-control" type="text" name="Email" id="email" />
    </div>
    <div class="form-group">
      <label for="password">Password</label>
      <input type="password" id="password" name="userpassword" class="form-control" required="required"/>
    </div>
    <div>
      <button id="loginSubmit" type="submit">OK</button>
    </div>   
    <div>
      <a href="default.asp" target="_blank"><img src="facebook_login.png"  style="width:100px;height:50px;border:0;" alt="HTML tutorial" >
      </a>    
    </div>
    <div>
      <a href="default.asp" target="_blank"><img src="google_login.png" alt="HTML tutorial" >
      </a>    
  </form>  
  </body>
</html>

LoginCookies.html:

<!DOCTYPE html>
<html>
    <head>
        <title>User</title>
        <script src="login_cookies.js"></script>
    </head>
    <body onload="checkCookie()">
        <div>
            <h3>Hello</h3>
            <input type="text" name="text" id='setText'/>
        </div>
    </body>
</html>

LoginCookies.js

document.addEventListener('DOMContentLoaded', checkCookie, false);

function setCookie(cname,cvalue,exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (120*1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
    document.getElementById("setText").innerHTML = cvalue;
}

function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

function checkCookie() {
    var useremail = localStorage.getItem("email ");
    var user=getCookie("username");
    if (user != "") {
        alert("Welcome " + user);
    } else {
       //user = prompt("Please enter your name:",useremail);
       if (useremail != "" && useremail != null) {
           setCookie("username", useremail, 30);
       }
    }
}

每当用户点击扩展程序图标时,我必须在用户登录所有标签的扩展程序后维护LoginCookies.html。建议我如何使用cookie维护html

0 个答案:

没有答案