Skip to content

Instantly share code, notes, and snippets.

@ninowalker
Last active March 24, 2023 23:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ninowalker/9952bf435f8acffa3ef59d6c538ca165 to your computer and use it in GitHub Desktop.
Save ninowalker/9952bf435f8acffa3ef59d6c538ca165 to your computer and use it in GitHub Desktop.
Import search engine definitions by CLI

Import Search Engines to Chrome

If your daily life means bouncing between github, JIRA, jenkins, AWS, etc... (and your team), you want to standardize shortcuts

https://www.ghacks.net/2018/03/30/custom-search-engines-in-google-chrome/

This script will update your preferences, and make for glorious browser efficiency.

Usage

⚠️ shut down Chrome first, otherwise it shall fail due to locking.

cat sample.json | python import-se.py
### Import Search Engines to Chrome
### Python 2; OSX
import os
import sqlite3
import json
import sys
def load(db, *records):
conn = sqlite3.connect(db)
c = conn.cursor()
max_id = int(c.execute('select max(id) from keywords').fetchone()[0])
for record in records:
if c.execute('select 1 from keywords where keyword = ?', [record['keyword']]).fetchone():
print "Record already exists: %s" % (record['short_name'])
continue
max_id += 1
record['id'] = max_id
keys = list(record.keys())
values = map(lambda k: record[k], keys)
c.execute("INSERT INTO keywords (%s) VALUES (%s)" % (",".join(keys), ",".join(["?"] * len(keys))), values)
print "Record added: %s" % (record['short_name'])
conn.commit()
conn.close()
if __name__ == '__main__':
db = os.path.expanduser("~/Library/Application Support/Google/Chrome/Default/Web Data")
data = json.load(sys.stdin)
if isinstance(data, dict):
data = [data]
load(db, data)
[
{
"short_name": "Github",
"keyword": "g",
"favicon_url": "https://github.com/favicon-ent.ico",
"url": "https://github.com/search?q={searchTerms}",
"safe_for_autoreplace": 0,
"date_created": "13162568566000000",
"usage_count": 100,
"last_modified": 13162568566000000,
"last_visited": 13188056277435693
}
]
@jimbrig
Copy link

jimbrig commented Mar 24, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment