Skip to content

Instantly share code, notes, and snippets.

Forked from steventroughtonsmith/Add Web
Last active December 3, 2023 23:33
Show Gist options
  • Save omz/b39519b877c07dbc69f8 to your computer and use it in GitHub Desktop.
Save omz/b39519b877c07dbc69f8 to your computer and use it in GitHub Desktop.
Insert a custom browser tab into Pythonista
# coding: utf-8
from objc_util import *
import console
import urllib
import dialogs
WKWebView = ObjCClass('WKWebView')
UIViewController = ObjCClass('UIViewController')
UIBarButtonItem = ObjCClass('UIBarButtonItem')
NSURLRequest = ObjCClass('NSURLRequest')
UITextField = ObjCClass('UITextField')
UISearchBar = ObjCClass('UISearchBar')
NSDataDetector = ObjCClass('NSDataDetector')
def main():
rootVC = UIApplication.sharedApplication().keyWindow().rootViewController()
tabVC = rootVC.detailViewController()
methods = [share, goBack, goForward, searchBarSearchButtonClicked_]
protocols = ['OMTabContent', 'UISearchBarDelegate']
CustomViewController = create_objc_class('CustomViewController', UIViewController, methods=methods, protocols=protocols)
vc =
vc.title = 'Web'
share_item = UIBarButtonItem.alloc().initWithImage_style_target_action_(UIImage.imageNamed_('Action'), 0, vc, sel('share'))
back_item = UIBarButtonItem.alloc().initWithImage_style_target_action_(UIImage.imageNamed_('Back'), 0, vc, sel('goBack'))
fw_item = UIBarButtonItem.alloc().initWithImage_style_target_action_(UIImage.imageNamed_('Forward'), 0, vc, sel('goForward'))
vc.navigationItem().rightBarButtonItems = [share_item, fw_item, back_item]
webView =
searchbar = UISearchBar.alloc().initWithFrame_(((0, 0), (200, 32)))
searchbar.searchBarStyle = 2
searchbar.placeholder = 'Search or enter address'
searchbar.delegate = vc
# There seems to be a bug in objc_util that makes the regular method name translation fail for setAutocapitalizationType: (could have to do with the property being defined in a protocol, not completely sure).
ObjCInstanceMethod(searchbar, 'setAutocapitalizationType:')(0)
vc.navigationItem().titleView = searchbar
vc.view = webView
def share(_self, _cmd):
view = ObjCInstance(_self).view()
url = view.URL()
if url:
url_str = str(url.absoluteString())
def goBack(_self, _cmd):
view = ObjCInstance(_self).view()
def goForward(_self, _cmd):
view = ObjCInstance(_self).view()
def searchBarSearchButtonClicked_(_self, _cmd, _sb):
searchbar = ObjCInstance(_sb)
term = str(searchbar.text())
if term:
det = NSDataDetector.dataDetectorWithTypes_error_(1<<5, None)
res = det.firstMatchInString_options_range_(term, 0, (0, len(term)))
view = ObjCInstance(_self).view()
if res:
searchbar.text = res.URL().absoluteString()
view.loadRequest_(NSURLRequest.requestWithURL_(nsurl('' + urllib.quote(term))))
if __name__ == '__main__':
Copy link

Yay thanks

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