本文介绍了将文件中的自动填充数据导入 Chrome的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个用户的 CSV 文件(带有电子邮件和姓名).谷歌浏览器存储其自动填充数据,您可以在此处手动添加它们:chrome://settings/addresses
是否可以自动从文件中导入数据?
解决方案
设置 UI 使用内部 API chrome.autofillPrivate.saveAddress
(
for (const el of document.querySelectorAll('body > input'))el.remove();Object.assign(document.body.appendChild(document.createElement('input')), {类型:'文件',风格:'位置:绝对;顶部:2ex;右:0;z-index:999',onchange(e) {如果 (!this.files[0])返回;const fr = 新的 FileReader();fr.readAsText(this.files[0], 'UTF-8');fr.onload = () =>{for (fr.result.split(/
?
/)) {const [name, email] = line.split(',');chrome.autofillPrivate.saveAddress({电子邮件地址:[电子邮件],全名:[名称],});}};fr.onerror = 控制台错误;},});
- 要处理 CSV 中的引用字段和多行字段,您应该修改此原始代码,在 JavaScript 中有许多正确解析 CSV 的示例.
- 您可以将devtools snippets中的代码保存到以后再用.
- 您不能在扩展程序中使用此私有 API.
- 支持的字段及其类型在 autofill_private.js 中列出
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?
解决方案
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(/
?
/)) {
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
这篇关于将文件中的自动填充数据导入 Chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!