python 将自定义浏览器选项卡插入Pythonista

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 将自定义浏览器选项卡插入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')

@on_main_thread
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 = CustomViewController.new().autorelease()
	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 = WKWebView.new().autorelease()
	webView.loadRequest_(NSURLRequest.requestWithURL_(nsurl('http://www.google.com')))
	
	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
	tabVC.addTabWithViewController_(vc)
	
def share(_self, _cmd):
	view = ObjCInstance(_self).view()
	url = view.URL()
	if url:
		url_str = str(url.absoluteString())
		dialogs.share_url(url_str)

def goBack(_self, _cmd):
	view = ObjCInstance(_self).view()
	view.goBack()

def goForward(_self, _cmd):
	view = ObjCInstance(_self).view()
	view.goForward()

def searchBarSearchButtonClicked_(_self, _cmd, _sb):
	searchbar = ObjCInstance(_sb)
	term = str(searchbar.text())
	searchbar.resignFirstResponder()
	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:
			view.loadRequest_(NSURLRequest.requestWithURL_(res.URL()))
			searchbar.text = res.URL().absoluteString()
		else:
			view.loadRequest_(NSURLRequest.requestWithURL_(nsurl('http://www.google.com/search?q=' + urllib.quote(term))))
	
if __name__ == '__main__':
	main()

以上是关于python 将自定义浏览器选项卡插入Pythonista的主要内容,如果未能解决你的问题,请参考以下文章

将自定义小部件添加到 Flutter TabBar 的活动选项卡

国际开发协会 Python + Qt。如何将我自己的选项卡插入 IDA 主选项卡小部件?

将 ActiveX 控件插入到 Powerpoint 幻灯片中

如何将自定义选项添加到 Microsoft Edge 右键菜单?

每 x 秒执行一次 Python 而无需重新打开选项卡

如何在一个选项卡中使用 Selenium 和 Python 逐一运行测试?