问题
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).
- go to
chrome://settings/addresses
- open devtools console
- 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