1

I am building my first chrome extension for our website. Not too fancy, i just need to see if user is logged in and if so i would send an ajax request and grab some user data from server and show it in extension popup. At the moment i think, i should check the presence of auth cookie to make sure that user is logged in. I want to, is it a good idea to check the presence of auth cookie for this purpose or should i be taking some other path? This is the code i use to check if user is logged in

isLoggedIn: function () {
                var cookie = document.cookie(this.cookieName);
                if (console)
                    console.log(cookie);
                if (cookie) {
                    return true;
                }
                return false;
            },

I know i can't access cookies right away from background process and i have to pass cookie info from content script to background process but i think it explains the idea. Any suggestions?
Edit: I have read this article to send cookie data from content script to popup.js which is in "default_popup" setting in manifest.json file. but i can't seem to pass cookie data back to popup.html and js files referenced there. How can i make it work? Here is my manifest.json file

{
    "name": "Camba TV - Visual Security Made Simple",
    "version": "1.0",
    "description": "My Cameras on Camba.tv",
    "icons": { 
        "48": "icon48.png",
        "128": "icon128.png" 
    },
    "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html"
    },
    "content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*"],
      "js": ["Cookies.js"]
    }
  ],
  "permissions": [
  "http://localhost:50477/",
    "tabs",
    "http://*/*",
    "https://*/*",
    "cookies"
  ]

}
Muhammad Adeel Zahid
  • 17,474
  • 14
  • 90
  • 155
  • Some issues: The `cookies` permission is needed if you want to use the `chrome.cookies.*` API, which you don't. So, that's obsolete. Possible issue: `http://localhost:50477/` only matches `/`, and not `/index.html`, for example. Is this intended? Now it doesn't matter, since you're running the code in all domains using `http://*/*`. Your current question is missing important details: 1) Can you paste the relevant part of your extension's code? 2) Can you provide a demo page (link?), where the extension can be tested? – Rob W Apr 07 '12 at 10:47
  • thanks Rob W for ur response. My implementation has changed a lot since i posted the question. Now my interest here is that if i can find some tutorial that walks me through creating a useful chrome extension. I am fed up of this `Hello World` walk through. – Muhammad Adeel Zahid Apr 09 '12 at 13:17
  • Coding is easy. The hardest part is the creative part: What useful extension do you need? When you've got an idea, implement it with aid of the [Documentation](http://code.google.com/chrome/extensions/devguide.html) (and Stack overflow (*not* gimmecodeplz, but a specific question)). – Rob W Apr 09 '12 at 13:20
  • By useful extension, i mean an application where most part of commonly used API is used so that i can understand it as i read the code. i don't want anything ready made but that i have found sample apps to be very useful in learning new technology. – Muhammad Adeel Zahid Apr 09 '12 at 13:22
  • and my specific question was how can check in my extension if a user is logged in or not? first i thought i should check existance of auth cookie to check it. Then i preferred to call a method of webservice to check user's stauts ( because most of the times auth cookies are not allowed to be changed by client script). what are the standards here? – Muhammad Adeel Zahid Apr 09 '12 at 13:27
  • The best way to learn is by failing. Think of a useful application concept, and implement it. I've posted [many documented answers](http://stackoverflow.com/search?tab=newest&q=[google-chrome-extension]%20user%3a938089) in the google-chrome-extension tag, which may be interesting. Understanding how to work with Content Scripts, background scripts and injected scripts is essential (see [this answer](http://stackoverflow.com/a/9916089/938089?chrome-extension-auto-run-a-function)). – Rob W Apr 09 '12 at 13:27
  • Reply to the original question: Since you control the website: It may be an option to create a web service which returns whether the current user is logged in or not. That might be more reliable than sniffing cookies. – Rob W Apr 09 '12 at 13:30
  • Thnax robs. webservice method is the route i ended up on. and thanks for other resources as well. – Muhammad Adeel Zahid Apr 10 '12 at 04:28
  • I need the same kind of functionality. I understand you can build a webservice which will tell you the logged-in status of the user. My question is - Which user would you be checking it for? And how would get to know the `user`? What would be the input parameters? – Praful Bagai May 29 '19 at 13:52

0 Answers0