如何从Chrome扩展中跟踪浏览器的会话 [英] How to track session of a browser from chrome extension

查看:417
本文介绍了如何从Chrome扩展中跟踪浏览器的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我需要知道如何跟踪我的网站从谷歌浏览器扩展会话。我的网站已被添加为谷歌浏览器的扩展。当扩展图标被点击时,它导航到我的网站的主页。它是一个登录页面。所以我需要知道一个用户是否已经登录或不是。我希望这只能使用sessions.But,但我不知道如何跟踪铬扩展的会话变量。请帮助我。

解决方案

一种解决方案是将您的网页的登录状态与您的扩展进行通信(如 此处 )。






在您的网页上,您必须向该分机发送消息,以通知其有关用户的登录状态。




  • 一旦用户成功登录,请确保您知道该扩展程序:



    chrome.runtime.sendMessage(< your_extension_id> ;, {status:logged in});


  • 一旦您检测到用户会话已结束(无论是过期还是手动注销),请确保让扩展程序知道: chrome.runtime.sendMessage(< your_extension_id> ;, {status:logged out});






  • 从您的扩展程序侦听网页中的消息并进行相应更新。



    扩展程序源代码:

    background.js:

      var url =< the_url_of_your_webpage_that_sends_messages>; 
    $ b $ *听取外部消息(来自网页的消息)* /
    chrome.runtime.onMessageExternal.addListener(函数(msg,sender){
    if(sender) url == url){
    / *好的,这个页面可以和我沟通* /
    if(msg.status ===logged in){
    / *酷,用户已登录* /
    alert(Logged in!);
    } else if(msg.status ===logged out){
    / *有多悲伤,用户正在离开* /
    alert(已注销!);
    }
    }
    });

    manifest.json:

      {
    manifest_version:2,
    name:Test Extension,
    version:0.0,

    background:{
    persistent:false,
    scripts:[background.js]
    },

    externally_connectable:{
    matches:[< the_url_of_your_webpage_that_sends_messages>]
    }
    }


    Hi I need to know how to track session of my website from google chrome extension.My website has been added as an extension in google chrome.When the extension icon is clicked it navigates to the home page of my website.It is a login page.So I need to know whether a user has been logged or not.I hope this can be done only using sessions.But I don't know how to track session variable from chrome extension.Please help me.

    解决方案

    One solution is to communicate the login status from your web-page to your extension (as exlained here).


    From your webpage you have to send messages to the extension to inform it about the user's login status.

    • Once the user successfully logs in, make sure you let the extension know:

      chrome.runtime.sendMessage(<your_extension_id>, { status: "logged in" });

    • Once you detect that the users session has ended (either expired or due to manually logging out), make sure you let the extension know:

      chrome.runtime.sendMessage(<your_extension_id>, { status: "logged out" });


    From your extension listen for messages from the webpage and update accordingly.

    Extension source code:

    background.js:

    var url = "<the_url_of_your_webpage_that_sends_messages>";
    
    /* Listen for external messages (messages from web-pages) */
    chrome.runtime.onMessageExternal.addListener(function(msg, sender) {
        if (sender.url == url) {
            /* OK, this page is allowed to communicate with me */
            if (msg.status === "logged in") {
                /* Cool, the user is logged in */
                alert("Logged in !");
            } else if (msg.status === "logged out") {
                /* How sad, the user is leaving */
                alert("Logged out !");
            }
        }
    });
    

    manifest.json:

    {
        "manifest_version": 2,
        "name":    "Test Extension",
        "version": "0.0",
    
        "background": {
            "persistent": false,
            "scripts": ["background.js"]
        },
    
        "externally_connectable": {
            "matches": ["<the_url_of_your_webpage_that_sends_messages>"]
        }
    }
    

    这篇关于如何从Chrome扩展中跟踪浏览器的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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