Skip to content

Instantly share code, notes, and snippets.

@masonwan
Last active July 11, 2024 15:24
Show Gist options
  • Save masonwan/b33c5d4d6179112d3c7a170411cdaf27 to your computer and use it in GitHub Desktop.
Save masonwan/b33c5d4d6179112d3c7a170411cdaf27 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.

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