【问题标题】:Read value from chrome autofill with javascript chrome extension使用 javascript chrome 扩展从 chrome 自动填充中读取值
【发布时间】:2020-06-25 10:03:54
【问题描述】:

我想做什么:

开发 chrome 扩展程序,在网站上提交表单(通过按钮单击事件)。表单的输入字段由 chrome 自动填充填充。为此,请使用带有内容脚本的声明性 js 注入。表单如下所示:

           <form action="...." method="post">
                <table style="margin-top:4px; margin-bottom:4px">
                    <tr>
                        <td width="40%">
                            <label for="username">Login:</label>
                        </td>
                        <td>
                            <input name="i_username" type="text" id="username" autocapitalize="off" value="" autofocus/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="password">Passwort:</label>
                        </td>
                        <td>
                            <input name="i_password" type="password" id="password"/>
                        </td>
                    </tr>

                    <tr>

                        <td>
                            <button type="submit" name="proceed" value="Login">Login</button>
                        </td>
                    </tr>
                </table>
            </form>

会发生什么:

页面已加载。这些值由 chrome 自动填充填充,人眼可见。但是,当我尝试使用 javascript 读取输入值时,它们是空的。 (因此,使用按钮提交表单会出错,因为这些值是“空的”。)一旦真正的用户与页面交互,这些值就可以在我的 javascript 中访问。

我对这个主题做了很多研究,包括以下内容:

这些线程都没有得出最终结论。

我尝试了一系列解决方案来模拟“真实的用户交互”以及其他变通方法。我试图用 js 键事件或鼠标事件来模拟真实的用户输入。我还尝试触发诸如 .blur()、.focus() ... 之类的事件。

是否可以在完全没有用户交互的情况下以任何方式读取自动填充的值?

实际上,获取由 chrome 自动填充填充的值就足够了。例如,直接从 chrome 密码存储中读取它们会很棒(我知道这是不可能的)。

谢谢, OliDev

【问题讨论】:

    标签: javascript html google-chrome-extension autofill content-script


    【解决方案1】:

    我要回答我自己的问题:

    每个事件都有一个“isTrusted”属性: “Event 接口的 isTrusted 只读属性是一个布尔值,当事件由用户操作生成时为真,当事件由脚本创建或修改或通过 EventTarget.dispatchEvent() 调度时为假。” (来自https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted)。

    因此不可能使用编程的 javascript 事件来模拟“真实用户输入”。但是,只有当存在 isTrusted=true 事件时,chrome 的自动填充值才可用!

    注意:虽然 window.scroll() 事件有 isTrusted=true,但它不会使自动填充值可读。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-05
      • 2018-04-19
      • 1970-01-01
      • 2014-09-19
      • 1970-01-01
      • 2019-04-10
      • 2011-11-01
      • 2013-03-22
      相关资源
      最近更新 更多