Skip to content

Instantly share code, notes, and snippets.

@dj1020
Last active June 8, 2023 17:46
Show Gist options
  • Save dj1020/89788df1ce95ee174399cdca7fecfcea to your computer and use it in GitHub Desktop.
Save dj1020/89788df1ce95ee174399cdca7fecfcea to your computer and use it in GitHub Desktop.
Export Chrome Search Engine to a Json File, and Import into Brave
/*
* Source: https://superuser.com/a/1626575
* 2021/07/22 Tested successfully on Chrome Version 91.0.4472.164 (Official Build) (x86_64) on Mac
* Usage:
* 1. Open `chrome://settings/searchEngines` on Chrome
* 2. Open Developer Tools (Cmd+Opt+i)
* 3. Choose `Console` tab
* 4. Paste code below and save as a json file
*/
(function exportSEs() {
/* Auxiliary function to download a file with the exported data */
function downloadData(filename, data) {
const file = new File([data], { type: 'text/json' });
const elem = document.createElement('a');
elem.href = URL.createObjectURL(file);
elem.download = filename;
elem.click();
}
let searchEngines = [];
document.querySelector('settings-ui').shadowRoot
.querySelector('settings-main').shadowRoot
.querySelector('settings-basic-page').shadowRoot
.querySelector('settings-search-page').shadowRoot
.querySelector('settings-search-engines-page').shadowRoot
.querySelector('settings-search-engines-list#otherEngines').shadowRoot
.querySelectorAll('settings-search-engine-entry')
.forEach($el => searchEngines.push(
{
name: $el.shadowRoot.querySelector('#name-column').textContent,
keyword: $el.shadowRoot.querySelector('#keyword-column').textContent,
url: $el.shadowRoot.querySelector('#url-column').textContent
})
)
downloadData('search_engines.json', JSON.stringify(searchEngines));
}());
/*
* Source: https://superuser.com/a/1626575
* 2021/07/22 Tested successfully on Brave Version 1.26.77 Chromium: 91.0.4472.164 (Official Build) (x86_64) on Mac
* But not all imported, most of engines imported.
*/
(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