变量从Popup.js传递到contentScript.js迷路 - 未定义的值

时间:2016-05-25 22:16:05

标签: javascript jquery html google-chrome google-chrome-extension

我正在尝试创建一个chrome扩展程序,它在popup.html中获取电子邮件和密码。在popup.js中,这些值被提取并发送到contentScript.js,其中这些值用于登录Facebook。 但是,不知何故,这是行不通的。 请帮忙!!

的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>

在Popup.js中,我从popup.html中提取值并将值传递给contentscript.js。 此外,由于只有匹配“匹配”时才会执行内容脚本:[“http:// / ”,“https:// / ”]根据清单,所以我用facebook.com更新了标签,以便contentscript.js从facebook.com html获取emailId和密码字段并更新它。

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

});

0 个答案:

没有答案