如何通过Google Chrome扩展程序中的弹出窗口访问内容

时间:2012-04-04 19:39:50

标签: google-chrome google-chrome-extension

我正在google chrome中开发一个简单的页面操作扩展,它具有以下用途。

  1. 在点击时显示弹出窗口并提示输入用户名和密码。
  2. 经过一次小验证,它应该填写网站的用户名和密码,比如gmail.com。
  3. 我看到了几个关于如何在窗口加载时执行此操作的示例,我能够单独执行弹出窗口。但是我无法从弹出窗口的html访问父html。

    任何帮助都将受到高度赞赏。

    以下是我的代码的内容。

    的manifest.json:

    {
      "name": "My First Chrome App",
      "version": "1.0",
      "description": "Auto fill content",
      "background": { "scripts": ["background.js"] },
      "page_action" :
      {
        "default_icon" : "icon-19.png",
        "default_title" : "Auto Fill",
        "default_popup" : "test.html"   
      },
      "permissions" : [
      "tabs"
      ],
      "icons" : {
        "48" : "icon-48.png",
        "128" : "icon-128.png"
      },
      "manifest_version": 2 
    }
    

    background.js

    function checkForValidUrl(tabId, changeInfo, tab) {
      //checks whether the document contains Steve Jobs
      if (tab.url.indexOf("gmail")) {
        chrome.pageAction.show(tabId);
      }
    };
    chrome.tabs.onUpdated.addListener(checkForValidUrl);
    

    的test.html

    <html>
    <head>
        <title> Hi Steve </title>
        <script type="text/javascript">
            function fill()
            {
                alert("inside method");         
            }
        </script>
    </head>
    <body>
        <form name="userinfo" id="userinfo">
            username : 
            <input type="text" name="username"/>
            password :
            <input type="password" name="password"/>
            <input type="button" value="Log In" onclick="fill()";/>
        </form>
    </body>
    </html>
    

    谢谢, 香卡

1 个答案:

答案 0 :(得分:1)

为您的登录名创建一个包含用户名和密码字段的弹出窗口。

http://code.google.com/chrome/extensions/browserAction.html#popups

当提交弹出表单时,向后台页面发送一条消息,然后向您的(页面)内容脚本发送一条消息,然后该脚本负责填写并提交表单。

http://code.google.com/chrome/extensions/content_scripts.html

您需要了解传递消息才能执行此操作:

http://code.google.com/chrome/extensions/messaging.html

相关问题