Created
May 29, 2019 09:39
-
-
Save hellricer/5a5eabc2fc0a4e58c7b0833abe283fce to your computer and use it in GitHub Desktop.
ELinks hooks providing a framework for manipulating DOM
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
import os | |
import re | |
import subprocess | |
from bs4 import BeautifulSoup | |
import rules | |
def replacer(url, html): | |
new = html | |
reload(rules) | |
for pattern in rules.replace: | |
if pattern in url: | |
for replacement in rules.replace[pattern]: | |
new = re.sub(replacement[0], replacement[1], new) | |
return new | |
def modifier(url, html): | |
soup = BeautifulSoup(html, 'html.parser') | |
soup.select | |
reload(rules) | |
for pattern in rules.modify: | |
if pattern in url: | |
for selector in rules.modify[pattern]: | |
[selector[1](e, soup) for e in soup.select(selector[0])] | |
return str(soup) | |
def edit_rules(): | |
subprocess.call(['vim', os.environ['HOME'] + '/.elinks/rules.py']) |
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
import elinks | |
import hooks | |
import devtools | |
def pre_format_html_hook(url, html): | |
reload(devtools) | |
html = devtools.modifier(url, html) | |
html = devtools.replacer(url, html) | |
return html |
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 -*- | |
def remove(e, soup): | |
e.decompose() | |
# list items are always rendered on new-lines in ELinks... | |
def to_span(e, soup): | |
span = soup.new_tag('span') | |
span.contents = e.contents | |
span.insert(0, soup.new_string(' • '.decode('utf-8'))) | |
e.replaceWith(span) | |
def add_border(e, soup): | |
e['border'] = 1 | |
e['frame'] = 'box' | |
def add_padding(e, soup): | |
level = int(e['width']) / 40 | |
td = soup.new_tag('td') | |
td['colspan'] = level | |
e.parent.insert(0, td) | |
modify = { | |
'news.ycombinator.com' : [ | |
('img[src="y18.gif"]', remove), | |
('table.itemlist, table.comment-tree', add_border), | |
('table.comment-tree > tr td img', add_padding), | |
], | |
'tolkiengateway.net' : [ | |
# pattern is CSS selector | |
('label[for=searchInput]', remove), | |
('input#searchGoButton', remove), | |
# you can also use lambdas: | |
('div.magnify', lambda e, soup: e.decompose()), | |
('.pBody > ul > li', to_span), | |
], | |
'docs.python.org' : [ | |
('div[role=navigation] > ul > li', to_span), | |
] | |
} | |
# regex substitutions | |
replace = { | |
'tolkiengateway.net' : [ | |
('<table border="0"', '<table border="1"'), | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment