0

I am trying to create a chrome extension which takes the email and password in popup.html. In popup.js, these values are extracted and sent to the contentScript.js where these values are used to login into Facebook. But, somehow this is not working. Please help!!

manifest.json

    {
    "manifest_version": 2,

    "name": "FbLogin",
    "description": "Login into FB account with just one touch",
    "version": "1",

    "browser_action": {
    "default_icon": "icon.png",
    "default_title": "fbQuickLogin",
    "default_popup": "popup.html"
    },
    "icons": {
        "64": "icon.png",
        "32": "icon.png",
        "16": "icon.png"
    },
    "background": {
        "scripts": ["jquery.js","background.js"]
    },
    "content_scripts": [
        {
            "matches": ["http://*/*","https://*/*"],
            "js": ["jquery.js","myScript.js"]
        }
    ],
    "permissions": [
        "notifications",
        "https://*.facebook.com/*","http://*.facebook.com/*",
        "tabs",
        "cookies",
        "activeTab"
    ]
    }

popup.html

<html>
    <head>
        <script src="jquery.js"></script>
        <script src="popup.js"></script>
    </head>

    <body>
        <lable for="email">Email:</lable><input type="text" name="email" id="email"/>
        <br/><br/>
        <lable for="password">Pass:</lable><input type="password" name="pass" id="pass"/>
        <br/><br/>
        <button id='click-me'>Click Me!</button>
    </body>
</html>

In Popup.js, i am extracting the values from popup.html and passing the values to contentscript.js. Also, since the contentscript will execute only if it matches "matches": ["http:///","https:///"] as per manifest, so i updated the tab with facebook.com, so that the contentscript.js will fetch the emailId and password field from facebook.com html and update it.

popup.js

function sendClicks() {
    var email = $("#email").val();
    var pass = $("#pass").val();

    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        chrome.tabs.sendMessage(tabs[0].id, {email: email, pass: pass}, function(response) {
            alert("content script Response received: " + response.farewell);
        });
    });

    //from popup to contentScript
    chrome.tabs.update({url: "http://facebook.com/"});
    window.close(); // Note: window.close(), not this.close()

}

$(document).ready(function(){
    console.log("popup.js > extension ready");
    $("#click-me").click(function() {
        sendClicks();
    });
});

myScript.js

$(document).ready(function(){
    var email;
    var pass;
    chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
        email = request.email;
        pass = request.pass;

        alert("Popup message received: "+ email + " " + pass);
        sendResponse({farewell: "goodbye"});
        return true;
    });
    document.getElementById('email').value = email;
    document.getElementById('pass').value = pass;

    var submitBtn = document.getElementById('loginbutton');
    if (submitBtn.type == "submit" || submitBtn.type == "button") {
        submitBtn.click();
    }
    else {
        document.forms[0].submit();
   }

});
abhi
  • 1
  • 2

0 Answers0