Automatic log in and log out when browser opens and shuts down
Is there a way to write a js script for automatic log in (when the browser is opened) and automatic log out(when browser is closed) for google chrome extensions using these-
When a new window is opened-
chrome.windows.onCreated.addListener(function(Window window) {...}));
and, when a window is opened-
chrome.windows.onRemoved.addListe开发者_StackOverflow中文版ner(function(integer windowId) {...}));
Source: http://code.google.com/chrome/extensions/windows.html
You are thinking too complicated. In your background page, register event handlers for the load
and unload
events. The former will happen when your extension is initialized and the latter when it is shut down. Normally that's the same as browser start and shutdown (and I guess that you want to treat the case where your extension is uninstalled the same as browser shutdown).
About automatic login, the answer is yes. I have a simple script that logs me into some sites automatically. But I don't use chrome.windows listener for that. I have a script that basically does this -
$("#username").val("username");
$("#password").val("mysupersecretpassword");
$("#login-form").submit();
My manifest.json specifies that this as a content script for the site that I want to run it on.
{ "name" : "Login Script",
... //
"content_scripts": [
{
"matches": [ "http://127.0.0.1/phpmyadmin/" ], // Running it here on my local phpmyadmin
"js": ["js/jquery.min.js", "js/login.js"] //load jquery since I use it here, and the login script
} ],
... // edited for brevity
}
This isn't an ideal solution, in terms of storing passwords within the script in my case. But this is one way of doing the login.
Regarding log-out, you can probably create a logout function in your background page, and call that from the windows.onRemoved event. I'm not sure a script running on the page would be allowed to continue running when it is being closed. Calling the background page, allows the background page code to log you out, while the window itself closes.
HTH
精彩评论