Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save erbanku/8f0a4e35940e36eb2d7886579ca1a558 to your computer and use it in GitHub Desktop.
Save erbanku/8f0a4e35940e36eb2d7886579ca1a558 to your computer and use it in GitHub Desktop.
Better way to manage Chrome's search engines

Problem

Chrome has the feature to automatically add search engine when it detects an input field on websites. After using Chrome months, it often resutls a bunch of search engines stayed in the settings. And the setting page does not provide a convinient way to remove them.

Updates on 2024-11-21

The editting the Web Data as SQLite file did not seem to work anymore. Chrome seems to revert all the changes when restart.

To make it even worse, it seems that the DOM tree is not accessable from the development console on the setting page, so even a bookmarklet could not work. Neighter does Chrome provide an API to access the search engines.

Updates on 2024-09-17

Please note tha the following solution did not seem to work anymore in 2024. I now recommend to update the SQLite file related to search engines setting directly. Probably use DB Browser for SQLite. And the file related to the search engine is %LOCALAPPDATA%\Google\Chrome\User Data\Default\Web Data on Windows (other platforms).

Solution

Stop auto-add search engines

Use Don't add custom search engines.

Delete auto created search engines

Note that in order to run the script, execute the code in the console of Developer Tool (⌘⌥J).

  1. Use export-search-engines.js to dump the current search engines, generating backup.json.
  2. Optionally use remove-auto-search-engines.js to remove the search engines which has more than 6 charcters for keyword from backup.json, generating output.json.
  3. Use clear-search-engines.js to remove all current search engines from Chrome.
  4. Import output.json back to Chrome.
settings.SearchEnginesBrowserProxyImpl.prototype.getSearchEnginesList()
.then(function (val) {
val.others.sort(function (a, b) { return b.modelIndex - a.modelIndex; });
val.others.forEach(function (engine) {
settings.SearchEnginesBrowserProxyImpl.prototype.removeSearchEngine(engine.modelIndex);
});
});
(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();
}
/* Actual search engine export magic */
settings.SearchEnginesBrowserProxyImpl.prototype.getSearchEnginesList()
.then((searchEngines) => {
downloadData('search_engines.json', JSON.stringify(searchEngines.others));
});
}());
(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]);
});
}());
import { createRequire } from 'module'
import fs from 'fs'
const require = createRequire(import.meta.url)
const searchEngines = require('./backup.json')
let map = searchEngines
.reduce((map, e) => {
if (map[e.keyword.length] === undefined) {
map[e.keyword.length] = []
}
map[e.keyword.length].push(e)
return map
}, {})
let newSearchEngines = Object.entries(map)
.filter(entry => parseInt(entry[0], 10) <= 6)
.reduce((list, entry) => {
list.push(...entry[1])
return list
}, [])
fs.writeFileSync('output.json', JSON.stringify(newSearchEngines))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment