Skip to content

Instantly share code, notes, and snippets.

@neilio
Last active April 28, 2020 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neilio/d92614097753bd301c1a42cdf9a59437 to your computer and use it in GitHub Desktop.
Save neilio/d92614097753bd301c1a42cdf9a59437 to your computer and use it in GitHub Desktop.
Import Chrome search keywords
(async function importSEs() {
/* Auxiliary function to open a file selection dialog */
function selectFileToRead() {
return new Promise((resolve) => {
const input = document.createElement('input');
input.setAttribute('type', 'file');
input.addEventListener('change', (e) => {
resolve(e.target.files[0]);
}, false);
input.click();
});
}
/* Auxiliary function to read data from a file */
function readFile(file) {
return new Promise((resolve) => {
const reader = new FileReader();
reader.addEventListener('load', (e) => {
resolve(e.target.result);
});
reader.readAsText(file);
});
}
const file = await selectFileToRead();
const content = await readFile(file);
const searchEngines = JSON.parse(content);
searchEngines.forEach(({ name, keyword, url }) => {
/* Actual search engine import magic */
chrome.send('searchEngineEditStarted', [-1]);
chrome.send('searchEngineEditCompleted', [name, keyword, url]);
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment