more simple version chrome opener than "Tools/script/google.py" (It's just a sample script.)
This file contains 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
# -*- coding: utf-8 -*- | |
import io | |
import sys | |
import os | |
import re | |
import json | |
import ctypes | |
__MYNAME__, _ = os.path.splitext( | |
os.path.basename(sys.modules[__name__].__file__)) | |
try: | |
from urllib.parse import quote_plus as url_quote_plus | |
from urllib.parse import quote as url_quote | |
_encode = lambda s: s | |
_decode = lambda s: s | |
def _path2fileschemeuri(path): | |
from urllib.request import pathname2url | |
from urllib.parse import urljoin | |
return urljoin("file:", pathname2url(path)) | |
except ImportError: | |
from urllib import quote_plus as _quote_plus | |
from urllib import quote as _quote | |
def url_quote_plus(s, safe='/', encoding=None, errors=None): | |
if encoding is None: | |
encoding = 'utf-8' | |
if errors is None: | |
errors = 'strict' | |
s = s.encode(encoding, errors) | |
return _quote_plus(s, safe) | |
def url_quote(s, safe='/', encoding=None, errors=None): | |
if encoding is None: | |
encoding = 'utf-8' | |
if errors is None: | |
errors = 'strict' | |
s = s.encode(encoding, errors) | |
return _quote(s, safe) | |
_encode = lambda s: s.encode(sys.getfilesystemencoding()) | |
_decode = lambda s: s.decode(sys.getfilesystemencoding()) | |
def _path2fileschemeuri(path): | |
from urllib import pathname2url | |
from urlparse import urljoin | |
return urljoin("file:", pathname2url(path)) | |
_roottab_defaults = json.loads("""[ | |
["search_duckduckgo", | |
"https://duckduckgo.com/?q={q}", true], | |
["search_google", | |
"https://www.google.com/search?q={q}", true], | |
["search_ecosia", | |
"https://www.ecosia.org/search?q={q}", true], | |
["search_youtube", | |
"https://www.youtube.com/results?search_query={q}", true], | |
["youtube_video", | |
"https://www.youtube.com/watch?v={q}", true], | |
["youtube_channel", | |
"https://www.youtube.com/channel/{q}/videos", true], | |
["translate_google", | |
"https://translate.google.com/?sl=auto&text={q}&op=translate", false], | |
["wikipedia_en", | |
"https://en.wikipedia.org/w/index.php?search={q}", false], | |
["wikipedia_ja", | |
"https://ja.wikipedia.org/w/index.php?search={q}", false] | |
]""") | |
if __name__ == '__main__': | |
# | |
_setting = os.path.join( | |
os.environ.get( | |
"HOME", os.environ.get("USERPROFILE")), | |
".{}.json".format(__MYNAME__)) | |
if not os.path.exists(_setting): | |
with io.open(_setting, "w", encoding="utf-8") as fo: | |
json.dump(_roottab_defaults, fo, indent=4, ensure_ascii=False) | |
_roottab = _roottab_defaults | |
else: | |
_roottab = json.load(io.open(_setting, encoding="utf-8")) | |
# | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument("param", nargs="+") | |
parser.add_argument( | |
"--mode", choices=[ | |
"as_is"] + [k for k, _, _ in _roottab], | |
default="as_is") | |
parser.add_argument( | |
"--extra_vers", default="{}", | |
help='''\ | |
for example, if you specify this '{"tl": "ja"}', | |
you can refer like "https://translate.google.com/?sl=auto&tl={tl}&text={q}&op=translate" | |
''') | |
args = parser.parse_args(sys.argv[1:]) | |
# | |
param = re.sub(r"\s+", " ", " ".join(args.param).strip()) | |
if args.mode == "as_is": | |
if re.match(r"[a-z]+://", param): | |
page = "{}".format(param) | |
else: | |
page = _path2fileschemeuri(os.path.abspath(param)) | |
else: | |
fmt, qfp = {k: (fmt, qf) for k, fmt, qf in _roottab}[args.mode] | |
qf = url_quote_plus if qfp else url_quote | |
kwa = json.loads(args.extra_vers) | |
kwa["q"] = qf(_decode(param)) | |
page = fmt.format(**kwa) | |
if hasattr(ctypes, "windll"): | |
ctypes.windll.shell32.ShellExecuteW( | |
0, | |
u"open", | |
u"chrome", | |
u" ".join([u"-newtab", u'"{}"'.format(page)]), | |
u"", | |
1) # 1: SW_SHOWNORMAL | |
else: | |
# sorry, i don't test on unix. | |
os.execvp("chrome", ["-newtab", page]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unless you hate browsers other than chrome, you should usually use the standard library "webbrowser". On the other hand, if you want to launch Chrome anyway, and for Windows users, this script can help alleviate some frustration. That's because on Windows, the location of chrome.exe is usually placed in a location that isn't in the PATH environment variable, and it's painful to launch it from the command line.