Is there any way to open a tab from a Chrome extension immediately after Chrome gets opened? My school has an access point which you have to sign in to every time you open up your puter to use the internet, and I would like to make an extension that automatically logs me in, but I cannot figure out how to make the script run as soon as I open Chrome.
EDIT: The login page is not the page that is opened when I open the browser: I've got to manually go to a certain site to make the system prompt me to log in, otherwise it will just tell me I'm not connected to the internet.
Is there any way to open a tab from a Chrome extension immediately after Chrome gets opened? My school has an access point which you have to sign in to every time you open up your puter to use the internet, and I would like to make an extension that automatically logs me in, but I cannot figure out how to make the script run as soon as I open Chrome.
EDIT: The login page is not the page that is opened when I open the browser: I've got to manually go to a certain site to make the system prompt me to log in, otherwise it will just tell me I'm not connected to the internet.
Share Improve this question edited Feb 3, 2015 at 17:10 Marco Bonelli 69.8k21 gold badges127 silver badges146 bronze badges asked Feb 2, 2015 at 17:33 ScottiphusScottiphus 431 silver badge6 bronze badges2 Answers
Reset to default 6Preparation
What you need to do here is to:
- Create a basic "empty" extension with the required
manifest.json
file. - Declare the
"background"
field, which will run abackgrond.js
script to open the login page when Chrome is opened, using thechrome.tabs
API. You'll need then to add a permission for that. - Declare the
"content_scripts"
field in your manifest, and set acontnet.js
script to match the login URL that gets opened and allows you to log in. - The script (
content.js
), which will be executed in the created tab, will contain the code to automatically fill the fields with username and password and click the log-in button.
Implementation
Create a basic extension, which will contain three files:
manifest.json
: the extension's manifestbackground.js
: the extension's background scriptcontent.js
: your content script
Write the essential fields in your manifest, like: version, name, background etc. Then add the
"content_scripts"
field to declare your script. Yourmanifest.json
should then look like this:{ "manifest_version": 2, "name": "Some name", "version": "0", "permissions": [ "tabs" ], "background": { "scripts": ["background.js"] }, "content_scripts": { { "matches": ["http://your-login-page-url..."], "js": "/content.js" } } }
In your
background.js
script you'll open a tab right when the extension is started (which is also when Chrome gets opened), like this:chrome.tabs.create({url: "http://your-login-page-url..."}); // this URL ---------------^^ should be the same as the one that matches the content script in the manifest.json
Change the URL with the real one and check that the opened tab is the right one.
Now, in your
content.js
script, which will be loaded into the tab that has just been created, you'll get the right fields and fill them up, then click the login button.For example, if the login page looks something like this:
... <span>User:</span><input id="username" type="text"/> <span>Password:</span><input id="password" type="password"/> <button id="login-btn">Log in</button> ...
Then in your
content.js
script you'll do:document.getElementById('username').value = 'yourusername'; document.getElementById('password').value = 'yourpassword'; document.getElementById('login-btn').click();
Obviously you'll have to replace
yourusername
andyourpassowrd
with the real credentials.
That's all, pretty easy: just a tab creation and a content script injected inside it. This extension will now log-in for you right when you open Chrome!
You can create an extension with a Background Page. The background page would just need to open a new tab at the right url for the login page. Your manifest would specify one script for the background page
"background": {
"scripts": ["background.js"]
}
and that script would just contain this one line of JavaScript:
chrome.tabs.create({ url: 'the login page url' });
This works because background pages are loaded and persisted in memory when the browser starts. Therefore the js file is executed just once when you open the browser. The background page can be reloaded when the extension is updated, but since this will be a private extension you don't have to worry about that and it only makes development easier.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744227047a4564058.html
评论列表(0条)