在Chrome扩展程序上执行我的自动填充Javascript,然后点击 [英] Execute my Autofill Javascript on Chrome extension click

查看:75
本文介绍了在Chrome扩展程序上执行我的自动填充Javascript,然后点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行中的Javascript,可首次提示用户输入电子邮件地址和密码;之后,只要用户再次访问该网页,它就会自动填充用户名和密码.在我的html代码中,我只需要调用我的checkCookie()函数即可在JSFiddle.net上执行此操作.

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.

现在,我想创建一个Chrome扩展程序,该扩展程序在单击时将运行自动填充JS.我有这些代码,但是在扩展名上单击时,没有任何输出.有人可以告诉我我在做什么错.

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

推荐答案

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

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.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:

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:

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

这篇关于在Chrome扩展程序上执行我的自动填充Javascript,然后点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆