1

My extension should automatically fill my gmail user name and password, but it's not working.

manifest.json

{
 "manifest_version": 2,
 "name": "Click to execute",
 "description": "Akshaya app",
 "version": "1.0",
 "icons": {
   "48": "icon.png"
 },
 "permissions": [
   "tabs", "<all_urls>"
 ],
 "browser_action": {
   "default_icon": "icon.png",
   "default_popup": "popup.html"
 },
"content_scripts": [
       {
           "matches": ["https://www.facebook.com/*"],
           "js": ["popup.js"]
       }
     ]
}

popup. html file

<button id="buttonSet">Set Value</button> 
   <script type="text/javascript" src="popup.js"></script>

popup.js

function setValue() {

var name ="username";
var pass = "password";
document.getElementById('username').value =name;
document.getElementById('pass').value =pass;
}
document.getElementById('buttonSet').addEventListener('click', setValue);

I got all code from Internet and i haven't any previous experience in working with google extension and i googled many times for solving the issue i couldn't find any solution for this issue

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
Dfx
  • 65
  • 1
  • 8
  • To be sure you say "fill my gmail user name and password" . But where is this fields ? you mean to fill the Facebook user&password fields or you have fields in your popup.html ? – OriEng Mar 21 '17 at 15:56
  • check the js document.getElementById('username').value = name ; – Dfx Mar 21 '17 at 16:08
  • Sorry but I couldn't understand . the 'username' and 'pass' is inside your popup.html ?You just want to put there values? or where? It's real not clear . – OriEng Mar 21 '17 at 16:19
  • sir, i want on clicking the button the email and pass should automatically appear in the text fields – Dfx Mar 21 '17 at 17:25
  • "it's not working" is not a sufficient description of a problem. You need to tell us what it is/is not doing and what you expect it to do. – Makyen Mar 21 '17 at 18:22
  • 1
    I suggest you read the [Chrome extension overview](https://developer.chrome.com/extensions/overview) (perhaps along with the pages linked from the overview). The [architecture section](https://developer.chrome.com/extensions/overview#arch) has overall architecture information which should help your understanding of how things are generally organized/done. You will probably also want to read [Content Scripts](https://developer.chrome.com/extensions/content_scripts), and [Message Passing](https://developer.chrome.com/extensions/messaging). – Makyen Mar 21 '17 at 18:24
  • If your user interaction *begins* with the user clicking a `browserAction` button, thus the content script should be injected with [`chrome.tabs.executeScript()`](https://developer.chrome.com/extensions/tabs#method-executeScript) instead of a *manifest.json* `content_script` entry. That way your content script does not burden the browser by being injected into every page just to wait to be used. Using `chrome.tabs.executeScript()`, the script can begin functioning when it is injected with [the data, if any is needed, that has been passed to it](http://stackoverflow.com/a/40815514/3773011). – Makyen Mar 21 '17 at 18:26
  • 1
    You should almost never use the same script, other than libraries, as more than one type of script in your *manifest.json* (e.g. `content_scripts`, background scripts, included in your popup, etc.). You should nearly always use a separate script file for each of these. – Makyen Mar 21 '17 at 18:30

1 Answers1

2

I hope I understand you right . You could use message passing between your content script to your script that run in popup.html(myscript.js):

manifest.json

{
 "manifest_version": 2,
 "name": "Click to execute",
 "description": "Akshaya app",
 "version": "1.0",
 "icons": {
           "48": "icon.png"
           },
 "permissions": [
                 "tabs", "<all_urls>"
                ],
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
                  },
"content_scripts": [
                      {
                       "matches": ["https://www.facebook.com/*"],
                       "js": ["popup.js"]
                      }
                    ]
 }

popup.html.

<script type="text/javascript" src="myscript.js"></script>
<button id="buttonSet">Set Value</button> 
<input type="text" id="username">
<input type="text" id="pass">

popup.js

chrome.runtime.onMessage.addListener(function(request, sender,   sendResponse) { 
     //Take the fields of user and password from the DOM of facebook log-in page 
     document.getElementById('email').value=request.user;
     document.getElementById('pass').value=request.pass;

});

myscript.js

window.onload=function(){

 document.getElementById('buttonSet').addEventListener('click',function(){

 chrome.tabs.query({}, function(tabs) {
 for(var i = 0; i < tabs.length; i++) {
  chrome.tabs.sendMessage(tabs[i].id, {user :document.getElementById('username').value,
                                     pass :document.getElementById('pass').value}, function(response) {
        });
 }
 }); 

 });

 }

In myscript.js you will need to send a message to a content script tab you want by their ID . Here it's example to that I send to all the tabs message until I find the one that wait for message. At the end of the result in the first input will show 'username' and you could do this also for the second parameter.

Good luck .

OriEng
  • 1,424
  • 18
  • 34
  • Error in event handler for (unknown): TypeError: Cannot read property 'response' of undefined at **if(response.response!="undefined")** – Dfx Mar 23 '17 at 06:35
  • sir here **typeof response** is undefined. so there is no value in responce.response – Dfx Mar 23 '17 at 08:14
  • I test it - your facebook.com page need to be open and then click on the 'set value' button, then the first textfield will fill in text 'username' . This check of 'undefined' done because we send it to all open tabs and just the facebook return a value ( because 'facebook.com' it's under 'matches' in your manifest and just there this script is inject ) – OriEng Mar 23 '17 at 08:30
  • I working with your manifest: { "manifest_version": 2, "name": "Click to execute", "description": "Akshaya app", "version": "1.0", "icons": { "48": "icon.png" }, "permissions": [ "tabs", "" ], "browser_action": { "default_icon": "icon.png", "default_popup": "popup.html" }, "content_scripts": [ { "matches": ["https://www.facebook.com/*"], "js": ["popup.js"] } ] } – OriEng Mar 23 '17 at 08:47
  • i didnt edited anything sir just copied and pasted but now there is a problem with the manifest.json file **Invalid value for 'content_scripts[0].matches[0]': Missing scheme separator** – Dfx Mar 23 '17 at 08:57
  • I add the manifest to answer . I load it again and test it now and when you are in https://www.facebook.com/ and you open the pop-up window and click on 'set value' the first field is fill with 'username' – OriEng Mar 23 '17 at 09:04
  • sir now the "username " is setting in popup's textfield which is not setting to facebook's username field .. i need to set the value to the facebook – Dfx Mar 23 '17 at 10:37
  • OK , that what I ask you in the start and I don't understand that this is what you looking for . I edit my answer again . Now when you are in Facebook log-in page and insert the values they show up in Facebook user name and password fields . ( I take this Facebook fields by the DOM ) – OriEng Mar 23 '17 at 11:21
  • sir just copied and pasted your code but popup.js is not working – Dfx Mar 23 '17 at 12:01
  • You set values in the text fields that in the popup.html ? because it's take the values and insert them to facebook fields . If you want default values and not input just set : document.getElementById('username').value ="yourname"; document.getElementById('pass').value ="yourpass"; I check it again with the code and it's work for me ( put attention that you in the log-in page of facebook that contain the fields of user & password ) – OriEng Mar 23 '17 at 12:07
  • i have added console.log inside the popup.js and myscript.js myscript.js is working but popup.js is not working. there is no connection between myscript and popup – Dfx Mar 24 '17 at 04:40
  • sir, if i add ** document.getElementById('email').value=request.email;** which is not working on but ***document.getElementById('email').value="something" ** working properly. the value "something" showing inside facebook's email field – Dfx Mar 24 '17 at 04:58
  • To be sure I check it in 2 different computers and it's work: http://i.imgur.com/6HVykUS.png . Can you set console.log(request) in myscript.js and to check what the value you get. I get the the right object there: Object {user: "test", pass: "test1"} – OriEng Mar 24 '17 at 07:34
  • myscript.js dosent have a variable named request so i got this error Error in event handler for (unknown): ReferenceError: request is not defined & email is not setting and pwd is setting normally – Dfx Mar 24 '17 at 08:26
  • sorry sir i put user :document.getElementById('email').value in my script and called request.email ... now its clear thankuuu :) – Dfx Mar 24 '17 at 08:30