diff options
author | Ben Boeckel <MathStuf@gmail.com> | 2010-10-03 23:16:25 -0400 |
---|---|---|
committer | Ben Boeckel <MathStuf@gmail.com> | 2010-10-03 23:16:25 -0400 |
commit | be07c4f015137b223d25e1e8bb63b7851bc2923e (patch) | |
tree | 6c05595f776852d52b943bbf4f6f573c398c2979 /examples/data/scripts/per-site-settings.py | |
parent | 1d4b7c0763a89066faa717adea4d35e7a0ad8458 (diff) |
Add per-site-settings script
Allows per-site settings. Was originally meant to be a noscript
implementation, but having it be able to do any action was just too easy
from there.
Diffstat (limited to 'examples/data/scripts/per-site-settings.py')
-rwxr-xr-x | examples/data/scripts/per-site-settings.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/examples/data/scripts/per-site-settings.py b/examples/data/scripts/per-site-settings.py new file mode 100755 index 0000000..b8c6340 --- /dev/null +++ b/examples/data/scripts/per-site-settings.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python +# Per-site settings plugin + +# Format of the noscript file: +# +# <url><TAB><path><TAB><command> +# +# - url +# May either be a regex, or literal. If literal, it will block any +# subdomains as well. +# - path +# May either be a regex, or literal. If literal, it will block any +# decendent paths as well. +# - options +# Given to uzbl verbatim. + +import os +import re +import socket +import urlparse +import sys + +def noscript_path(): + env = os.environ + if 'XDG_DATA_HOME' in env: + root = env['XDG_DATA_HOME'] + else: + root = os.path.join(env['HOME'], '.local', 'share') + return os.path.join(root, 'uzbl', 'per-site-options') + +def grep_url(url, path, filepath): + entries = [] + with open(filepath, 'r') as f: + for line in f: + parts = line.split('\t', 2) + if (url.endswith(parts[0]) or re.match(parts[0], url)) and \ + (path.startswith(parts[1]) or re.match(parts[1], path)): + entries.append(parts[2]) + return entries + +def write_to_socket(commands, sockpath): + sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + sock.connect(sockpath) + for command in commands: + sock.write(command) + sock.close() + +if __name__ == '__main__': + sockpath = sys.argv[5] + url = urlparse.urlparse(sys.argv[6]) + + host, path = (url.hostname, url.path) + + commands = grep_url(host, path, noscript_path()) + + write_to_socket(commands, sockpath) |