2013-11-22 43 views
1

您好我需要知道如何從谷歌瀏覽器擴展中跟蹤我的網站的會話。我的網站已被添加爲谷歌瀏覽器的擴展。當擴展圖標被點擊時,它將導航我的網站的主頁。它是一個登錄頁面。所以我需要知道用戶是否已登錄或不。我希望這隻能使用sessions.But,但我不知道如何跟蹤會話變量鉻擴展。請幫助我。如何從Chrome擴展跟蹤瀏覽器的會話

回答

3

一種解決方案是將您的網頁的登錄狀態與您的分機進行通信(詳見here)。


從你的網頁,你必須將消息發送到擴展它通知用戶的登錄狀態。

  • 一旦用戶成功登錄,請確保你讓擴展知道:

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

/* 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>"] 
    } 
} 
相關問題