Execute my Autofill Javascript on Chrome extension click

一世执手 提交于 2021-02-08 09:56:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!