问题
I have a working Javascript which prompts the user for his email and password for first time; and afterwards, it autofills the username and password whenever the user revisits that webpage. In my html code, I just have to call my checkCookie() function to perform this on JSFiddle.net.
Now I want to create a Chrome extension which on click runs the autofill JS. I have these codes but on extension click, I get no output. Can anybody suggest me what am I doing wrong.
// manifest.json
{
"manifest_version": 2,
"name": "Auto-fill Passwords",
"description": "This extension autofill password on click.",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png"
},
"background": {
"scripts": ["background.js"]
}
}
// background.js
chrome.browserAction.onClicked.addListener(function (tab)
{ //Fired when User Clicks ICON
chrome.tabs.executeScript(tab.id, {
"file": "autofill.js"
}, function () {
function setCookie(c_name, value, exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
}
function checkCookie() {
var username = prompt("Please enter your name:", "");
var n= getCookie("username",username);
if(n)
{
alert("UserID Match! The user's password is " +n) ;
}
else
{
//alert("UserID password doesnot exist ");
var password = prompt("Please enter your password:", "");
username += "!";
username += password;
alert("Username = "+ username);
setCookie("username", username, 365);
}
}
console.log("Script Executed .. "); // Notification on Completion
});
}
});
//autofill.js
alert("Check cookie function is called");
checkCookie();
回答1:
First of all, you have too much brackets in your background.js
. And I would also recommend to declare setCookie()
and checkCookie()
functions inside autofill.js (if you want to call it inside the injected script). Otherwise, it doesn't make sense to execute autofill.js script with checkCookie()
inside before you actually declare those functions in callback function. You also didn't specify required permissions in the manifest file. I changed your code a little bit. Now it works. However, you didn't specify getCookie()
function, so you may wish to do it and use the part of code I left commented.
manifest.json:
{
"manifest_version": 2,
"name": "Auto-fill Passwords",
"description": "This extension autofill password on click.",
"version": "1.0",
"permissions": [
"activeTab",
"tabs"
],
"browser_action": {
"default_icon": "icon.png"
},
"background": {
"scripts": ["background.js"]
}
}
background.js:
chrome.browserAction.onClicked.addListener(function (tab) { //Fired when User Clicks ICON
chrome.tabs.executeScript(tab.id, {
"file": "autofill.js"
}, function() {
alert();
});
console.log("Script Executed .. "); // Notification on Completion
});
autofill.js:
alert("Check cookie function is called");
function setCookie(c_name, value, exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
}
function checkCookie() {
var username = prompt("Please enter your name:", "");
/*
var n= getCookie("username",username);
if(n) {
alert("UserID Match! The user's password is " +n) ;
} else {
//alert("UserID password doesnot exist ");
var password = prompt("Please enter your password:", "");
username += "!";
username += password;
alert("Username = "+ username);
setCookie("username", username, 365);
*/
}
checkCookie();
来源:https://stackoverflow.com/questions/20613061/execute-my-autofill-javascript-on-chrome-extension-click