This code is a part of a chrome extension.
manifest.json
{
"name": "BrowserExtension",
"version": "0.0.1",
"manifest_version": 2,
"description" : "Description ...",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"background": {
"scripts": ["background.js"],
"persistent": false
},
"permissions": ["*://*.google.com/", "tabs", "activeTab", "*://*.facebook.com/"],
"content_scripts": [{
"matches": ["http://*/*","http://*/", "https://*.facebook.com/","https://*/*"],
"js": ["content.js"]
}]
}
popup.html
<body>
<pre>
<input type="button" value="Get Password" id="link">
</pre>
<script type="text/javascript" src="content.js"></script>
</body>
content.js
{
window.open("https://www.facebook.com");
var myUsername = 'ABCD';
var myPassword = 'password';
var loginField = document.getElementById('email');
var passwordField = document.getElementById('pass');
loginField.value = myUsername;
passwordField.value = myPassword;
}
If I leave this code without encapsulating it in any function, then if I open facebook.com, then I can find the values set into their appropriate fields. But, if I change the Content.js to something like:
var x = document.getElementById("link");
x.addEventListener("click", myFunction);
function myFunction()
{
alert("content.js Started .. ");
window.open("https://www.facebook.com");
var myUsername = 'ABCD';
var myPassword = 'password';
var loginField = document.getElementById('email');
var passwordField = document.getElementById('pass');
loginField.value = myUsername;
passwordField.value = myPassword;
}
then click on the button, facebook is being opened but the fields are not set. I would really want to be able to do that even when the code is in a function block.