【问题标题】:Execute my Autofill Javascript on Chrome extension click在 Chrome 扩展程序上执行我的自动填充 Javascript 点击
【发布时间】: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


【解决方案1】:

首先,您的background.js 中的括号太多。我还建议在 autofill.js 中声明 setCookie()checkCookie() 函数(如果你想在注入的脚本中调用它)。否则,在回调函数中实际声明这些函数之前,在内部执行带有checkCookie() 的 autofill.js 脚本是没有意义的。您也没有在清单文件中指定所需的权限。我稍微改变了你的代码。现在它起作用了。但是,您没有指定 getCookie() 函数,因此您可能希望这样做并使用我留下注释的部分代码。

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

自动填充.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();

【讨论】:

  • 感谢您的回复。我一定会在周末尝试这个,并会让你知道。感谢您的回答
猜你喜欢
  • 1970-01-01
  • 2020-06-25
  • 1970-01-01
  • 2014-04-16
  • 1970-01-01
  • 1970-01-01
  • 2013-11-23
  • 1970-01-01
  • 2014-11-22
相关资源
最近更新 更多