简体   繁体   English

Chrome扩展程序:填写文本字段

[英]Chrome Extension : fill the text fields

My extension should automatically fill my gmail user name and password, but it's not working. 我的扩展程序应自动填写我的gmail用户名和密码,但无法正常工作。

manifest.json 的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 html文件

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

popup.js 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 我从互联网上获得了所有代码,而且我以前没有使用过Google扩展程序的经验,而且我用Google搜索了很多次以解决该问题,但找不到该问题的任何解决方案

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): 您可以在内容脚本和在popup.html(myscript.js)中运行的脚本之间使用消息传递

manifest.json 的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. 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 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 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 . 在myscript.js中,您需要通过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 . 祝好运 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM