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();