【发布时间】:2013-12-16 14:17:35
【问题描述】:
我有一个有效的 Javascript,它第一次提示用户输入他的电子邮件和密码;之后,每当用户重新访问该网页时,它都会自动填充用户名和密码。在我的 html 代码中,我只需调用我的 checkCookie() 函数即可在 JSFiddle.net 上执行此操作。
现在我想创建一个 Chrome 扩展程序,单击它会运行自动填充 JS。我有这些代码,但在扩展点击时,我没有得到任何输出。任何人都可以建议我我做错了什么。
// 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();
【问题讨论】:
-
我之前使用过内容脚本,但我在某处读到过,最好使用 background.js 来完成这项任务。期待一些不错的建议。感谢阅读
标签: google-chrome-extension content-script