Created
January 5, 2022 15:47
-
-
Save karlmayer/b6d5ea049d5c8d073eb563946b8f5032 to your computer and use it in GitHub Desktop.
Chrome browser search engine export & import function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // export search engines | |
| // from https://superuser.com/a/1626575 | |
| // visit chrome://settings/searchEngines, run in Developer Tools (F12) | |
| (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)); | |
| }()); | |
| // import search engines | |
| (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