Import autofill data from a file into Chrome

放肆的年华 提交于 2021-02-18 19:09:46

问题


I have a CSV file of users (with email and name). Google Chrome stores its autofill data and you can add them manually here: chrome://settings/addresses

Is it possible to import the data automatically from the file?


回答1:


The settings UI uses an internal API chrome.autofillPrivate.saveAddress (source).

  1. go to chrome://settings/addresses
  2. open devtools console
  3. paste and run the code below that adds an input button in the top right corner where you can select your CSV file:
for (const el of document.querySelectorAll('body > input'))
  el.remove();
Object.assign(document.body.appendChild(document.createElement('input')), {
  type: 'file',
  style: 'position:absolute; top:2ex; right:0; z-index:999',
  onchange(e) {
    if (!this.files[0])
      return;
    const fr = new FileReader();
    fr.readAsText(this.files[0], 'UTF-8');
    fr.onload = () => {

      for (const line of fr.result.split(/\r?\n/)) {
        const [name, email] = line.split(',');
        chrome.autofillPrivate.saveAddress({
          emailAddresses: [email],
          fullNames: [name],
        });
      }

    };
    fr.onerror = console.error;
  },
});
  • To process quoted and multi-line fields in CSV you should modify this primitive code, there are many examples of parsing CSV in JavaScript properly.
  • You can save the code in devtools snippets to reuse it later.
  • You can't use this private API in an extension.
  • Supported fields and their types are listed in autofill_private.js


来源:https://stackoverflow.com/questions/56967729/import-autofill-data-from-a-file-into-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!