Skip to content

Instantly share code, notes, and snippets.

@jsbain
Forked from steventroughtonsmith/Add Web Tab.py
Created December 5, 2016 10:41
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jsbain/75d9be3609721883144bcfec1c293d1d to your computer and use it in GitHub Desktop.
Insert a custom browser tab into Pythonista
# coding: utf-8
from Foundation import *
from QuartzCore import *
from UIKit import *
import console
WKWebView = ObjCClass('WKWebView')
@on_main_thread
def main():
rootVC = UIApplication.sharedApplication().windows()[0].rootViewController()
tabVC = rootVC.detailViewController()
methods = [openURL, search]
protocols = ['OMTabContent']
CustomViewController = create_objc_class('CustomViewController', UIViewController, methods=methods, protocols=protocols)
vc = CustomViewController.new()
vc.title = 'Web'
urlBarItem = UIBarButtonItem.alloc().initWithImage_style_target_action_(UIImage.imageNamed_('Textures/ionicons-link-24'),0,vc,sel('openURL'))
searchBarItem = UIBarButtonItem.alloc().initWithImage_style_target_action_(UIImage.imageNamed_('Textures/ionicons-search-24'),0,vc,sel('search'))
vc.navigationItem().rightBarButtonItems = [urlBarItem, searchBarItem]
webView = WKWebView.new()
webView.loadRequest_(NSURLRequest.requestWithURL_(NSURL.URLWithString_('http://www.google.com')))
vc.view = webView
tabVC.addTabWithViewController_(vc)
def openURL(_self, _cmd):
address = console.input_alert('Open URL')
if len(address) > 0:
ObjCInstance(_self).view().loadRequest_(NSURLRequest.requestWithURL_(NSURL.URLWithString_(address)))
def search(_self, _cmd):
term = console.input_alert('Search')
if len(term) > 0:
ObjCInstance(_self).view().loadRequest_(NSURLRequest.requestWithURL_(NSURL.URLWithString_('http://google.com/search?q='+term)))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment